捕獲其他程序窗口句柄
要對(duì)其他程序進(jìn)行操作,首先要捕獲其他程序的窗口句柄。
‘查找標(biāo)題欄包含“inWindowText ” 的窗口,窗口標(biāo)題內(nèi)容不確定的可以使用,例如動(dòng)態(tài)變化標(biāo)題的窗口,如果標(biāo)題固定,直接用FindWindowEx()就可以了。
Public Function MyFindWindow(ByVal className As String, ByVal inWindowText As String) As Integer '查找標(biāo)題欄包含“inWindowText”的窗口
Dim hMyWindow As Integer
Dim sss As New String("", 256)
hMyWindow = FindWindowEx(0, 0, className, Nothing) '以desktop window為父,按照Z order查找第一個(gè)子窗口
'hMyButton = FindWindowEx(hMyWindow, 0&, Nothing, Nothing) '為遍歷所有類型窗口,將第三個(gè)參數(shù)設(shè)為“Nothing”
MyFindWindow = 0
While hMyWindow
GetWindowText(hMyWindow, sss, 256) '獲取窗口標(biāo)題
If InStr(sss, inWindowText) > 0 Then '進(jìn)行比較
MyFindWindow = hMyWindow
Exit Function ‘找到退出
End If
hMyWindow = FindWindowEx(0, hMyWindow, className, Nothing) '以desktop window為父,按照Z order查找在hMyWindow后的下一個(gè)子窗口
End While
End Function
4.2、單擊其他程序的菜單
'點(diǎn)擊級(jí)菜單的第個(gè)子項(xiàng)(從0算起)
Public Function ClickMenu(ByVal hMyWindow As Integer,ByVal nSubMenu As Integer, ByVal nMenuItemID As Integer) As Integer
Dim hMyWindow As Integer
Dim MyMenu As Integer
Dim MyGetMenuItemID As Integer
Dim nReturn As Integer
If hMyWindow = 0 Then
MsgBox("沒有找到相應(yīng)窗口!")
Else
MyMenu = GetMenu(hMyWindow)
MyMenu = GetSubMenu(MyMenu, nSubMenu)
MyGetMenuItemID = GetMenuItemID(MyMenu, nMenuItemID) '“按條件選擇設(shè)備”菜單
BringWindowToTop(hMyWindow)
nReturn = PostMessage(hMyWindow, WM_SYSCOMMAND, MyGetMenuItemID, 0)
nReturn = PostMessage(hMyWindow, WM_COMMAND, MyGetMenuItemID, 0)
End If
End Function
4.3、ListView相關(guān)操作見另文。
4.4.1、'讀COMBOBOX
Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
Dim hMyWindow, hMyButton As Integer
hMyWindow = FindWindow("WindowsForms10.Window.8.app3", "遠(yuǎn)程瀏覽")
hMyButton = FindWindowEx(hMyWindow, 0, "WindowsForms10.COMBOBOX.app3", Nothing) '獲取第二個(gè)WindowsForms10.COMBOBOX.app3窗口:ATMID 下拉選擇框
hMyButton = FindWindowEx(hMyWindow, hMyButton, "WindowsForms10.COMBOBOX.app3", Nothing) '獲取第二個(gè)WindowsForms10.COMBOBOX.app3窗口:ATMID 下拉選擇框
MsgBox(hMyButton)
Dim i As Integer = SendMessage(hMyButton, CB_GETCURSEL, 0, 0) '讀取當(dāng)前COMBOBOX索引值, 第三個(gè)參數(shù)為列表框的索引
MsgBox(i)
Dim k As Integer = SendMessage(hMyButton, CB_GETLBTEXTLEN, i, 0) ' 讀取索引位置文本長(zhǎng)度
MsgBox(k)
Dim ptr As IntPtr = Marshal.AllocHGlobal(256)
i = SendMessage(hMyButton, CB_GETLBTEXT, i, ptr) ' 讀取索引位置文本
MsgBox(i)
MsgBox(IntPtrToStr(ptr))
End Sub
4.4.2、設(shè)置其他程序的下拉框的選擇
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim hMyWindow, hMyButton As Integer
hMyWindow = FindWindow("WindowsForms10.Window.8.app3", "遠(yuǎn)程瀏覽")
hMyButton = FindWindowEx(hMyWindow, 0, "WindowsForms10.COMBOBOX.app3", Nothing) '獲取第二個(gè)WindowsForms10.COMBOBOX.app3窗口:ATMID 下拉選擇框
MsgBox(hMyButton)
hMyButton = FindWindowEx(hMyWindow, hMyButton, "WindowsForms10.COMBOBOX.app3", Nothing) '獲取第二個(gè)WindowsForms10.COMBOBOX.app3窗口:ATMID 下拉選擇框
MsgBox(hMyButton)
SendMessage(hMyButton, CB_SETCURSEL, 1, 0) '第三個(gè)參數(shù)為列表框的索引
End Sub
4.5.1、讀Edit
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
Dim processId As Integer
hwnd = FindWindow("WindowsForms10.Window.8.app3", "遠(yuǎn)程瀏覽")
hwnd = FindWindowEx(hwnd, 0, "WindowsForms10.COMBOBOX.app3", Nothing) '獲取第一個(gè)WindowsForms10.COMBOBOX.app3窗口:
hwnd = FindWindowEx(hwnd, 0, "Edit", Nothing)
Dim ptr As IntPtr = Marshal.AllocHGlobal(256)
SendMessage(hwnd, WM_GETTEXT, 255, ptr)
MsgBox(IntPtrToStr(ptr))
MsgBox(Marshal.PtrToStringAnsi(ptr))
End Sub
4.5.2、設(shè)置其他程序Edit框的值
'瀏覽路徑
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim hMyWindow, hMyButton, MyEditbox As Integer
Dim processId As Integer
hMyWindow = FindWindow("WindowsForms10.Window.8.app3", "遠(yuǎn)程瀏覽")
hMyButton = FindWindowEx(hMyWindow, 0, "WindowsForms10.COMBOBOX.app3", Nothing) '獲取第一個(gè)WindowsForms10.COMBOBOX.app3窗口:
MyEditbox = GetDlgItem(hMyButton, 1001) '獲
hMyButton = FindWindowEx(hMyButton, 0, "Edit", Nothing) '獲取“瀏覽路徑”輸入框
Dim str As String = "D:\Image\"
SendMessage(hMyButton, WM_SETTEXT, 0, StrToIntPtr(str))
End Sub
'將字符串存儲(chǔ)到IntPtr 指針中。
'Dim ptr As IntPtr = Marshal.StringToHGlobalAuto("1234")
'指針ptr 存放內(nèi)容格式為:1個(gè)數(shù)據(jù),1個(gè)結(jié)束符0,例如上面數(shù)據(jù),存放如下:49、0,50、0,51、0,52、0
'而很多時(shí),傳遞字符指針,要求數(shù)據(jù)是連續(xù)存放的,最后才是結(jié)束符0,例如SendMessage(hMyButton, WM_SETTEXT, 0, StrToIntPtr(str))。
Public Function StrToIntPtr(ByVal inStrText As String) As IntPtr
Dim nLen As Integer = Len(inStrText)
'分配內(nèi)存,以免內(nèi)存區(qū)域給其他進(jìn)程改寫。
Dim ptr As IntPtr = Marshal.AllocHGlobal(nLen + 1)
Dim mstr As Byte() = Encoding.Unicode.GetBytes(inStrText)
For i = 0 To nLen - 1
Marshal.WriteByte(ptr, i, mstr(i * 2))
Next
Marshal.WriteByte(ptr, nLen, 0)
Return ptr
End Function
4.6、單擊其他程序按鈕
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim hMyWindow, hMyButton As Integer
hMyWindow = FindWindow("WindowsForms10.Window.8.app3", "遠(yuǎn)程瀏覽")
hMyButton = FindWindowEx(hMyWindow, 0, "WindowsForms10.BUTTON.app3", "遠(yuǎn)程瀏覽(&B)") '“遠(yuǎn)程瀏覽(&B)”按鈕
MsgBox(hMyButton)
SendMessage(hMyButton, BM_CLICK, 0, 0)
End Sub
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。