Re-positioning a dialog box

Last Edited By Krjb Donovan
Last Updated: Mar 11, 2014 07:52 PM GMT

Question

Hi, Ralph

I am using Visual C++ 6.

I would like to have a dialog box open in the upper-center of the screen, since it is somewhat tall. At present, Visual C++ 6's default opens in the center of the screen, leaving the remainder of the dialog box below the screen. I have found 'WindowPlacement', but am unable to make it work.

Would you please show me a way to do this?

Thanks in advance for your help, Neil McCollum

Answer

Hello Neil,

You do not mention how you are creating this dialog. If, as most likely, it is from a resource template then you can adjust the DIALOGEX x,y values in the resource source (.rc) file, see the MSDN library information at:

   http://msdn.microsoft.com/en-us/library/aa381002%28v=VS.85%29.aspx

Note: If you are using the resource editor in Visual Studio then set the dialog properties through the IDE interface.

Note2: I tried on an existing test application with dialog resources in MSVC 9.0 (2008) (I not longer have version 6.0 installed - sorry) and leaving the x,y properties 0,0 seems to cause the dialog to be centred on the parent (both have to be zero) even with the Center property set to False. This is not mentioned in the description of the x and y DIALOGEX properties. Oh and negative values seem to work OK.

The problem with such static positioning is that if the parent window changes its position then so will the dialog - which may make parts of it be off the bottom - or possibly worse - off the top of the screen. If so one quick fix might be to make the dialog parent the desktop (screen) (i.e. a NULL parent HWND).

For best size and positioning control you may need to use a custom dialog procedure and move the dialog window in the dialog procedure (for example using MoveWindow in WM_INITDIALOG message handling code), as you will have the HWND of the window in the dialog procedure which is especially useful for modal dialogs. CreateDialog etc. return the HWND to you for a modeless dialog so you should be able to use that with MoveWindow from elsewhere.

If you are using the MFC then remember that a CDialog is a CWnd - so I suggest you implement an OnInitDialog override in your CDialog dialog sub-class and call MoveWindow to reposition the dialog window (note that MoveWindow uses screen coordinates), for example to move an About dialog to the centre top of the screen you could write something like so:

   class CAboutDlg : public CDialog
   {
   // ...

     virtual BOOL OnInitDialog();
   
   // ...
   };
   BOOL CAboutDlg::OnInitDialog()
   {
     CRect aboutRect;
     GetWindowRect( aboutRect );
     aboutRect.MoveToY(0);
     MoveWindow(aboutRect.left, aboutRect.top, aboutRect.Width(), aboutRect.Height());
     return CDialog::OnInitDialog();
   }

And ensure the Center positioning property of the About dialog resource is set to True.

I suggest that you check out the material on dialog boxes in the MSDN library. Try starting at:

   http://msdn.microsoft.com/en-us/library/ms632588%28v=VS.85%29.aspx

(for example the WM_REPOSITION message might be useful).

The MFC CDialog (MSVC 2008) material can be found at:

   http://msdn.microsoft.com/en-us/library/2d9cs4b6%28v=VS.90%29.aspx

You can of course also check out the MSDN library documentation (discs) that came with the MSVC 6.0 distribution to ensure more relevant material for your Visual Studio version.

Hope this helps.

Advertisement

©2017 eLuminary LLC. All rights reserved.