You are here:

C++/Re-positioning a Dialog box

Advertisement


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.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2012 About.com, a part of The New York Times Company. All rights reserved.