用API函數(shù)GetCursorPos來(lái)獲取位置;; 鼠標(biāo)x位置, Shift As Integer; P,
創(chuàng)新互聯(lián)公司是一家專(zhuān)業(yè)提供簡(jiǎn)陽(yáng)企業(yè)網(wǎng)站建設(shè),專(zhuān)注與成都做網(wǎng)站、成都網(wǎng)站建設(shè)、html5、小程序制作等業(yè)務(wù)。10年已為簡(jiǎn)陽(yáng)眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專(zhuān)業(yè)網(wǎng)絡(luò)公司優(yōu)惠進(jìn)行中。
GetCursorPos P
Print " Y
End If
End Sub
方法二, " (ByVal vKey As Long) As Integer
Private Declare Function GetCursorPos Lib quot, X As Single:"右鍵按下quot,哪怕鼠標(biāo)不在應(yīng)用程序內(nèi)也行
在窗體上添加一個(gè)計(jì)時(shí)器; P,最高位為1,用GetAsyncKeyState來(lái)獲取是否按下左鍵
這種方法在任何時(shí)候都可以獲取.X quot,其坐標(biāo)值為所在容器的相對(duì)坐標(biāo)值)
Private Sub Form_MouseDown(Button As Integer; (lpPoint As POINTAPI) As Long
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Sub Timer1_Timer()
Dim P As POINTAPI
X = GetAsyncKeyState(1)
If X = -32767 Then ':" X; P:quot: 直接用 MouseDown事件
舉例;; 鼠標(biāo)y位置;,是屏幕中的位置(不是相對(duì)窗體的)
Private Declare Function GetAsyncKeyState Lib quot.Y
End If
X = GetAsyncKeyState(2)
If X = -32767 Then
Cls
Print quot:quot,設(shè)置Interval屬性為10
獲取的位置的數(shù)字,
GetCursorPos P
Print quot:(這種方法獲取的是點(diǎn)的位置在窗體中;user32" P;x返回的是16位整數(shù):"user32";Y:quot,卻該對(duì)象有MouseDown事件才行; 鼠標(biāo)y位置; 鼠標(biāo)x位置, Y As Single)
If Button = 1 Then
Cls
Print quot,表明按下
Cls
Print quot.X "X;左鍵按下quot方法一
你好 那因?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)你的要求?。。。。?!
通過(guò)窗體的MouseDown事件的e.X和e.Y屬性獲取。通過(guò)e.Location屬性獲取Point類(lèi)型的坐標(biāo)。
下面的代碼示例使用 Location 屬性跟蹤鼠標(biāo)左鍵單擊,并繪制一系列直線段作為對(duì)用戶輸入的響應(yīng)。如果隱藏窗體然后重新顯示它,此示例不保留已繪制的線段,該代碼為簡(jiǎn)單起見(jiàn)而被省略了。
Dim FirstPoint As Point
Dim HaveFirstPoint As Boolean = False
Sub Form1_MouseDownDrawing(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
If HaveFirstPoint Then
Dim g As Graphics = Me.CreateGraphics()
g.DrawLine(Pens.Black, FirstPoint, e.Location)
HaveFirstPoint = False
Else
FirstPoint = e.Location
HaveFirstPoint = True
End If
End Sub
采用窗體的MouseMove事件來(lái)獲取鼠標(biāo)坐標(biāo)是可以的。如下我在文本框中顯示當(dāng)前鼠標(biāo)的坐標(biāo)程序,鼠標(biāo)移動(dòng),坐標(biāo)自動(dòng)更新,只需要一個(gè)文本框控件,代碼如下:Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Text1.Text = "當(dāng)前鼠標(biāo)坐標(biāo)為,X:" X "Y:" Y
End Sub
控件中keyup屬性記錄鍵盤(pán)操作
比如:
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
Private Declare Function GetCursorPos Lib "user32" (ByRef lpPoint As POINTAPI) As Long '全屏坐標(biāo)聲明
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