Results 1 to 2 of 2

Thread: Position of the Cursor in X,Y Coords

  1. #1
    DerFarm
    Guest

    Position of the Cursor in X,Y Coords

    I've got a program on one window which requires a left-mouse
    holddown (3rd party, proprietary software....I can't change it).
    When I hit a hotkey my other program reads the text from the
    screen and throws up a form to check that the data was read
    correctly. If the form does not come up under the cursor, moving
    the cursor will cause the data on program 1 to change....kinda
    makes it hard to modify the fields and still see the actual data.

    GetCursor will return the handle for the cursor, but how can I get
    the position of the cursor? If I can find the cursor position in X,Y
    coordinates, I can move my form to include the silly thing, thus
    preserving the data for verification.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    The GetCursorPos function retrieves the cursor’s position, in screen coordinates.
    VB Code:
    1. 'This example uses a Form, called 'Form1'
    2. 'a Picture Box, called 'ExplButton' (50x50 pixels)
    3. 'a Picture Box with an icon in it, called 'picIcon'
    4. 'two timers (Timer1 and Timer2), both with interval 100
    5. 'Button, called 'Command1'
    6.  
    7. Private Type RECT
    8.     Left As Long
    9.     Top As Long
    10.     Right As Long
    11.     Bottom As Long
    12. End Type
    13. Private Type POINTAPI
    14.     X As Long
    15.     Y As Long
    16. End Type
    17.  
    18. 'Declare the API-Functions
    19. Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    20. Private Declare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long
    21. Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
    22. Sub DrawButton(Pushed As Boolean)
    23.     Dim Clr1 As Long, Clr2 As Long
    24.     If Pushed = True Then
    25.         'If Pushed=True then clr1=Dark Gray
    26.         Clr1 = &H808080
    27.         'If Pushed=True then clr1=White
    28.         Clr2 = &HFFFFFF
    29.     ElseIf Pushed = False Then
    30.         'If Pushed=True then clr1=White
    31.         Clr1 = &HFFFFFF
    32.         'If Pushed=True then clr1=Dark Gray
    33.         Clr2 = &H808080
    34.     End If
    35.  
    36.     With Form1.ExplButton
    37.         ' Draw the button
    38.         Form1.ExplButton.Line (0, 0)-(.ScaleWidth, 0), Clr1
    39.         Form1.ExplButton.Line (0, 0)-(0, .ScaleHeight), Clr1
    40.         Form1.ExplButton.Line (.ScaleWidth - 1, .ScaleHeight - 1)-(.ScaleWidth - 1, 0), Clr2
    41.         Form1.ExplButton.Line (.ScaleWidth - 1, .ScaleHeight - 1)-(0, .ScaleHeight - 1), Clr2
    42.     End With
    43. End Sub
    44. Private Sub Command1_Click()
    45.     Dim Rec As RECT
    46.     'Get Left, Right, Top and Bottom of Form1
    47.     GetWindowRect Form1.hwnd, Rec
    48.     'Set Cursor position on X
    49.     SetCursorPos Rec.Right - 15, Rec.Top + 15
    50. End Sub
    51. Private Sub ExplButton_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    52.     DrawButton True
    53. End Sub
    54. Private Sub ExplButton_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    55.     DrawButton False
    56. End Sub
    57. Private Sub ExplButton_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    58.     DrawButton False
    59. End Sub
    60. Private Sub Form_Load()
    61.     'KPD-Team 1998
    62.     'URL: [url]http://www.allapi.net/[/url]
    63.     'E-Mail: [email][email protected][/email]
    64.  
    65.     Dim Stretched As Boolean
    66.     'picIcon.Visible = False
    67.     'API uses pixels
    68.     picIcon.ScaleMode = vbPixels
    69.     'No border
    70.     ExplButton.BorderStyle = 0
    71.     'API uses pixels
    72.     ExplButton.ScaleMode = vbPixels
    73.     'Set graphic mode te 'persistent graphic'
    74.     ExplButton.AutoRedraw = True
    75.     'API uses pixels
    76.     Me.ScaleMode = vbPixels
    77.     'Set the button's caption
    78.     Command1.Caption = "Set Mousecursor on X"
    79.  
    80.     ' If you set Stretched to true then stretch the icon to te Height and Width of the button
    81.     ' If Stretched=False, the icon will be centered
    82.     Stretched = False
    83.  
    84.     If Stretched = True Then
    85.         ' Stretch the Icon
    86.         ExplButton.PaintPicture picIcon.Picture, 1, 1, ExplButton.ScaleWidth - 2, ExplButton.ScaleHeight - 2
    87.     ElseIf Stretched = False Then
    88.         ' Center the picture of the icon
    89.         ExplButton.PaintPicture picIcon.Picture, (ExplButton.ScaleWidth - picIcon.ScaleWidth) / 2, (ExplButton.ScaleHeight - picIcon.ScaleHeight) / 2
    90.     End If
    91.     ' Set icon as picture
    92.     ExplButton.Picture = ExplButton.Image
    93. End Sub
    94. Private Sub Timer1_Timer()
    95.     Dim Rec As RECT, Point As POINTAPI
    96.     ' Get Left, Right, Top and Bottom of Form1
    97.     GetWindowRect Me.hwnd, Rec
    98.     ' Get the position of the cursor
    99.     GetCursorPos Point
    100.  
    101.     ' If the cursor is located above the form then
    102.     If Point.X >= Rec.Left And Point.X <= Rec.Right And Point.Y >= Rec.Top And Point.Y <= Rec.Bottom Then
    103.         Me.Caption = "MouseCursor is on form."
    104.     Else
    105.         ' The cursor is not located above the form
    106.         Me.Caption = "MouseCursor is not on form."
    107.     End If
    108. End Sub
    109. Private Sub Timer2_Timer()
    110.     Dim Rec As RECT, Point As POINTAPI
    111.     ' Get Left, Right, Top and Bottom of ExplButton
    112.     GetWindowRect ExplButton.hwnd, Rec
    113.     ' Get the position of the cursor
    114.     GetCursorPos Point
    115.     ' If the cursor isn't located above ExplButton then
    116.     If Point.X < Rec.Left Or Point.X > Rec.Right Or Point.Y < Rec.Top Or Point.Y > Rec.Bottom Then ExplButton.Cls
    117. End Sub

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