当上下文菜单出现时如何获取鼠标位置?
我有一个面板,里面有很多图片框。每个图片框都注册了“contextRightMenu”作为其上下文菜单。
当上下文菜单弹出时我想要的是获取当前的鼠标位置。
我尝试通过使用 mouseDown 并单击来获取鼠标位置,但这些事件在单击上下文菜单的一项后发生,但为时已晚。
上下文菜单的弹出事件不传递鼠标事件参数,所以我不知道如何获取鼠标位置。
如果我可以获得鼠标事件参数,那就很容易了。
然后我就可以:
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
}
任何人都可以帮助我获取一些鼠标事件参数,或者建议一个将在上下文菜单弹出之前运行的事件吗?
提前致谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您想要光标位置相对于右键单击的 PictureBox 或相对于父面板、父窗口或可能只是屏幕位置吗?
以下内容可能有助于作为起点。在这里,我获取整个屏幕上的当前鼠标坐标,然后使用 contextRightMenu 中的 SourceControl(它是对右键单击的控件实例的引用),我们将屏幕坐标转换为相对于源控件的点。
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.
处理 PictureBox 的鼠标点击。像这样的东西(在vb.net中):
Handle the MouseClick of your PictureBox. Something like this (in vb.net):
你可以尝试一下picturebox的MouseClick事件,如果是右键单击,则获取位置。
You can try the MouseClick event of picturebox and get the location if it is a right click.
您可能想看看 ContextMenuStrip 类 和 Control.ContextMenuStripChanged 事件 ,一些例子这里
you may wantto take a look to ContextMenuStrip Class and Control.ContextMenuStripChanged Event, some exemple here