Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PopupMenu position 1

Status
Not open for further replies.

AndyGroom

Programmer
May 23, 2001
972
GB
Why doesn't this code display the popupmenu where the mouse cursor is?

Dim CP as PointAPI
a& = GetCursorPos(CP)
PopupMenu MyMenu, , CP.X, CP.Y

I know that I can omit the X and Y parameters from the PopupMenu call and the menu will appear at the cursor position, but my problem is that I need to record where the mouse was while I display another form, and then show the menu where the mouse originally was when the click occured. The user could have moved the mouse between the click and the form appearing.

- Andy
_______________________________
"On a clear disk you can seek forever"
 
The GetCursorPos will return the X and Y coordinates of the mouse pointer so i think that it will pop up the menu where the mouse stands!!

I can't understand the 2nd part..so if you can elaborate!

Nick
 
Two reasons for this behaviour.

1. The X and Y arguments of the PopupMenu method are relative to the client area of the form which is calling this method. The GetCursorPos function retrieves the mouse position in screen coordinates.

2. The X and Y arguments are measured in the scalemode of the calling form (which is twips by default) whereas the GetCursorPos function retrieves the position in pixels.

You can overcome this problem using the following code.
___
[tt]
'Declarations required.
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function ScreenToClient Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
Private Type POINTAPI
x As Long
y As Long
End Type
'
'...
'
Dim P As POINTAPI
GetCursorPos P
ScreenToClient hwnd, P
PopupMenu MyMenu, , ScaleX(P.x, vbPixels, ScaleMode), ScaleY(P.y, vbPixels, ScaleMode)[/tt]
___

The ScreenToClient function maps screen coordinates to form's client area. The PopupMenu method is called with X and Y converted from pixels to the form's scalemode. If the form's scalemode is set to twips as default then you could use a simpler conversion like this.

[tt]PopupMenu MyMenu, , P.x \ Screen.TwipsPerPixelX, P.y \ Screen.TwipsPerPixelY[/tt]
 
Err...

The last line in the above post should be read as:
[tt]
PopupMenu MyMenu, , P.x * Screen.TwipsPerPixelX, P.y * Screen.TwipsPerPixelY[/tt]
 
Thanks Hypetia, I'll give it a go.

- Andy
_______________________________
"On a clear disk you can seek forever"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top