当上下文菜单出现时如何获取鼠标位置?

发布于 2024-08-29 02:47:11 字数 766 浏览 4 评论 0原文

我有一个面板,里面有很多图片框。每个图片框都注册了“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

翻了热茶 2024-09-05 02:47:11

您想要光标位置相对于右键单击的 PictureBox 或相对于父面板、父窗口或可能只是屏幕位置吗?

以下内容可能有助于作为起点。在这里,我获取整个屏幕上的当前鼠标坐标,然后使用 contextRightMenu 中的 SourceControl(它是对右键单击的控件实例的引用),我们将屏幕坐标转换为相对于源控件的点。

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);
  }
}

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);
  }
}
此刻的回忆 2024-09-05 02:47:11

处理 PictureBox 的鼠标点击。像这样的东西(在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

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
垂暮老矣 2024-09-05 02:47:11

你可以尝试一下picturebox的MouseClick事件,如果是右键单击,则获取位置。

You can try the MouseClick event of picturebox and get the location if it is a right click.

风为裳 2024-09-05 02:47:11

you may wantto take a look to ContextMenuStrip Class and Control.ContextMenuStripChanged Event, some exemple here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文