Always-in-front dialogs

后端 未结 2 2026
忘掉有多难
忘掉有多难 2020-12-06 17:21

Is there a way to create a modeless dialog box in C++ MFC which always stays on top of the other windows in the application? I\'m thinking sort of like the Find dia

相关标签:
2条回答
  • 2020-12-06 17:58

    Note: This does not work under Windows 10, and may not work under Windows 7 and 8 (Reports vary).

    From Nish:

    ###Making your dialog stay on top

    Haven't you seen programs which have an "always-stay-on-top" option? Well the unbelievable thing is that you can make your dialog stay on top with just one line of code. Simply put the following line in your dialog class's OnInitDialog() function.

    SetWindowPos(&this->wndTopMost,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
    

    Basically what we are doing is to use the SetWindowPos function to change the Z-order of our dialog window. We make our dialog stay on top of all other windows by moving it to the top of the Z-order. Now even when you activate some other window, our window will stay on top. But I'd advise you to make sure you know exactly what you are doing when you do this, for it might annoy people if they can't get your window out of the way when they want to do that.

    As you mentioned in the comments, the above line makes the window sit on top of every application. You'll need to do

    SetWindowPos(&this->wndTop,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
    

    To make a window sit on top of only your application.

    0 讨论(0)
  • 2020-12-06 18:03

    The accepted answer fails for Windows 7 or above. (Or perhaps its me) But making the modeless dialog as popup instead of child solves it. It now gets positioned wrt main dialog window but you can write code to constrain anywhere. Using the no border or top bar makes it a simple window.

    0 讨论(0)
提交回复
热议问题