Results 1 to 6 of 6

Thread: Global Mouse Position Over My Form

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2009
    Location
    Anywhere I want to.
    Posts
    368

    Global Mouse Position Over My Form

    I need to get the mouse XY coordinates while over a form with several controls visible.

    I use this to get the global mouse position.
    Form scalemode is vbTwips.
    Form is 2 - Sizable
    Form has no control box or min max. Not a toolbar.
    Form has a Caption showing.

    SystemMetricsGet = GetSystemMetrics(eMetric)

    Coords X, Y ' just shows the values, X, Y are Variants in Coords

    Code:
    Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
    
        Dim fCapHeight  As Single
        Dim fFrmBorderWidth As Single
    
        ' Get position
        GetCursorPos RECT     ' API
        fCapHeight = ScaleY(SystemMetricsGet(SM_CYCAPTION), vbPixels, vbTwips)
        fFrmBorderWidth = ScaleX(SystemMetricsGet(SM_CXDLGFRAME), vbPixels, vbTwips)
        Coords csCursorXY, ScaleX(RECT.x, vbPixels, Units) - ScaleX(Me.Left + fFrmBorderWidth, vbTwips, Units), _
                                  ScaleY(RECT.y, vbPixels, Units) - ScaleY(Me.Top + fCapHeight, vbTwips, Units)
    
        ' Position via Mouse_Move
        Coords csPanelCoordXY, x, y       ' show cursor position for local X Y
    The XY of the two are off a little. What's wrong with my code ?

  2. #2
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,808

    Re: Global Mouse Position Over My Form

    I think I'm missing something.

    What's wrong with GetCursorPos?

    Code:
    
    Option Explicit
    Private Type POINTAPI
        X As Long
        Y As Long
    End Type
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    
    
    Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Dim MousePos As POINTAPI
        GetCursorPos MousePos
    
        Debug.Print "Mouse Position On Desktop (in Pixels):"
        Debug.Print "  x="; MousePos.X
        Debug.Print "  y="; MousePos.Y
    End Sub
    
    
    Or, are you asking a question about something? If so, you've posted in the codebank, and need to contact the moderators to have it moved to the Q&A section.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2009
    Location
    Anywhere I want to.
    Posts
    368

    Re: Global Mouse Position Over My Form

    First I am having trouble identifying where I am posting sometimes. Where on the web page does it show if I am in the Codebank or in the Forum ?
    Thank you !

    My code is trying to get the XY position of the mouse on the form (in my "Units") not the screen XY.
    Units may be Inch or mm or whatever.

    I need that XY of the form while hovering the mouse over objects (picturebox etc.) on the form.

    If not over any object, then mouse_move gives form XY. If I move the object, then form mouse_move should give the same coordinates in Units as does a computer form XY from GetCursorPos.

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    12,098

    Re: Global Mouse Position Over My Form

    Quote Originally Posted by LorinM View Post
    First I am having trouble identifying where I am posting sometimes. Where on the web page does it show if I am in the Codebank or in the Forum ?
    There is always the breadcrumbs at the top of the page just under the menu. Does it say VB6 and Earlier or Codebank - VB6 and Earlier? Post in the former if you have a question and the latter if you have a solution you'd like to share.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2009
    Location
    Anywhere I want to.
    Posts
    368

    Re: Global Mouse Position Over My Form

    Got it !

    Also how do I or a moderator say when satisfied and done ?


    Is you code as buggy as your posts ? LOL

    Even years later I find a bug in mine.

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    12,098

    Re: Global Mouse Position Over My Form

    Thread Tools > Marked as Resolved
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width