用API GetCursorPos
为静宁等地区用户提供了全套网页设计制作服务,及静宁网站建设行业解决方案。主营业务为网站建设、网站设计、静宁网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!
用Timer隔一段时间获取一次鼠标位置。
键盘输入,用 GetKeyboardState
加点分的话就给你完整的代码
Public Class NativeMethods
'获取鼠标位置
Declare Function GetCursorPos Lib "user32.dll" _
(ByRef lpPoint As System.Drawing.Point) as boolean
Private Shared keyState() As Byte
DllImport("user32.dll") _
Private Shared Function GetKeyboardState(ByVal keyState() As Byte) As Boolean
End Function
Private Shared Sub Update()
keyState = New Byte(256) {}
Dim result As Boolean = GetKeyboardState(keyState)
' Check for error:
If result = False Then
Debug.WriteLine("GetKeyBoardState error: " Marshal.GetLastWin32Error)
Throw New Exception("GetKeyBoardState error: " Marshal.GetLastWin32Error)
End If
End Sub
Public Enum LightState
Off
[On]
End Enum
' Example - the keyboard lights...
Public Shared ReadOnly Property CapsLockState() As LightState
Get
Update()
Dim isOn As Boolean = (keyState(Keys.CapsLock) = 1)
Return IIf(isOn, LightState.On, LightState.Off)
End Get
End Property
Public Shared ReadOnly Property NumLockState() As LightState
Get
Update()
Dim isOn As Boolean = (keyState(Keys.NumLock) = 1)
Return IIf(isOn, LightState.On, LightState.Off)
End Get
End Property
Public Shared ReadOnly Property ScrollLockState() As LightState
Get
Update()
Dim isOn As Boolean = (keyState(Keys.Scroll) = 1)
Return IIf(isOn, LightState.On, LightState.Off)
End Get
End Property
End Class
pinvoke.net: GetCursorPos (user32)
pinvoke.net: GetKeyboardState (user32)
Private Declare Function GetCursorPos Lib "user32" (ByRef lpPoint As POINTAPI) As Long '全屏坐标声明
Private Declare Function ScreenToClient Lib "user32.dll" (ByVal hwnd As Int32, ByRef lpPoint As POINTAPI) As Int32 '窗口坐标声明
Private Structure POINTAPI '声明坐标变量
Public x As Int32 '声明坐标变量为32位
Public y As Int32 '声明坐标变量为32位
End Structure
'以上是声明部分
'以下是窗口部分
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick '用时钟控件来取坐标,窗口上放个Timer组件,Enabled为允许,周期为10到100毫秒均可
Dim P As POINTAPI '声明坐标变量
Dim xx, yy '声明转换要用到的变量
GetCursorPos(P) '获取鼠标在屏幕中的位置
ScreenToClient(Me.Handle.ToInt32, P) '转换为本窗体的坐标
xx = P.x.ToString '把X转换成能显示到文本框的字符串格式
yy = P.y.ToString '把Y转换成能显示到文本框的字符串格式
TextBox1.Text = xx + "和" + yy '文本框的内容为X坐标和Y坐标
End Sub
你好 那因为你放在了Label1控件的移动事件了 。
说明:
当前你的代码是在当鼠标经过Label1的MouseMove事件才被激活,其他事件不备激活的。
建议应用函数调用具体方法网上应该有
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
TextBox1.Text = System.Windows.Forms.Cursor.Position.X.ToString "," System.Windows.Forms.Cursor.Position.Y.ToString
End Sub
上面代码我是做过测试才发的,完全能实现你的要求!!!!!!
控件中keyup属性记录键盘操作
比如:
Private Sub from1_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
if e.keycode = keys.Enter then
lable1.text =Cursor.Position.X
lable2.text = Cursor.Position.Y
end if
end sub