展开全部
用API获取,
GetWindowThreadProcessId ihwnd, pid 得到进程ID
hProcess = OpenProcess(&H1F0FFF, False, pid) 获得进程句柄
例子:
Option Explicit
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Any) As Long
Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Long
Private Const WM_KEYDOWN = &H100
Private Const WM_KEYUP = &H101
Private Const WM_CHAR = &H102
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Const GW_CHILD = 5
Private Const GW_HWNDNEXT = 2
Dim WarHand As Long '魔兽ID
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) '通过API函数延时
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer '后台键盘
Private Declare Function Beep Lib "kernel32" (ByVal dwFreq As Long, ByVal dwDuration As Long) As Long
Public Function GetHandle(Title As String) As Long
Dim tmp As String
Dim hwnd As Long
Dim lngProcID As Long
Dim strTitle As String * 255 '//用来存储窗口的标题
'//取得桌面窗口
hwnd = GetDesktopWindow()
'//取得桌面窗口的第一个子窗口
hwnd = GetWindow(hwnd, GW_CHILD)
'//通过循环来枚举所有的窗口
Do While hwnd <> 0
'//取得下一个窗口的标题,并写入到列表框中
GetWindowText hwnd, strTitle, Len(strTitle)
If Left$(strTitle, 1) <> vbNullChar Then
tmp = Left$(strTitle, InStr(1, strTitle, vbNullChar))
If Left(tmp, Len(Title)) = Title Then
GetHandle = hwnd
End If
End If
'//调用GetWindow函数,来取得下一个窗口
hwnd = GetWindow(hwnd, GW_HWNDNEXT)
Loop
End Function
'//构造扫描码
Private Function MakeKeyLparam(ByVal VirtualKey As Long, ByVal flag As Long) As Long
Dim s As String
Dim Firstbyte As String 'lparam参数的24-31位
If flag = WM_KEYDOWN Then '如果是按下键
Firstbyte = "00"
Else
Firstbyte = "C0" '如果是释放键
End If
Dim Scancode As Long
'获得键的扫描码
Scancode = MapVirtualKey(VirtualKey, 0)
Dim Secondbyte As String 'lparam参数的16-23位,即虚拟键扫描码
Secondbyte = Right("00" & Hex(Scancode), 2)
s = Firstbyte & Secondbyte & "0001" '0001为lparam参数的0-15位,即发送次数和其它扩展信息
MakeKeyLparam = Val("&H" & s)
End Function
Public Function PostKey(wHandle As Long, KeyCode As Long) '//发送按键
PostMessage wHandle, WM_KEYDOWN, KeyCode, MakeKeyLparam(KeyCode, WM_KEYDOWN) '按下某键
PostMessage wHandle, WM_KEYUP, KeyCode, MakeKeyLparam(KeyCode, WM_KEYUP) '释放某键
End Function
Private Sub Form_Load()
If App.PrevInstance = True Then
MsgBox "本程序已经运行。 ", vbInformation
End
End If
Timer1.Enabled = True
Timer1.Interval = 100
End Sub
Private Sub TimePress_Timer()
PostKey WarHand, 38 '上,37左,40下,39左
'Sleep 10 '0.5S
PostKey WarHand, 38 '上,37左,40下,39左
'Sleep 10 '0.5S
PostKey WarHand, 37 '上,37左,40下,39左
'Sleep 10 '0.5S
PostKey WarHand, 40 '上,37左,40下,39左
'Sleep 10 '0.5S
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub Timer1_Timer()
'F10,运行上上左下
If GetAsyncKeyState(vbKeyF8) Then
WarHand = GetHandle("Warcraft III")
Debug.Print WarHand
If WarHand <= 0 Then
' Beep 2500, 1000
MsgBox "Warcraft III 运行了吗?", vbExclamation
TimePress.Enabled = False
' Beep 2500, 500
Exit Sub
Else
TimePress.Enabled = True
End If
End If
'F12,停止,上上左下
If GetAsyncKeyState(vbKeyF12) Then
TimePress.Enabled = False
End If
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询