天天看點

vb 模拟 click 視窗上的按鈕

最小化一樣也沒問題的, 你隻要先找到它的句柄即可, 再找子線程句柄, 下面以電腦為例  

'請先打開你的 電腦 再添加 Command1  

Option Explicit  

Private Declare FunXXction FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long  

Private Declare FunXXction 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 FunXXction SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long  

Const WM_SETTEXT = &HC  

Const BM_CLICK = &HF5  

Dim HwndVal&, ChildHwnd&, i&  

Private Sub Command1_Click()  

 HwndVal = FindWindow(vbNullString, "電腦")  

 If HwndVal = 0 Then MsgBox "電腦沒運作": Exit Sub  

 Print "電腦的句柄是: " & CStr(HwndVal)  

 SendMessage HwndVal, WM_SETTEXT, 0, ByVal "CBM666 的電腦"  

 '标記的下面兩行是直接給電腦的TextBox指派  

 'ChildHwnd = FindWindowEx(HwndVal, 0, "Edit", vbNullString)  

 'If ChildHwnd <> 0 Then SendMessage ChildHwnd, WM_SETTEXT, 0, ByVal "123456789"  

 For i = 1 To 10  

 If i = 10 Then  

 ChildHwnd = FindWindowEx(HwndVal, 0, "Button", "=")  

 If ChildHwnd <> 0 Then SendMessage ChildHwnd, BM_CLICK, ByVal 0&, ByVal 0&  

 Else  

 ChildHwnd = FindWindowEx(HwndVal, 0, "Button", CStr(i))  

 If i < 9 Then  

 ChildHwnd = FindWindowEx(HwndVal, 0, "Button", "+")  

 End If  

 Next i  

End Sub  

'*************************** 模拟鍵盤輸入  

'Dim Rtn&  

'Private Sub Command1_Click()  

' Rtn = Shell("Calc.EXE", 1) '執行小算盤。  

' AppActivate Rtn '啟動小算盤。  

' For i = 1 To 10 '設定回圈執行次數。  

' If i = 10 Then  

' SendKeys i & "=", True ' 按下按鍵給小算盤  

' Else  

' SendKeys i & "{+}", True ' 按下按鍵給小算盤  

' End If  

' Next i '将所有I 值相加。  

'End Sub

---------------------------------------------------------

點選例子

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 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 SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private ConstWM_CLOSE

Private ConstWM_QUIT

Private ConstWM_LBUTTONDOWN

Private ConstWM_LBUTTONUP

Private ConstBM_CLICK

Dim countNum As Single '下載下傳次數'

Dim DownloadUrlStr As String '下載下傳頁面'

Dim DownloadTimes As Integer '下載下傳間隔(秒)'

Private Sub Form_Load() '初始化'

TimerForStart.Enabled = False

TimeForClear.Enabled = False

TimeForClear.Interval = 1000

ConstWM_CLOSE = &H10

ConstWM_QUIT = &H12

WM_LBUTTONDOWN = &H201

WM_LBUTTONUP = &H202

ConstBM_CLICK = &HF5

End Sub

Private Sub SaveBtn_Click() '儲存'

DownloadUrlStr = UrlStr.Text

DownloadTimes = TimersStr.Text

TimerForStart.Interval = DownloadTimes * 1000

countNum = 0

TimersNow.Caption = 0

If DownloadUrlStr = "" Then

MsgBox "輸入網址"

Else

WebBrowser1.Navigate DownloadUrlStr

End If

Dim indexForHiapk As Long

Dim indexForAppChina As Long

indexForHiapk = InStr(UrlStr, "http://static.apk.hiapk.com")

indexForAppChina = InStr(UrlStr, "http://www.appchina.com")

If indexForHiapk <> 0 Then

StationName.Caption = "安卓網"

If indexForAppChina <> 0 Then

StationName.Caption = "應用彙"

Private Sub CountDownload() '累計下載下傳次數'

countNum = countNum + 1

TimersNow.Caption = countNum

Private Sub StartBtn_Click() '開始按鈕'

TimeForClear.Enabled = True

TimerForStart.Enabled = True

Private Sub StopBtn_Click() '停止按鈕'

Private Sub TimerForStart_Timer() '開始執行要做的事'

Call CountDownload

Call StartDownload

Private Sub TimeForClear_Timer() '清除下載下傳視窗'

Call SaveWinCon

Private Sub StartDownload() '開始下載下傳'

indexForHiapk = InStr(UrlStr, "***")

indexForAppChina = InStr(UrlStr, "***")

Call DownloadForHiapk

Call DownloadForAppChina

Private Sub DownloadForHiapk() 'hiapk'

Dim wb

Set wb = WebBrowser1.Document

For i = wb.All.length - 1 To 0 Step -1

If LCase(wb.All(i).tagname) = "a" Then

If wb.All(i).className = "d1" Then

wb.All(i).Click

Next

Private Sub DownloadForAppChina() 'appchina'

If wb.All(i).id = "dtpc" Then

'MsgBox "這裡"'

Private Sub SaveWinCon()

Dim Hwnd_SaveFile As Long

Dim Hwnd_ForBtn As Long

Dim RetVal As Long '有沒有關閉成功'

Dim RetValDown As Long '有沒有關閉成功'

Dim RetValUp As Long '有沒有關閉成功'

Hwnd_SaveFile = FindWindow(vbNullString, "檔案下載下傳")

Hwnd_ForBtn = FindWindowEx(Hwnd_SaveFile, 0, "Button", "取消")

SetForegroundWindow Hwnd_SaveFile

'關閉儲存視窗'

If Hwnd_ForBtn <> 0 Then

' RetVal = PostMessage(Hwnd_SaveFile, ConstWM_QUIT, 0&, 0&)'

' RetValDown = PostMessage(Hwnd_ForBtn, ConstWM_LBUTTONDOWN, 1&, 0&)'

' RetValUp = PostMessage(Hwnd_ForBtn, ConstWM_LBUTTONUP, 1&, 0&)'

'MsgBox RetValDown'

' MsgBox RetValUp'

SendMessage Hwnd_ForBtn, ConstBM_CLICK, ByVal 0&, ByVal 0&

If RetVal = 0 Then

'MsgBox "關閉出錯! "'

'MsgBox "成功關閉"'

' MsgBox "沒找到"'

 End If

vb 模拟 click 視窗上的按鈕

參考資料

<a href="http://www.cnblogs.com/del/archive/2008/02/28/1085432.html">http://www.cnblogs.com/del/archive/2008/02/28/1085432.html</a>

<a href="http://www.vbgood.com/api.html">http://www.vbgood.com/api.html</a>