标签:
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
VB.NET自动操作其他程序(3)--捕获窗口句柄、单击菜单、按钮、COMBOBOX、EditBox
标签:
原文地址:http://www.cnblogs.com/lefour/p/5464111.html