---页首---

Windows获取进程的全部顶级窗口

一般标准做法如下(也是官方推荐的方式):

EnumWindows
  → 拿到每个顶层窗口 HWND
  → GetWindowThreadPrcessId
  → 过滤当前进程ID

示例代码

BOOL CALLBACK
collectCurrentProcessWindowProc(HWND hwnd, LPARAM lParam)
{
  // Destination vector supplied by collectCurrentProcessWindows.
  auto* windows = reinterpret_cast<std::vector<HWND>*>(lParam);
  if (!windows || !IsWindow(hwnd)) {
    return TRUE;
  }

  // Process identifier associated with the enumerated top-level window.
  DWORD processId = 0;
  GetWindowThreadProcessId(hwnd, &processId);
  if (processId == GetCurrentProcessId()) {
    windows->push_back(hwnd);
  }
  return TRUE;
}

// Enumerates all top-level windows created by the current process.
std::vector<HWND>
collectCurrentProcessWindows()
{
  // Top-level windows found for the current process.
  std::vector<HWND> windows;
  EnumWindows(collectCurrentProcessWindowProc, reinterpret_cast<LPARAM>(&windows));
  return windows;
}
posted @ 2026-09-08 16:49  20190311  阅读(3)  评论(0)    收藏  举报
---页脚---