12

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 dialog in Visual Studio 2005 - where it stays on top, but you can still edit the underlying text.

(If it makes any difference, it's not MDI; it's a dialog-based app)

Adam Davis
  • 91,931
  • 60
  • 264
  • 330
Smashery
  • 57,848
  • 30
  • 97
  • 128

3 Answers3

17

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.

Adam Davis
  • 91,931
  • 60
  • 264
  • 330
  • 1
    This puts the window on top of absolutely everything (every other application) - is there a way to have it so that the window just stays on top of the application of which it is a part? – Smashery Feb 27 '09 at 03:57
  • 1
    Found it: instead of &this->wndTopMost, I used &this->wndTop – Smashery Feb 27 '09 at 04:04
  • Keep in mind the things that could happen if two applications both create topmost windows - it is best to try to avoid using this if at all possible - also users can be annoyed by this behaviour – 1800 INFORMATION Feb 27 '09 at 05:38
  • As I found this looking for a console-on-top and in the end the solution is amost the same, let me add a link to an ready-to-use example: http://stackoverflow.com/questions/27068650/c-console-application-always-on-top/27076663#27076663 – jave.web Nov 22 '14 at 11:17
  • 2
    As Sanjeev mentioned in his answer this fails. We have this problem under Windows 10. – AngelM1981 Jul 28 '20 at 14:36
1

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.

Sanjeev
  • 141
  • 2
0

It worked for me in Microsoft Windows Version 10.0.18362.476. Had to put SetWindowPos(&this->wndTopMost,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE); in OnInitDialog and make the dialog as a PopUp.

Toni
  • 1,555
  • 4
  • 15
  • 23