如何在应用程序中使无模式对话框始终位于顶部 [英] How to make a modeless dialog always on top within the app

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

问题描述

我的应用中有一个无模式的弹出对话框.我想让它只在它所属的应用程序中置顶,而不是总是在桌面上置顶.

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天全站免登陆