I have a panel which holds many pictureboxes. Each picturebox has registered "contextRightMenu" as their context menu.
What i want when the context menu pops up is to get the current mouseposition.
I have tried getting the mouseposition by using mouseDown and click, but these events happens after one of the items of the context menu is clicked, and that is too late.
the popup event of the context menu does not deliver mouse event args, so i don't know how to get the mouseposition.
If i can get mouse event args it 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 + e.X, iLocationY + e.Y); // Location + position within the sender = current mouseposition
}
Can anyone help me either get some mouse event args, or suggest a event that will run before the contextmenu pop ups?
Thanks in advance
Do you want the cursor location relative to the PictureBox that was right clicked or relative to the parent Panel, or the parent Window or possibly just the screen position?
The following might help as a starting point. Here I get the current mouse cooridnates on the entire screen then using the SourceControl from the contextRightMenu, which is a reference to the instance of the control that was right clicked on, 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);
}
}
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(e.X, e.Y))
'if you need just the location
e.Location
end if
end sub
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With