0

嗨,我正在尝试处理外部程序中的控件。我可以获取主窗体句柄,然后是面板句柄,但无法确定我正在获取哪个面板,因为有 4 个面板显示使用 spy++

我知道我是否可以通过使用实例来选择面板,这将允许我选择我想要的面板。我想选择TPanel3。

    Dim destination As IntPtr = FindWindow("TDeviceMainForm", "Gem")
    If destination Then MessageBox.Show("destination")
    Dim destControlpnl As IntPtr = FindWindowEx(destination, Nothing, "TPanel", Nothing)
    destControlpnl = FindWindowEx(destination, Nothing, "TPanel", Nothing)
    If destControlpnl Then MessageBox.Show("destControlpnl")
    Dim destControl As IntPtr = FindWindowEx(destControlpnl, Nothing, "TPanel", Nothing)
    If destControl Then MessageBox.Show("destControl")
4

1 回答 1

1

假设所有四个面板都是主窗口的直接子窗口,您可以使用FindWindowEx()它们来枚举它们,直到找到您感兴趣的面板。您可以使用该hwndChildAfter参数,例如:

Dim destination As IntPtr = FindWindow("TDeviceMainForm", "Gem")
Dim destControlpnl As IntPtr = FindWindowEx(destination, Nothing, "TPanel", Nothing)
destControlpnl = FindWindowEx(destination, destControlpnl, "TPanel", Nothing)
destControlpnl = FindWindowEx(destination, destControlpnl, "TPanel", Nothing)

这假设面板按主窗口子窗口的 Z 顺序顺序排列。

话虽如此,您提到的控件的命名约定表明 VCL 框架正在用于该应用程序的 UI。如果这是真的,VCL 用户通常会清除该TPanel.Caption属性,在这种情况下,搜索期间将没有可用的窗口名称。如果您在 Spy++ 中没有看到该字符串"Panel3"作为所需窗口名称,则TPanel该窗口名称将无法通过 Win32 API 访问。您必须找到一些其他标准来验证TPanel您真正想要的标准,例如寻找特定于那个的孙窗口TPanel

但是,如果您确实看到"Panel3"了窗口名称,则可以通过 Win32 API 访问它,这将FindWindowEx()通过使用其lpszWindow参数将您的搜索代码大大简化为单个调用:

Dim destination As IntPtr = FindWindow("TDeviceMainForm", "Gem")
Dim destControlpnl As IntPtr = FindWindowEx(destination, Nothing, "TPanel", "Panel3")
于 2015-08-17T18:03:07.127 回答