C/C++/C# 强制窗口位于顶部

发布于 2024-08-14 12:18:06 字数 114 浏览 2 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

枕头说它不想醒 2024-08-21 12:18:06
SetWindowPos(that_window_handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);

BringWindowToTop 将窗口移动到 Z 顺序的顶部(暂时),但不会使其成为最顶层窗口。

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.

无需解释 2024-08-21 12:18:06

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

您还可以使用 Win32 API SetWindowPos还允许您执行一些操作,例如使窗口成为顶级窗口。

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.

可爱咩 2024-08-21 12:18:06

如果您想将应用程序窗口从后面(或最小化)移到前面,BringWindowToTop() 不起作用。下面的代码使这个技巧可靠:

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

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);
森林迷了鹿 2024-08-21 12:18:06
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 );
}
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 );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文