C# 出现关联菜单时如何获取鼠标位置?

C# 出现关联菜单时如何获取鼠标位置?,c#,compact-framework,event-handling,location,C#,Compact Framework,Event Handling,Location,我有一个面板,里面有很多图片盒。每个picturebox都注册了“contextRightMenu”作为其上下文菜单 当关联菜单弹出时,我想要的是获取当前鼠标位置 我已经尝试使用mouseDown和click来获取mouseposition,但是这些事件发生在上下文菜单的一个项目被单击之后,已经太晚了 上下文菜单的弹出事件不提供鼠标事件参数,因此我不知道如何获取鼠标位置 如果我能得到鼠标事件参数,这很容易 那我就可以: this.contextRightClick.Popup += new S

我有一个面板,里面有很多图片盒。每个picturebox都注册了“contextRightMenu”作为其上下文菜单

当关联菜单弹出时,我想要的是获取当前鼠标位置

我已经尝试使用mouseDown和click来获取mouseposition,但是这些事件发生在上下文菜单的一个项目被单击之后,已经太晚了

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

如果我能得到鼠标事件参数,这很容易

那我就可以:

 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
}
有人能帮我获取一些鼠标事件参数,或者建议在contextmenu弹出窗口之前运行的事件吗


提前感谢

您可以尝试picturebox的鼠标单击事件,如果是右键单击,则可以获取位置。

处理picturebox的鼠标单击。类似这样的内容(在vb.net中):


您可能想要查看,例如,您想要光标相对于右键单击的图片框的位置,还是相对于父面板的位置,或者父窗口的位置,或者可能只是屏幕位置

以下内容可能有助于作为起点。在这里,我得到整个屏幕上的当前鼠标坐标,然后使用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);
  }
}

mouseclick没有任何mouseevent argsTry MouseDown或MouseUp事件。不知道CF中支持的内容。还可以查看Cursor.Position属性。找到了一些内容:private void panel mouseclick(对象发送方,EventArgs e){MouseEventArgs args=e as MouseEventArgs;}但是args只是返回null,无论如何:(尽量使用“e”。我认为CF event中的类型是MouseEventArgs,尽管它是EventArgs。但不确定。嗨!这是我感兴趣的父面板的位置。但我会研究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);
  }
}