How to get the mouse cursor when the context menu appears?

I have a panel that contains a lot of images. Each graphic box registered a context menu contextRightMenu.

What I want when the context menu appears is to get the current mouse position.

I tried to get a mouseposition using mouseDown and click, but these events occur after clicking one of the context menu items, and it's too late.

the popup event of the context menu does not pass the arguments of the mouse events, so I don’t know how to get the mouse pointer.

If I can get the mouse event arguments, this is easy.

Then I just can:

this.contextRightClick.Popup += new System.EventHandler(this.contextRightClick_Popup); // If EventArgs include mouseposition within the sender private void contextRightClick_Popup)(object sender, EventArgs e) { int iLocationX = sender.Location.X; int iLocationY = sender.Location.Y; Point pPosition = new Point(iLocationX + eX, iLocationY + eY); // Location + position within the sender = current mouseposition } 

Can someone help me either get some arguments for the mouse events, or suggest an event that will be executed before the context menus appear?

Thanks in advance

+6
c # event-handling compact-framework location
source share
4 answers

Do you want the cursor position relative to the PictureBox to be right-clicked or relative to the parent panel or parent window, or perhaps only the screen position?

The following may help as a starting point. Here I get the current mouse coordinates in full screen, and then using the SourceControl from the contextualRightMenu, which is a link to the instance of the control that was right-clicked, we convert the screen coordinates to a point relative to the source control.

 void contextRightMenu_Popup(object sender, EventArgs e) { ContextMenu menu = sender as ContextMenu; if (menu != null) { // Get cursor position in screen coordinates Point screenPoint = Cursor.Position; // Convert screen coordinates to a point relative to the control // that was right clicked, in your case this would be the relavant // picture box. Point pictureBoxPoint = menu.SourceControl.PointToClient(screenPoint); } } 
+8
source share

Handle the MouseClick of your PictureBox. Something like this (in vb.net):

 Sub OnMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) handles YourPictureBox.mouseclick If e.Button = Windows.Forms.MouseButtons.Right then 'if you need the screen posistion PointToScreen(New System.Drawing.Point(eX, eY)) 'if you need just the location e.Location end if end sub 
+1
source share

You can try the event with the MouseClick image and get it if it is a right-click.

0
source share

you might want to take a look at the ContextMenuStrip class and the .ContextMenuStripChanged Event Management class , some examples here

0
source share

All Articles