-
VB.NET自动操作其他程序(3)--捕获窗口句柄、单击菜单、按钮、COMBOBOX、EditBox
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
出处:https://www.cnblogs.com/lefour/p/5464111.html
要对其他程序进行操作,首先要捕获其他程序的窗口句柄。
‘查找标题栏包含“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
出处:https://www.cnblogs.com/lefour/p/5464111.html
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式