Ever wondered how those cute little round dialog boxes are created. I too wondered for some time. But let me tell you it is easy, take a look:
Do this from OnInitDialog!
//Create a region object globally. CRgn m_EllipticRegion; //a rect object CRect crDialogRect; //get your dialog size this->GetClientRect(crDialogRect); //Now create the elliptic region from the client rect m_EllipticRegion.CreateEllipticRgn(0/*x*/, 0/*y*/, crDialogRect.Width()/*width*/, crDialogRect.Height() /*Height*/ ); //create a round dialog this->SetWindowRgn(m_EllipticRegion, TRUE);
See I told you it’s easy 🙂
Two functions to look up would be…
As the name suggests _AfxPreInitDialog gets called before OnInitDialog is called and _AfxPostInitDialog gets called after OnInitDialog.
One of the things that happens in _AfxPostInitDialog is centering of a dialog if user hasn’t changed position of the dialog in OnInitDialog, so how is this done? _AfxPreInitDialog stores co-ordinates of the dialog before OnInitDialog was called and if these co-ordinates hasn’t changed then _AfxPostInitDialog calls CenterWindow MFC function.
All of this is taking place in AfxCallWndProc! WM_INITDIALOG is handled as a special case. Also HandleInitDialog function calls PreInitDialog and OnInitDialog functions!
So have fun looking up these functions!
Here is a sample on how to do it…
CDialogTemplate dlgTemplate; // Load dialog template into memory dlgTemplate.Load( MAKEINTRESOURCE( IDD_DIALOG_ID )); // Change font of dialog dlgTempl.SetFont( _T( "Courier" ), 10 ); // Creates and displays a modal dialog from a template in memory CDialog dlg; dlg.InitModalIndirect( dlgTemplate.m_hTemplate, 0 ); dlg.DoModal();
What’s the use of this? Well you can change the font of dialog at runtime? Just call dlgTemplate.SetFont. This changes the font of entire dialog. Cool isn’t it!
To use CDialogTemplate include “afxpriv.h”. Microsoft(R) says that this file could be changed in near future, so use with care.