开发者

VB.NET - Mouse Coordinates

I have a vb.net application, and I want to know how to find the co开发者_开发百科ordinates of the pointer (mouse) when it is clicked on the form. Not much else to say, so I'll leave it like that.. :D

Thanks


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,


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜