windows C/C++/C# 强制窗口在顶部

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1874262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:34:00  来源:igfitidea点击:

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

c++cwindowswinapialways-on-top

提问by Jerry Coffin

Is the there a way to force anotherwindow to be on top? Notthe application's window, but anotherone, already running on the system. (Windows, C/C++/C#)

有没有办法强制另一个窗口在上面?不是应用程序的窗口,而是另一个已经在系统上运行的窗口。(Windows, C/C++/C#)

采纳答案by Brian R. Bondy

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

您可以使用 Win32 API BringWindowToTop。它需要一个 HWND。

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

您还可以使用 Win32 API SetWindowPos,它也允许您执行诸如使窗口成为顶级窗口之类的操作。

回答by Jerry Coffin

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

BringWindowToTopmoves the window to the top of the Z-order (for now) but does notmake it a topmost window.

BringWindowToTop将窗口的Z顺序的顶部(现在)但不能使之成为最顶层的窗口。

回答by RED SOFT ADAIR

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:

如果您想将应用程序窗口从后面(或最小化)到前面,BringWindowToTop() 不起作用。以下代码可靠地执行此技巧:

ShowWindow(hwnd, SW_MINIMIZE);
ShowWindow(hwnd, SW_RESTORE);

回答by camino

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 );
}