简体   繁体   中英

How to get HWNDs of all windows present on a given monitor?

GetDesktopWindow() returns only the primary desktop. I've also tried EnumDisplayMonitors() , but that doesn't return the desktop handle.

You can do this using the functions that are available, as follows:

Method 1 (less accurate for windows overlapping a monitor boundary)

  1. Enumerate all windows ( EnumWindows() )
  2. Use MonitorFromWindow() to ignore the ones that aren't on the monitor are on a monitor other than the one you care about.

Method 2 (more accurate)

  1. Get the bounds of the monitor you care about ( GetMonitorInfo() function)
  2. Enumerate all windows ( EnumWindows() )
  3. Check each window's outline rectangle ( GetWindowRect() for XP, otherwise DwmGetWindowAttribute(DWMWA_EXTENDED_FRAME_BOUNDS) ) to see if it intersects ( IntersectRect() ) with the monitor rectangle.

You can use EnumWindows function and then check that window belongs to the specific monitor:

BOOL CALLBACK EnumWindowsProc(_In_ HWND hwnd, _In_ LPARAM lParam) noexcept
{
    auto const h_monitor{::MonitorFromWindow(hwnd, MONITOR_DEFAULTTONULL)};
    if(h_monitor == g_h_target_minitor)
    {
        // Do something...
    }
    return TRUE;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM