vb 鼠标后台点击
如下图所示,我想点击程序中的一个按钮,达到鼠标后台自动点击计算器中的数字“5”。请给出源码。谢谢!这是我的代码,是错的。大侠们看看哪里错了?PrivateDeclareF...
如下图所示,我想点击程序中的一个按钮,达到鼠标后台自动点击计算器中的数字“5”。请给出源码。谢谢!
这是我的代码,是错的。 大侠们看看哪里错了?
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
ByVal dwProcessID As Long) As Long
Private Const PROCESS_TERMINATE = &H1
Private hProcess As Long
Private Const WM_LBUTTONDOWN = &H201 '左键按下
Private Const WM_LBUTTONUP = &H202 '左键弹起
Private Sub Command1_Click()
Dim xy As Long
Dim pid As Long
pid = Shell("C:\WINDOWS\System32\calc.exe", vbNormalFocus) '进程的PID
If pid = 0 Then
MsgBox "没有打开程序"
Else
hProcess = OpenProcess(PROCESS_TERMINATE, 0, pid) '返回一个进程句柄值,否则将返回NULL
Label1.Caption = "进程句柄值: " & hProcess & "进程的PID: " & pid
End If
SendClick hProcess, 73, 224
End Sub
Public Function SendClick(hwnd As Long, mX As Long, mY As Long)
Dim i As Long
i = PostMessage(hwnd, WM_LBUTTONDOWN, 0, (mX And &HFFFF) + (mY And &HFFFF) * &H10000)
i = PostMessage(hwnd, WM_LBUTTONUP, 0, (mX And &HFFFF) + (mY And &HFFFF) * &H10000)
End Function 展开
这是我的代码,是错的。 大侠们看看哪里错了?
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
ByVal dwProcessID As Long) As Long
Private Const PROCESS_TERMINATE = &H1
Private hProcess As Long
Private Const WM_LBUTTONDOWN = &H201 '左键按下
Private Const WM_LBUTTONUP = &H202 '左键弹起
Private Sub Command1_Click()
Dim xy As Long
Dim pid As Long
pid = Shell("C:\WINDOWS\System32\calc.exe", vbNormalFocus) '进程的PID
If pid = 0 Then
MsgBox "没有打开程序"
Else
hProcess = OpenProcess(PROCESS_TERMINATE, 0, pid) '返回一个进程句柄值,否则将返回NULL
Label1.Caption = "进程句柄值: " & hProcess & "进程的PID: " & pid
End If
SendClick hProcess, 73, 224
End Sub
Public Function SendClick(hwnd As Long, mX As Long, mY As Long)
Dim i As Long
i = PostMessage(hwnd, WM_LBUTTONDOWN, 0, (mX And &HFFFF) + (mY And &HFFFF) * &H10000)
i = PostMessage(hwnd, WM_LBUTTONUP, 0, (mX And &HFFFF) + (mY And &HFFFF) * &H10000)
End Function 展开
展开全部
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const WM_LBUTTONDOWN = &H201 '左键按下
Private Const WM_LBUTTONUP = &H202 '左键弹起
Private Sub Command1_Click()
Dim n As Long
n = FindWindowEx(0&, 0&, vbNullString, "计算器")
If n Then
n = FindWindowEx(n, 0&, "Button", "5")
If n Then
SendClick n
Else
MsgBox "未找到按钮5!"
End If
Else
MsgBox "未找到计算器窗口!"
End If
End Sub
Public Function SendClick(hwnd As Long)
PostMessage hwnd, WM_LBUTTONDOWN, 0&, 0&
PostMessage hwnd, WM_LBUTTONUP, 0&, 0&
End Function
Private Sub Form_Load()
Shell "C:\WINDOWS\System32\calc.exe", vbNormalFocus
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询