4.1、捕獲其他程序窗口句柄
要對其他程序進行操作,首先要捕獲其他程序的窗口句柄。
‘查找標題欄包含“inWindowText ” 的窗口,窗口標題內容不確定的可以使用,例如動態變化標題的窗口,如果標題固定,直接用FindWindowEx()就可以了。
Public Function MyFindWindow(ByVal className As String, ByVal inWindowText As String) As Integer '查找標題欄包含“inWindowText”的窗口
Dim hMyWindow As Integer
Dim sss As New String("", 256)
hMyWindow = FindWindowEx(0, 0, className, Nothing) '以desktop window為父,按照Z order查找第一個子窗口
'hMyButton = FindWindowEx(hMyWindow, 0&, Nothing, Nothing) '為遍歷所有類型窗口,將第三個參數設為“Nothing”
MyFindWindow = 0
While hMyWindow
GetWindowText(hMyWindow, sss, 256) '獲取窗口標題
If InStr(sss, inWindowText) > 0 Then '進行比較
MyFindWindow = hMyWindow
Exit Function ‘找到退出
End If
hMyWindow = FindWindowEx(0, hMyWindow, className, Nothing) '以desktop window為父,按照Z order查找在hMyWindow后的下一個子窗口
End While
End Function
4.2、單擊其他程序的菜單
'點擊級菜單的第個子項(從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("沒有找到相應窗口!")
Else
MyMenu = GetMenu(hMyWindow)
MyMenu = GetSubMenu(MyMenu, nSubMenu)
MyGetMenuItemID = GetMenuItemID(MyMenu, nMenuItemID) '“按條件選擇設備”菜單
BringWindowToTop(hMyWindow)
nReturn = PostMessage(hMyWindow, WM_SYSCOMMAND, MyGetMenuItemID, 0)
nReturn = PostMessage(hMyWindow, WM_COMMAND, MyGetMenuItemID, 0)
End If
End Function
4.3、ListView相關操作見另文。
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", "遠程瀏覽")
hMyButton = FindWindowEx(hMyWindow, 0, "WindowsForms10.COMBOBOX.app3", Nothing) '獲取第二個WindowsForms10.COMBOBOX.app3窗口:ATMID 下拉選擇框
hMyButton = FindWindowEx(hMyWindow, hMyButton, "WindowsForms10.COMBOBOX.app3", Nothing) '獲取第二個WindowsForms10.COMBOBOX.app3窗口:ATMID 下拉選擇框
MsgBox(hMyButton)
Dim i As Integer = SendMessage(hMyButton, CB_GETCURSEL, 0, 0) '讀取當前COMBOBOX索引值, 第三個參數為列表框的索引
MsgBox(i)
Dim k As Integer = SendMessage(hMyButton, CB_GETLBTEXTLEN, i, 0) ' 讀取索引位置文本長度
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、設置其他程序的下拉框的選擇
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", "遠程瀏覽")
hMyButton = FindWindowEx(hMyWindow, 0, "WindowsForms10.COMBOBOX.app3", Nothing) '獲取第二個WindowsForms10.COMBOBOX.app3窗口:ATMID 下拉選擇框
MsgBox(hMyButton)
hMyButton = FindWindowEx(hMyWindow, hMyButton, "WindowsForms10.COMBOBOX.app3", Nothing) '獲取第二個WindowsForms10.COMBOBOX.app3窗口:ATMID 下拉選擇框
MsgBox(hMyButton)
SendMessage(hMyButton, CB_SETCURSEL, 1, 0) '第三個參數為列表框的索引
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", "遠程瀏覽")
hwnd = FindWindowEx(hwnd, 0, "WindowsForms10.COMBOBOX.app3", Nothing) '獲取第一個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、設置其他程序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", "遠程瀏覽")
hMyButton = FindWindowEx(hMyWindow, 0, "WindowsForms10.COMBOBOX.app3", Nothing) '獲取第一個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
'將字符串存儲到IntPtr 指針中。
'Dim ptr As IntPtr = Marshal.StringToHGlobalAuto("1234")
'指針ptr 存放內容格式為:1個數據,1個結束符0,例如上面數據,存放如下:49、0,50、0,51、0,52、0
'而很多時,傳遞字符指針,要求數據是連續存放的,最后才是結束符0,例如SendMessage(hMyButton, WM_SETTEXT, 0, StrToIntPtr(str))。
Public Function StrToIntPtr(ByVal inStrText As String) As IntPtr
Dim nLen As Integer = Len(inStrText)
'分配內存,以免內存區域給其他進程改寫。
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", "遠程瀏覽")
hMyButton = FindWindowEx(hMyWindow, 0, "WindowsForms10.BUTTON.app3", "遠程瀏覽(&B)") '“遠程瀏覽(&B)”按鈕
MsgBox(hMyButton)
SendMessage(hMyButton, BM_CLICK, 0, 0)
End Sub
