【问题标题】:How to get Window name in c++如何在 C++ 中获取窗口名称
【发布时间】:2015-10-14 21:20:45
【问题描述】:

我有一个文本框控件的窗口句柄 (HWND)。我有使用 EnumChildWindows 函数的窗口句柄。当我尝试使用 GetWindowText 获取控件的标题(文本)时,它总是返回 null。

其实是正确的,因为文本框的默认Text可能为null。

是否有任何函数来获取文本字段的名称,如 GetWindowText。我不想要文本我只想要名字。

【问题讨论】:

  • 如果文本框是空的,那么 GetWindowText 会给你一个空的。但是,我认为您可能对名称的概念感到困惑。你的意思是你可能想要附近标签的文本?
  • 控件没有名称,除非您指的是通过UI Automation 提供的UIA_NamePropertyId

标签: c++ winapi


【解决方案1】:

文本框必须归应用所有。如果您尝试从其他窗口的控件中获取文本,您将得到 0。但是可以通过 DLL 注入等来完成。

【讨论】:

  • 我不认为这是正确的。你认为 Spy++ 是如何工作的?
  • @Bathsheba 因为我在 15 年前就尝试过这样做并且知道其局限性。
  • If the target window is owned by the current process, GetWindowText causes a WM_GETTEXT message to be sent to the specified window or control. If the target window is owned by another process and has a caption, GetWindowText retrieves the window caption text. If the window does not have a caption, the return value is a null string. This behavior is by design. --- 见GetWindowText
  • 这是内容保护。在 Windows95/98 中没有这样的“问题”,但在 Windows NT (2000/XP) 中,您不能简单地获取其他文本框(编辑控件)的内容。在 Codeproject 中查找 DLL 注入黑客。请注意,它可能不适用于 x64 或 Windows 8。
  • GetWindowText() 可以跨进程正常工作 (and does not sent a WM_GETTEXT to do its thing)
【解决方案2】:

文本框本身不会包含标题。这将是与文本框相邻的标签。您可以使用 EnumChildWindows 找到该控件并提取标题。

【讨论】:

  • 我知道标签的标题更接近它。但是使用它你怎么知道下一个控件的名称???这里不可能吗? @Bathsheba
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-18
  • 1970-01-01
相关资源
最近更新 更多