我有一个面板,里面有许多图片盒.每个图片框都注册了“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); } }