如何获取自己易语言程序窗口中某组件的句柄

如何获取自己易语言程序窗口中某组件的句柄,第1张

例如:高级表格1取窗口句柄 ()
这样就取到 高级表格的窗口句柄(任何组件都可看作是一种窗口,这个取到的是高级表格句柄,不是表格所在窗口的句柄)
----------------------------------------
调用格式: 〈整数型〉 对象.取窗口句柄 () - 系统核心支持库->窗口
英文名称:GetHWnd
取出本窗口或窗口组件的窗口句柄(即HWND)。本命令为高级对象成员命令。
*** 作系统需求: Windows

1可以使用Spy++找到你想监控的窗口的类名和标题名,通过这两个参数获取监控窗口的句柄
IntPtr hWnd = FindWindow("#32770", "文件另存为"); 02
2通过Spy++,展开窗口树上的各个节点直到你要的控件,并且参考节点的顺序使用FindWindowEx函数从窗口句柄开始一级一级的往里找到你要的控件的句柄,下面的代码行是要找到文件另存为对话框内文件名的输入框,并且通过发消息来自动填入内容。
IntPtr hChild;
hChild = FindWindowEx(hWnd,IntPtrZero, "DUIViewWndClassName",StringEmpty);
hChild = FindWindowEx(hChild, IntPtrZero, "DirectUIHWND", StringEmpty); hChild = FindWindowEx(hChild, IntPtrZero, "FloatNotifySink",StringEmpty); hChild = FindWindowEx(hChild, IntPtrZero, "ComboBox", StringEmpty); hChild = FindWindowEx(hChild, IntPtrZero, "Edit", StringEmpty);02
SendMessage(hChild, WM_SETTEXT, IntPtrZero, "c:\1txt");
以上就是监控其他程序panel的思路。

Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Const GW_HWNDFIRST = 0
Private Const GW_HWNDNEXT = 2
Private Const GW_CHILD = 5
Private Sub Command1_Click()
Call List1_Click
End Sub
Private Sub Command2_Click()
Dim hwnd As Long
Dim s As String, t As String
List1Clear
hwnd = GetDesktopWindow()
s = String(256, Chr(0))
GetClassName hwnd, s, 255
s = Replace(s, Chr(0), "")
t = String(256, Chr(0))
GetWindowText hwnd, t, 255
t = Replace(t, Chr(0), "")
List1AddItem "桌面:" & hwnd & " 类名:" & s & " 标题:" & t & vbCrLf
hwnd = GetWindow(hwnd, GW_CHILD Or GW_HWNDFIRST)
s = String(256, Chr(0))
GetClassName hwnd, s, 255
s = Replace(s, Chr(0), "")
t = String(256, Chr(0))
GetWindowText hwnd, t, 255
t = Replace(t, Chr(0), "")
List1AddItem "窗口:" & hwnd & " 类名:" & s & " 标题:" & t & vbCrLf
While hwnd <> 0
hwnd = GetWindow(hwnd, GW_HWNDNEXT)
s = String(256, Chr(0))
GetClassName hwnd, s, 255
s = Replace(s, Chr(0), "")
t = String(256, Chr(0))
GetWindowText hwnd, t, 255
t = Replace(t, Chr(0), "")
List1AddItem "窗口:" & hwnd & " 类名:" & s & "标题:" & t & vbCrLf
Wend
End Sub
Private Sub Form_Load()
Command1Caption = "获取所有控件"
Command2Caption = "遍历所有窗体"
End Sub
Private Sub EnumAllHandles(ByVal hwnd As Long)
Dim hn As Long
Dim firsthd As Long
Dim s As String, t As String
firsthd = GetWindow(hwnd, GW_CHILD)
firsthd = GetWindow(firsthd, GW_HWNDFIRST)
hn = firsthd
Do While hn <> 0
s = String(256, Chr(0))
GetClassName hn, s, 255
s = Replace(s, Chr(0), "")
t = String(256, Chr(0))
GetWindowText hn, t, 255
t = Replace(t, Chr(0), "")
Text1Text = Text1Text & "句柄:" & hn & " 父句柄:" & hwnd & " 类名:" & s & "标题:" & t & vbCrLf
TreeView1NodesAdd "k" & hwnd, tvwChild, "k" & hn, "句柄:" & hn & " 类名:" & s & "标题:" & t
EnumAllHandles hn

hn = GetWindow(hn, GW_HWNDNEXT)
If hn = firsthd Then Exit Do
Loop
End Sub
Private Sub List1_Click()
If List1ListIndex = -1 Then Exit Sub
TreeView1NodesClear
TreeView1NodesAdd , , "k" & Trim(Str(Val(Mid(List1Text, 4)))), List1Text
Text1Text = ""
EnumAllHandles Val(Mid(List1Text, 4))
TreeView1Nodes("k" & Trim(Str(Val(Mid(List1Text, 4)))))Expanded = True
End Sub
'添加两个按钮一个文本框一个列表框和一个树形图

FindWindow函数是获取顶层窗口的函数,不能获取子窗口。

FindWindowEx才是获取指定窗口的子窗口句柄(指针)的函数,要成功获取,需要以下几个条件:

第一参数必须指定明确的父窗口,如果为NULL,则是获取顶层窗口(即,父窗口为桌面);

被查找窗口(控件)必须和第二参数是明确的父子关系,且子窗口不是模态窗口(模态窗口有些特殊,一般不这样获取);

子窗口获取可能不是一次成功,如果类似子窗口很多,可能需要判断hwndChildAfter。

从你图中代码,无法判断需要获取的是否是顶层窗口,如果是,那么第一参数要写ClassName,而不是你的资源号,比如这样:

CWnd pwnd = FindWindow(NULL,_T("资源管理器"));

如果是希望获取子窗口,在MFC下不推荐使用这两个函数。

另外,报错是因为在UNICODE字符集的工程下,第一参数需要使用宽字节字符串,加上_T转换就可以了。

如果窗口是现有程序的,使用VS自带的spy++获取窗口的相关信息,然後使用WinAPI获取句柄,具体参考spy++的使用方法和winapi的使用
FindWindow(
lpClassName, {窗口的类名}
lpWindowName: PChar {窗口的标题}
): HWND; {返回窗口的句柄; 失败返回 0}
//FindWindowEx 比 FindWindow 多出两个句柄参数:
FindWindowEx(
Parent: HWND; {要查找子窗口的父窗口句柄}
Child: HWND; {子窗口句柄}
ClassName: PChar; {}
WindowName: PChar {}
): HWND;
如果窗口是你的程序动态生成的,使用如下语句
Form _FORM=new Form();
IntPtr _P = _FORMHandle;
_P就是你实例化的窗口句柄


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/13396312.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-28
下一篇 2023-07-28

发表评论

登录后才能评论

评论列表(0条)

保存