Results 1 to 4 of 4

Thread: How can I get screen coordinates

  1. #1

    Thread Starter
    Fanatic Member sridharavijay's Avatar
    Join Date
    Sep 2002
    Location
    http://www.vijaysridhara.in
    Posts
    589

    How can I get screen coordinates

    I'm trying to place a form where the mouse is clicked. I'm always getting the client coordinates and not the screen coordinates. How can I get'em. Pls help

  2. #2
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    GetCursorPos api

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

  3. #3

    Thread Starter
    Fanatic Member sridharavijay's Avatar
    Join Date
    Sep 2002
    Location
    http://www.vijaysridhara.in
    Posts
    589
    OOPs!!! Too much...Any ways thank you
    this worked out for me
    Private Type POINTAPI
    x As Long
    y As Long
    End Type
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long

  4. #4
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

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