Private Declare Function GetCursorPos Lib "user32" (ByRef lpPoint As POINTAPI) As Long '全屏坐標(biāo)聲明
萬(wàn)安網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、APP開(kāi)發(fā)、響應(yīng)式網(wǎng)站等網(wǎng)站項(xiàng)目制作,到程序開(kāi)發(fā),運(yùn)營(yíng)維護(hù)。創(chuàng)新互聯(lián)自2013年起到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來(lái)保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)。
Private Declare Function ScreenToClient Lib "user32.dll" (ByVal hwnd As Int32, ByRef lpPoint As POINTAPI) As Int32 '窗口坐標(biāo)聲明
Private Structure POINTAPI '聲明坐標(biāo)變量
Public x As Int32 '聲明坐標(biāo)變量為32位
Public y As Int32 '聲明坐標(biāo)變量為32位
End Structure
'以上是聲明部分
'以下是窗口部分
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick '用時(shí)鐘控件來(lái)取坐標(biāo),窗口上放個(gè)Timer組件,Enabled為允許,周期為10到100毫秒均可
Dim P As POINTAPI '聲明坐標(biāo)變量
Dim xx, yy '聲明轉(zhuǎn)換要用到的變量
GetCursorPos(P) '獲取鼠標(biāo)在屏幕中的位置
ScreenToClient(Me.Handle.ToInt32, P) '轉(zhuǎn)換為本窗體的坐標(biāo)
xx = P.x.ToString '把X轉(zhuǎn)換成能顯示到文本框的字符串格式
yy = P.y.ToString '把Y轉(zhuǎn)換成能顯示到文本框的字符串格式
TextBox1.Text = xx + "和" + yy '文本框的內(nèi)容為X坐標(biāo)和Y坐標(biāo)
End Sub
Imports?System.Runtime.InteropServices
DllImport("user32.dll")?
Private?Shared?Function?SetCursorPos(ByVal?X?As?Integer,?ByVal?Y?As?Integer)?As?Boolean????End?Function
DllImport("user32.dll")?
Private?Shared?Sub?mouse_event(ByVal?flags?As?MouseEventFlag,?ByVal?dx?As?Integer,?ByVal?dy?As?Integer,?ByVal?data?As?UInteger,?ByVal?extraInfo?As?UIntPtr)????End?Sub
Flags()
Private?Enum?MouseEventFlag?As?UInteger???
Move?=?H1?
LeftDown?=?H2
LeftUp?=?H4
RightDown?=?H8
RightUp?=?H10
MiddleDown?=?H20?
MiddleUp?=?H40?
XDown?=?H80?
XUp?=?H100
Wheel?=?H800?
VirtualDesk?=?H4000
Absolute?=?H8000?
End?Enum
SetCursorPos(intX,?intY)mouse_event(MouseEventFlag.LeftDown,?intX,?intY,?0,?UIntPtr.Zero)
mouse_event(MouseEventFlag.LeftUp,?intX,?intY,?0,?UIntPtr.Zero)
你好 那因?yàn)槟惴旁诹薒abel1控件的移動(dòng)事件了 。
說(shuō)明:
當(dāng)前你的代碼是在當(dāng)鼠標(biāo)經(jīng)過(guò)Label1的MouseMove事件才被激活,其他事件不備激活的。
建議應(yīng)用函數(shù)調(diào)用具體方法網(wǎng)上應(yīng)該有
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
上面代碼我是做過(guò)測(cè)試才發(fā)的,完全能實(shí)現(xiàn)你的要求?。。。。?!