Click to See Complete Forum and Search --> : Need clarification--Urgent


Harini
June 28th, 2001, 02:05 PM
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

John G Duffy
June 28th, 2001, 04:23 PM
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

shree
June 28th, 2001, 06:51 PM
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.