C++.顶层窗口

#include <vector>
BOOL TraversalTopWnd(OUT std::vector<HWND> &vec)
{
    vec.clear();
    try
    {
        HWND hEnumWnd = GetTopWindow(0);
        while (hEnumWnd)
        {
            if (GetParent(hEnumWnd)== 0)
            {
                vec.push_back(hEnumWnd);
            }
            hEnumWnd = GetWindow(hEnumWnd, GW_HWNDNEXT);
        }
    }
    catch (...)
    {
        OutputDebugStringA(__FUNCTION__);
    }
    return vec.size() > 0;
}

int main()
{
    std::vector<HWND> vec;
    std::cout << TraversalTopWnd(vec) << endl;
    TCHAR sz[MAX_PATH] = { 0 };
    DWORD dwPid = 0;
    for each (HWND var in vec)
    {
        printf("窗口%08X", var);
        GetWindowThreadProcessId(var, &dwPid);
        printf(" 进程%08X|%d", dwPid, dwPid);
        ::GetWindowText(var, sz, MAX_PATH * sizeof(TCHAR) - sizeof(TCHAR));
        printf(" 标题%S", sz);
        memset(sz, 0, MAX_PATH * sizeof(TCHAR));
        ::GetClassName(var, sz, MAX_PATH * sizeof(TCHAR) - sizeof(TCHAR));      
        printf(" 类名%S", sz);
        printf("\r\n");
    }

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/dailycode/p/12460241.html
今日推荐