加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 04:24:49 所属栏目:百科 来源:网络整理
导读:我有一个面板,里面有许多图片盒.每个图片框都注册了“contextRightMenu”作为其上下文菜单. 弹出上下文菜单时我想要的是获取当前的鼠标位置. 我尝试使用mouseDown并单击来获取鼠标位置,但这些事件发生在单击上下文菜单的其中一个项目之后,这为时已晚. 上下文
我有一个面板,里面有许多图片盒.每个图片框都注册了“contextRightMenu”作为其上下文菜单.

弹出上下文菜单时我想要的是获取当前的鼠标位置.

我尝试使用mouseDown并单击来获取鼠标位置,但这些事件发生在单击上下文菜单的其中一个项目之后,这为时已晚.

上下文菜单的弹出事件不会传递鼠标事件参数,因此我不知道如何获取鼠标位置.

如果我能得到鼠标事件args很容易.

那我就可以:

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
}

任何人都可以帮助我获得一些鼠标事件args,或建议一个事件将在contextmenu弹出窗口之前运行?

提前致谢

解决方法

您是否希望光标位置相对于右键单击或相对于父面板,或父窗口或可能只是屏幕位置的PictureBox?

以下可能有助于作为起点.在这里,我在整个屏幕上获取当前鼠标cooridnates然后使用来自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);
  }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读