【问题标题】:Win32/Qt - Can one Enumerate all the toplevel windows belonging to the calling process?Win32/Qt - 可以枚举属于调用进程的所有顶级窗口吗?
【发布时间】:2013-11-14 00:38:04
【问题描述】:

我想检测所有顶级窗口,以便向它的后代发送消息。 我怎样才能做到这一点? 下面的代码好像没有检测到Qt顶层窗口,不知道为什么。

static BOOL CALLBACK EnumWindowsProc(_In_ HWND hwnd, _In_ LPARAM lParam) {
  WORD far wndProcessID;

  WORD currentProcessID = GetCurrentProcessId();
  std::vector<HWND> *topWindowList = (std::vector<HWND> *)lParam;
  if (topWindowList != NULL &&
      GetWindowThreadProcessId(hwnd, NULL) == currentProcessID) {
    printf("Found a top level window");
    fflush(stdout);
    topWindowList->push_back(hwnd);
  }
  return TRUE;
}

void enumAllDesktopChildWindow() {
  std::vector<HWND> topWindowList;
  EnumChildWindows(GetDesktopWindow(), EnumWindowsProc, LPARAM(&topWindowList));
}

【问题讨论】:

  • 你试过只用EnumWindows吗?
  • 在 Qt 应用程序中,您可以使用可移植的QGuiApplication::topLevelWindows()

标签: c++ qt winapi


【解决方案1】:

首先,GetWindowThreadProcessId API 返回一个线程 ID (TID) 而不是进程 ID (PID)

其次,如果你想枚举所有顶级Windows,你应该使用EnumWindows,而不是EnumChildWindows。如果您想要使用 EnumChildWindows,请将 NULL 作为第一个参数传递。

【讨论】:

    猜你喜欢
    • 2011-05-31
    • 1970-01-01
    • 1970-01-01
    • 2011-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-21
    相关资源
    最近更新 更多