VB.net - 在表单之外获取鼠标坐标

时间:2012-09-10 20:49:58

标签: vb.net transparency coordinates

我正在Vb.net中编写一个收集信息的简单程序。诸如鼠标x y坐标,像素颜色和按键数字之类的东西。我希望能够在屏幕上的任何地方查看光标的x和y坐标,而不仅仅是在表单上,​​我想以最简单的方式做到这一点。我达到预期效果的一种方法是使用以下设置:

Picturebox2:

  • BackColor =红色
  • image = 3 x 2像素图像(几乎不可见,但需要全部相同)

Form1中:

  • 透明度键=红色

这导致在光标位于表单边界外时显示鼠标坐标的外观。但它仍然在形式上。我正在使用的代码是:

Dim mouseloc As Point


Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
    mouseloc = Cursor.Position
    lblc.Text = PointToClient(mouseloc).ToString
    lbls.Text = PointToScreen(mouseloc).ToString
End Sub

Private Sub PictureBox2_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseMove
    mouseloc = Cursor.Position
    lblc.Text = PointToClient(mouseloc).ToString
    lbls.Text = PointToScreen(mouseloc).ToString
End Sub

我在Windows 7 x64 Sony VAIO上运行Visual Studio 2010

enter image description here

1 个答案:

答案 0 :(得分:0)

一种非常简单的方法是通过Me.Capture = True在Form中捕获鼠标。有关详细信息,请参见此处:

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.capture.aspx

话虽这么说,如果你需要跟踪鼠标,即使你不是活跃的应用程序,你将不得不使用某种类型的挂钩。虽然不清楚你想要做什么。

相关问题