C/C++/C# Force window to be on top

五迷三道 提交于 2019-12-04 05:31:46

You can use the Win32 API BringWindowToTop. It takes an HWND.

You could also use the Win32 API SetWindowPos which also allows you to do things like make the window a top-level window.

SetWindowPos(that_window_handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);

BringWindowToTop moves the window to the top of the Z-order (for now) but does not make it a topmost window.

BringWindowToTop() has no effect if you want to bring a applications window from behind (or minimized) to front. The following code does this trick reliable:

ShowWindow(hwnd, SW_MINIMIZE);
ShowWindow(hwnd, SW_RESTORE);
BOOL CALLBACK EnumWindowsProc(HWND hWnd, long lParam) {
     wchar_t buff[255];

    if (IsWindowVisible(hWnd)) {
        GetWindowText(hWnd, (LPWSTR) buff, 254);
        //wprintf(L"%s\n", buff);
        wstring ws = buff;
        if (ws.find(L"Firefox") != ws.npos)
        {
            ::SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
        }
    }
    return TRUE;
}

int main(){
    BOOL enumeratingWindowsSucceeded = ::EnumWindows( EnumWindowsProc, NULL );
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!