vb.net中使用sendkeys無(wú)效,是因?yàn)榇翱跊](méi)有處于活動(dòng)狀態(tài)。
專注于為中小企業(yè)提供成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)鹽邊免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了成百上千家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
VB6的SendKeys有時(shí)有些問(wèn)題,比如打開任務(wù)管理器,以下代碼可以:
Sub main()
Dim WshShell As Object
Set WshShell = CreateObject("WScript.Shell")
WshShell.SendKeys "^+{ESC}"
End Sub
以下代碼卻不可以:
Sub main()
SendKeys "^+{ESC}"
End Sub
所以最好使用WScript.Shell中的SendKeys。
幾乎找不到真正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ì)記錄位置,移開鼠標(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
比如你要更換在你某個(gè)窗體上的光標(biāo)。
那么在這個(gè)窗體內(nèi)用這樣的代碼
Me.Cursor = System.Windows.Forms.Cursors.Cross
如果你要更換當(dāng)前系統(tǒng)顯示的光標(biāo)
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Cross
其中Cross代表十字光標(biāo),你也可以更改成其他的,直接查看Cursors中的各成員就可以找到了。
改變鼠標(biāo)位置或讓鼠標(biāo)消失。
Shift + 方向鍵 ,選擇 從當(dāng)前光標(biāo)至移動(dòng)后光標(biāo)所在位置的所有文本
Ctrl+Shift+左右方向鍵,選擇 從當(dāng)前光標(biāo)至下一(上一)單詞首(尾)
Shift + Alt + 方向鍵,選擇 由當(dāng)前光標(biāo)與 移動(dòng)后光標(biāo)所構(gòu)成的矩形的區(qū)域內(nèi)所有文本
Ctrl + 單擊鼠標(biāo)左鍵, 選擇 單擊位置的整個(gè)單詞
Shift + 單擊鼠標(biāo)左鍵,選擇 從當(dāng)前光標(biāo)至 鼠標(biāo)單擊位置區(qū)間的所有文本
Shift + Home(End), 選擇 從當(dāng)前光標(biāo)至 行首(尾)的所有文本