使用win32 API获取对象的实例

时间:2015-08-17 17:52:48

标签: .net vb.net visual-studio winapi spy++

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

我知道如果我可以通过使用实例选择面板,这将允许我选择我想要的面板。我想选择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")

1 个答案:

答案 0 :(得分: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()调用{1}}参数:

lpszWindow
相关问题