欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Windows获取窗口列表

程序员文章站 2022-03-19 15:01:16
...

ENV

  • Windows 10
  • Visual Studio 2017

Source

#include <Windows.h>
#include <Dwmapi.h>
#include <iostream>

#pragma comment(lib, "dwmapi.lib")

int main(int argc, char* argv[])
{
    auto hwnd = GetDesktopWindow();
    hwnd = GetWindow(hwnd, GW_CHILD);

    auto counter = 0;
    while (hwnd != NULL) {
        RECT rect;
        // https://*.com/questions/34583160/winapi-createwindow-function-creates-smaller-windows-than-set
        // GetWindowRect(hwnd, &rect);
        DwmGetWindowAttribute(hwnd, DWMWA_EXTENDED_FRAME_BOUNDS, &rect, sizeof(RECT));

        char name[255];
        GetWindowText(hwnd, name, 255);

        std::cout << "#" << counter++ << " " << name 
            << ": (" << rect.left << ", " << rect.top << ") - "
            << rect.right - rect.left << " x " << rect.bottom - rect.top << std::endl;

        hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
    }
    return 0;
}
相关标签: window