VB.NET - 鼠标坐标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2150448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB.NET - Mouse Coordinates
提问by lab12
I have a vb.net application, and I want to know how to find the coordinates of the pointer (mouse) when it is clicked on the form. Not much else to say, so I'll leave it like that.. :D
我有一个 vb.net 应用程序,我想知道在表单上单击时如何找到指针(鼠标)的坐标。没什么好说的,所以我就这样离开吧.. :D
Thanks
谢谢
回答by MaddMark
Very simple code to put the mouse coords in a text box
将鼠标坐标放入文本框中的非常简单的代码
Private Sub Form1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
Dim MPx As Point = MousePosition()
TextBox1.Text = MPx.ToString
End Sub
tried and tested,
久经考验,
回答by Anax
I believe you are looking for the mousedown
event. Mind that, if you are looking for actual screen coordinates, you might have to perform some calculations as well or use Windows API to get the coordinates fast.
我相信你正在寻找这个mousedown
事件。请注意,如果您正在寻找实际的屏幕坐标,您可能还需要执行一些计算或使用 Windows API 来快速获取坐标。
回答by DotNet Programmer
You can also try this
你也可以试试这个
Private Sub Form1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
TextBox1.Text = e.Location.ToString()
End Sub
I would not advise
我不建议
Private Sub Form1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
TextBox1.Text = MousePosition().ToString()
End Sub
because it may change after you first click down on the button because you moved your mouse. So its always better if you are doing a calculation to assign the location to a variable or in e.location case it is already a separate variable that doesn't change. This is also why it is a better choice than MousePosition since mouseposition will constantly change while its in this click function rather then e.location will remain the same till it leaves the mouseclick event.
因为在您第一次点击按钮后它可能会改变,因为您移动了鼠标。因此,如果您正在进行计算以将位置分配给变量,或者在 e.location 情况下,它已经是一个不会更改的单独变量,那么它总是更好。这也是为什么它是比 MousePosition 更好的选择,因为鼠标位置会不断变化,而它在这个点击函数中而不是 e.location 将保持不变,直到它离开鼠标点击事件。