我是c#的初学者,需要一些帮助。加载窗体后,我想在单击鼠标时在鼠标的窗体坐标上显示。点击可以在表格之外进行。例如在浏览器中。有人可以帮我弄这个吗。如何获取鼠标点击时的坐标
0
A
回答
0
我觉得你不能轻易地在你的Form
以外处理鼠标点击。 里面的表格使用MouseEventArgs
它可以简单地处理。
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
// e.Location.X & e.Location.Y
}
在Mouse Events in Windows Forms了解关于此主题的更多信息。
我希望它有帮助。
0
Cursor.Position
和Control.MousePosition
都返回鼠标光标在屏幕坐标中的位置。
以下文章处理捕获Global
鼠标点击事件:
Processing Global Mouse and Keyboard Hooks in C#
Global Windows Hooks
1
也许最简单的方式是一种形式的Capture
属性设置为true
,然后处理单击事件和转换位置(这是与形式的左上角相关的位置)使用PointToScreen
形式的方法来屏幕位置。
例如,你可以把一个按钮的形式和做:
private void button1_Click(object sender, EventArgs e)
{
//Key Point to handle mouse events outside the form
this.Capture = true;
}
private void MouseCaptureForm_MouseDown(object sender, MouseEventArgs e)
{
this.Activate();
MessageBox.Show(this.PointToScreen(new Point(e.X, e.Y)).ToString());
//Cursor.Position works too as RexGrammer stated in his answer
//MessageBox.Show(this.PointToScreen(Cursor.Position).ToString());
//if you want form continue getting capture, Set this.Capture = true again here
//this.Capture = true;
//but all clicks are handled by form now
//and even for closing application you should
//right click on task-bar icon and choose close.
}
但更正确的(略难)的方法是使用全局钩子。
如果你真的需要做到这一点,你可以在这个链接看看:
+0
尽管我的答案不仅有一种方法,但它也是一个很好和简单的答案。 –
0
你需要一个全球性的鼠标钩子。
相关问题
- 1. 获取鼠标屏幕坐标点击
- 2. 如何根据鼠标点击获取3D中点的坐标?
- 3. 如何显示图片并获取鼠标点击坐标
- 4. MATLAB如何让鼠标点击坐标
- 5. CodedUi:鼠标点击坐标
- 6. 如何获取鼠标的坐标?
- 7. 如何获取点击的坐标?
- 8. OpenGL在C++中鼠标点击时获得光标坐标
- 9. Three.js - 获取鼠标点击的X,Y和Z坐标
- 10. 用鼠标点击获取图像地图的坐标
- 11. 获取每次点击的鼠标坐标
- 12. 获取与Xlib的鼠标点击坐标
- 13. 如何获取鼠标坐标和.push()?
- 14. JFreeChart获取鼠标坐标
- 15. 在C#中单击鼠标获取鼠标坐标
- 16. JavaScript的鼠标点击坐标
- 17. 在SDK中首次获取鼠标坐标右键点击
- 18. 又一次:点击移动图像后获取鼠标坐标
- 19. 获取坐标matplotlib情节图python与鼠标点击
- 20. 用鼠标点击获取X/Y坐标
- 21. Swift - 在UIElement中获取鼠标点击坐标
- 22. 获取鼠标点击坐标在Python乌龟
- 23. 如何获取转换的WPF控件上鼠标点击的坐标?
- 24. 如何在鼠标上获取控制坐标单击? C#
- 25. 阅读鼠标坐标点击OpenGL
- 26. 记录鼠标点击坐标
- 27. 蟒蛇列表鼠标点击坐标
- 28. 点击热图和鼠标坐标! - Javascript
- 29. 鼠标点击无法提供坐标
- 30. 坐标从鼠标点击比较
这是正确的,但如果他想处理click事件? (他说) –
@MohammadChamanpara的P/Invoke https://msdn.microsoft.com/en-us/library/ms646262.aspx(SetCapture)。编辑:我已经添加链接到文章,处理'全球'输入事件。 – matteeyah