如何将我的窗口投影为前台的活动窗口

发布于 2024-11-03 15:26:43 字数 222 浏览 4 评论 0原文

打开了一些窗口,并且从我的流程中,我想再启动一个窗口(高优先级),用户必须在其中输入凭据。我想将此凭据窗口置于前台,反过来,它可能会给用户带来良好的体验,因为他不需要手动选择凭据窗口。而且这是一次性启动,绝对不烦人,但强制用户输入凭据。

实现这一目标的最佳方法是什么?我不认为模拟鼠标点击是个好主意。有没有办法将消息发送到其余窗口以失去焦点?这样当我启动窗口时,它就会出现在前台。

There were some windows opened, And from my process I want to launch one more window(high priority) in which user has to enter credentials. And i want to put this credentials window to foreground, in turn it might give good experience to user as he need not manually select the credentials window. And this is one time launching, definitely not annoying but compulsory for user to enter creds.

what is the best way to achieve this? I don't think simulating mouse click is good idea. Is there a way to send msg to rest of windows to lose their focus? so that when i launch my window, it will come foreground.

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

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

发布评论

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

评论(4

这样的小城市 2024-11-10 15:26:43

这对我有用,无需使用 SetWindowPos 使窗口“始终位于顶部”:

HWND foreGround = GetForegroundWindow();
if (foreGround != m_hWnd)
{
    if (!::SetForegroundWindow(m_hWnd))
    {
        if (!foreGround)
        {
            foreGround = FindWindow(_T("Shell_TrayWnd"), NULL);
        }

        DWORD idForeground = GetWindowThreadProcessId(foreGround, NULL);
        DWORD idTarget = GetWindowThreadProcessId(m_hWnd, NULL);

        AttachThreadInput(idForeground, idTarget, TRUE);

        ::SetForegroundWindow(m_hWnd);

        AttachThreadInput(idForeground, idTarget, FALSE);
    }

    BringWindowToTop();
}

我在聊天应用程序中使用它,这样新的聊天消息就可以窃取焦点(显然是可选的,因为有些用户不这样做像这样。)

This works for me without having to use SetWindowPos to make the window 'always on top':

HWND foreGround = GetForegroundWindow();
if (foreGround != m_hWnd)
{
    if (!::SetForegroundWindow(m_hWnd))
    {
        if (!foreGround)
        {
            foreGround = FindWindow(_T("Shell_TrayWnd"), NULL);
        }

        DWORD idForeground = GetWindowThreadProcessId(foreGround, NULL);
        DWORD idTarget = GetWindowThreadProcessId(m_hWnd, NULL);

        AttachThreadInput(idForeground, idTarget, TRUE);

        ::SetForegroundWindow(m_hWnd);

        AttachThreadInput(idForeground, idTarget, FALSE);
    }

    BringWindowToTop();
}

I use this in a chat application so a new chat message can steal focus (optional obviously as some users don't like this.)

余罪 2024-11-10 15:26:43

您需要使用 SetWindowPos 调用 < code>wndTopMost 参数将您的窗口设置在所有其他窗口的顶部。

You need to call SetWindowPos with wndTopMost parameter to set your window at the top of all other windows.

月竹挽风 2024-11-10 15:26:43

此 UI 模式(输入强制值的窗口)通常称为模式对话框。在 MFC 中,您需要查找 CDialog::DoModal。即,您从CDialog 派生凭据窗口,然后调用继承的DoModal 方法。

模式对话框会阻止用户在应用程序的其他窗口中进行输入,因此会自动移动到被阻止的窗口之前。

This UI pattern (window to enter mandatory values) is commonly known as a modal dialog. In MFC, you'd look for CDialog::DoModal. I.e. you derive your credential window from CDialog, and then call the inherited DoModal method.

A modal dialog blocks user input in other windows in your app, and therefore is automatically moved before the blocked windows.

骷髅 2024-11-10 15:26:43

用户期望程序仅在应有的时候进入前台。这种技术使程序能够违反该契约,窃取前景并破坏设计。在不考虑其他选项的情况下,请勿应用此选项。例如,使用相关窗口之间的所有者窗口关系,并在需要时使用AllowSetForegroundWindow() 传递成为前台的能力。

Users expect programs to only come to the foreground when they are supposed to. This technique enables programs to violate that contract, stealing foreground and subverting the design. Do not apply this without considering other options. For example using the owner window relationship between related windows and passing the ability to become foreground, when needed, using AllowSetForegroundWindow().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文