【问题标题】:Getting Instance of Object using win32 API使用 win32 API 获取对象实例
【发布时间】:2015-08-17 17:52:47
【问题描述】:

您好,我正在尝试处理外部程序中的控件。我可以获取主窗体句柄,然后是面板句柄,但无法确定我正在获取哪个面板,因为有 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")

【问题讨论】:

  • 不会是TPanel3。它将是一个类名称为TPanel 和窗口名称为Panel3 的控件。我猜。通过询问具有该类名称和窗口名称的控件,您会很容易找到它。
  • 控件的命名约定表明正在使用 VCL 框架。如果这是真的,VCL 用户通常会清除TPanel.Caption 属性,在这种情况下,将没有可检索的窗口名称。如果“Panel3”在 Spy++ 中不可见,则无法通过 Win32 API 访问。
  • 大家好,谢谢你们,这对我帮助很大:-)

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


【解决方案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-Order 顺序排列。

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

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

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

【讨论】:

  • 谢谢你,这让我明白了很多,也给了我更多要阅读的东西:-)
猜你喜欢
  • 2015-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多