获取控件上的单击点
我在我的 C# Win 应用程序中使用旧的 ActiveX 控件。 它有一个 MouseUp 事件,其 eventArgs 正在传递我们单击的点的 X 和 Y,但对于我的场景,我使用其 ItemClick 事件,并且其 eventArgs 没有有关 X 和 Y 的信息。 但我需要知道它们才能显示我的弹出窗口...那么有没有一种方法可以找出用户右键单击的 X 和 Y 的位置,以便我可以将其传递给我的 contextMenuStrip.Show 方法。
谢谢
I am using an old ActiveX control in my C# Win App.
it has a MouseUp event that its eventArgs is passing the X and Y of the point that we have clicked but for my scenario I am using its ItemClick event and its eventArgs does not have the info about X and Y.
but I need to know them to show my pop-up... so is there a way I can find out what is the location of X and Y that user has right-clicked so I can pass it to my contextMenuStrip.Show method.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Control 类有一个静态只读 MousePosition 属性,这给出了屏幕上的鼠标坐标。您可以使用它来了解上下文菜单的位置。
来自 MSDN:
The Control class has a static readonly MousePosition property, this gives the mouse coordinates on the screen. You could use this to know where to position the ContextMenu.
From MSDN:
Cursor.Position
将获取光标当前的屏幕坐标。对于大多数用途来说,这已经足够好了,尽管鼠标可能会在单击和调用的处理程序之间移动。Cursor.Position
will get you the current screen coordinates of the cursor. For most uses this is good enough, even though the mouse can potentially move between the click and the handler being called.您需要获取获取屏幕位置的光标位置,然后从控件内调用 pointToClient 来获取控件的相关点。又名。 0,0 是控件的左上角。
+1 其他答案引导我走向正确的方向。
You need to get the cursor position which gets the screen position, then call pointToClient from within the control to get the relevant point to the control. Aka. 0,0 is the top left of the control.
+1 to other answers for leading me in the right direction.