如何在应用程序中始终在最上方创建无模式对话框 [英] How to make a modeless dialog always on top within the app

查看:97
本文介绍了如何在应用程序中始终在最上方创建无模式对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一个无模式的弹出对话框.我想使其仅在其所属的应用程序中位于最高位置,而不总是使其在桌面上位于最高位置.

I have a modeless popup dialog in my app. I want to make it topmost only within the app it belongs to, not always topmost on the desktop.

我尝试将第一个参数设置为wndTopMost,但是这样对话框将保留在桌面的顶部,这是非常糟糕的用户体验.

I have tried to set the first parameter to wndTopMost, but this way the dialog will remain on top on the desktop, which is very bad user experience.

我也尝试过使用带有SWP_NOZORDER参数的wndNoTopMost,但这只会在显示对话框时将其放在前面.如果我将另一个对话框/窗口移到对话框位置,则该对话框将埋在新的对话框/窗口下.

I have also tried wndNoTopMost with SWP_NOZORDER parameter, but this only put the dialog in front when its displayed. If I move another dialog/window to the dialog location, the dialog will be buried under the new dialog/window.

我目前在OnInitDialog()中使用SetWindowPos;

I am currently using the SetWindowPos in the OnInitDialog();

   SetWindowPos(&wndNoTopMost
    , myRect.left
    , myRect.top
    , myRect.Width()
    , myRect.Height()
    , SWP_NOACTIVATE | SWP_NOCOPYBITS | SWP_NOZORDER
    );

推荐答案

是的HWND_TOPMOST非常的糟糕用户体验,因此,我建议您不要采取简单的方法来尝试拨动此开关.

Yes HWND_TOPMOST is a very bad user experience, so I will commend you for not taking the easy way out and trying to flip this switch.

使对话框显示在其他窗口顶部的关键是设置其所有者. (请注意,在Win32中,所有者不同于父项窗口,尽管这些术语常常令人困惑.)所有对话框都有一个所有者,并且对话框始终位于其所有者之上.

The key to getting a dialog to appear on top of other windows is setting its owner. (Note that in Win32, an owner is distinct from a parent window, although the terms are often confusing.) All dialogs have an owner, and dialogs always stay on top of their owner.

因此,当您创建无模式对话框时(例如使用CreateDialog函数),请确保将应用程序主窗口的句柄指定为其所有者.令人困惑的是,该参数名为hwndParent,但实际上它指定了所有者窗口.

So when you create the modeless dialog (e.g. using the CreateDialog function), make sure to specify the handle to your application's main window as its owner. Confusingly, the parameter is named hwndParent, but it actually specifies the owner window.

这篇关于如何在应用程序中始终在最上方创建无模式对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆