C++.进程顶层窗口1

安稳与你 提交于 2020-03-11 07:45:08
struct _PidHwnd
{
    DWORD m_dwPid;
    HWND m_hWnd;
};
BOOL CALLBACK _EnumWins(IN HWND h, IN LPARAM p)
{
    if (p)
    {
        _PidHwnd *pItem = (_PidHwnd*)p;
        DWORD dwPid = 0;
        GetWindowThreadProcessId(h, &dwPid); /*句柄所属进程*/
        if (dwPid == pItem->m_dwPid && IsWindowVisible(h) && 0 == GetParent(h)) /*顶层可见窗口*/
        {
            pItem->m_hWnd = h;
            return FALSE; /*成功匹配,退出枚举*/
        }
    }
    return TRUE; /*继续枚举*/
}
HWND GetTopWnd1(IN DWORD dwPid)
{
    _PidHwnd item = { 0 };
    item.m_dwPid = dwPid;
    EnumWindows(_EnumWins, (LPARAM)&item);
    return item.m_hWnd;
}

int main()
{
    STARTUPINFO si = { 0 };
    si.cb = sizeof(si);
    PROCESS_INFORMATION pi = { 0 };
    BOOL b = CreateProcess(_T("C:\\Using\\winmine.exe"), 0, 0, 0, 0, 0, 0, 0, &si, &pi);
    HWND h = 0;
    if (b)
    {
        for (int i = 0; i < 5 && !h; ++i)
        {
            h = GetTopWnd1(pi.dwProcessId);
            Sleep(1000);
        }
        printf("%d %08X \r\n", h, h);
    }

    return 0;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!