首先需要一個(gè)判斷鼠標(biāo)位置的api,然后單位時(shí)間進(jìn)行判斷,如果某時(shí)間鼠標(biāo)的位置超出了窗體,則用另一個(gè)api來(lái)設(shè)置鼠標(biāo)位置
成都創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、成都網(wǎng)站制作、網(wǎng)站營(yíng)銷推廣、網(wǎng)站開(kāi)發(fā)設(shè)計(jì),對(duì)服務(wù)成都混凝土攪拌罐等多個(gè)行業(yè)擁有豐富的網(wǎng)站建設(shè)及推廣經(jīng)驗(yàn)。成都創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司成立于2013年,提供專業(yè)網(wǎng)站制作報(bào)價(jià)服務(wù),我們深知市場(chǎng)的競(jìng)爭(zhēng)激烈,認(rèn)真對(duì)待每位客戶,為客戶提供賞心悅目的作品。 與客戶共同發(fā)展進(jìn)步,是我們永遠(yuǎn)的責(zé)任!
這個(gè)都不難理解?詳情可查看vbgood的api手冊(cè)
幾乎找不到真正VB.NET的api,都是自己將VB的api改成VB.NET的api,我的理解是聲明中對(duì)應(yīng)輸入是H的地方保留Long,其他的Long改成Integer,既然VB.NET連MSDN都找不到較全的api說(shuō)明,不保證api的用法在VB.NET其他版本中適用。
給你個(gè)好玩的例子,包括鼠標(biāo)的隱藏(Ctrl_↓)、顯示(Ctrl_↑),移動(dòng)、點(diǎn)擊
在Form1上放上2個(gè)TextBox,其中TextBox1.Multiline = True,并且面積大些,2個(gè)Button,運(yùn)行后在TextBox1中點(diǎn)擊一下會(huì)記錄位置,移開(kāi)鼠標(biāo),點(diǎn)擊移動(dòng)或點(diǎn)擊Button,可模擬在TextBox1中的點(diǎn)擊
Public Class Form1
Public My_MousePosition As Point
'模擬鼠標(biāo)移動(dòng)、點(diǎn)擊
Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
'定義光標(biāo)的位置
Declare Function SetCursorPos Lib "user32" Alias "SetCursorPos" (ByVal x As Integer, ByVal y As Integer) As Integer
'鼠標(biāo)的隱藏
Declare Function ShowCursor Lib "user32" Alias "ShowCursor" (ByVal bShow As Integer) As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Multiline = True
Button1.Text = "點(diǎn)擊"
Button2.Text = "移動(dòng)"
Me.KeyPreview = True
End Sub
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.Control And e.KeyCode = Keys.Down Then ShowCursor(0)
If e.Control And e.KeyCode = Keys.Up Then ShowCursor(1)
End Sub
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
TextBox2.Text = MousePosition.X " " MousePosition.Y
End Sub
Private Sub TextBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseClick
My_MousePosition = MousePosition
TextBox1.Text = My_MousePosition.X " " My_MousePosition.Y vbCrLf
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SetCursorPos(My_MousePosition.X, My_MousePosition.Y)
mouse_event(H2, 0, 0, 0, 0)
mouse_event(H4, 0, 0, 0, 0)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
SetCursorPos(My_MousePosition.X, My_MousePosition.Y)
End Sub
End Class
其中
mouse_event Lib "user32" (ByVal dwFlags As Long
的dwFlags的代碼是
Const MOUSEEVENTF_LEFTDOWN = H2
Const MOUSEEVENTF_LEFTUP = H4
Const MOUSEEVENTF_MIDDLEDOWN = H20
Const MOUSEEVENTF_MIDDLEUP = H40
Const MOUSEEVENTF_MOVE = H1
Const MOUSEEVENTF_ABSOLUTE = H8000
Const MOUSEEVENTF_RIGHTDOWN = H8
Const MOUSEEVENTF_RIGHTUP = H10
SetCursorPos(xr,yr)
sleep (1000) '移動(dòng)鼠標(biāo)后加個(gè)延時(shí) 先加大一點(diǎn)的沒(méi)關(guān)系 ?測(cè)試有效后再縮短延時(shí)時(shí)間
其他行的sleep 也如此 再者你的leftdown 和 up 常量有先賦值吧
先查一下有沒(méi)有用相應(yīng)事件去修改鼠標(biāo)的狀態(tài),鼠標(biāo)移動(dòng)的事件或者鼠標(biāo)移動(dòng)到tabcontrol的事件!