CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 1999
    Posts
    84

    Need clarification--Urgent

    I have placed a command button on a form, which starts up in maximized state.

    I am using the GetCursorPos API and getting the cursor position.

    The values of the x and y co-ordinates returned are lesser than the left and top of the command button, inspite of the command button being placed to a much more left and top positions.

    I am not getting where am I going late.

    Please suggest accordingly.

    Thanks
    Harini

  2. #2
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Need clarification--Urgent

    GetCurPos API returns the co-ordinates in PIXELS. Normally referencing, say a command button, the co-ordinates are returned in Twips. To convert multiply the returned values by Screen.TwipsPerpixelsX or Y like so

    private Type RECT
    Left as Long
    Top as Long
    Right as Long
    Bottom as Long
    End Type
    'Declare the API-Functions
    private Declare Function GetCursorPos Lib "user32" (lpPoint as POINTAPI) as Long
    private Declare Function SetCursorPos Lib "user32" (byval X as Long, byval Y as Long) as Long
    private Declare Function GetWindowRect Lib "user32" (byval hwnd as Long, lpRect as RECT) as Long
    '
    '
    private Sub Command1_Click()
    Dim Rec as RECT
    'get Left, Right, Top and Bottom of Form1
    GetWindowRect Form1.hwnd, Rec
    'set Cursor position on X
    SetCursorPos Rec.Right - 15, Rec.Top + 15
    ' Display my co-ordinates
    print me.Top
    print me.Left
    ' display my co-ordinates using the RECTangle
    print Rec.Top '* Screen.TwipsPerPixelY
    print Rec.Left '* Screen.TwipsPerPixelX
    print Rec.Top * Screen.TwipsPerPixelY
    print Rec.Left * Screen.TwipsPerPixelX
    End Sub




    John G

  3. #3
    Join Date
    Mar 1999
    Location
    Nepal
    Posts
    540

    Re: Need clarification--Urgent

    Another method to remove the discrepancy is to use the ScreenToClient() API function. It will convert the screen coordinates in pixels returned by GetCursorPos() to whatever unit the client uses, considering the height of the title bar and regardless of the top and left properties of the form.




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

Featured