These are a few tips regarding dialog based applications. Its not that the same information cannot be found elsewhere. It can even be found on my site. But, I thought of putting them all together in one place. So that a beginner need not sift through endless pages to find a piece of information he wants.
Its not extensive and its not the complete set of instructions needed for developing dialog based apps. These are just a few answers to some simple but confusing questions.
To put a toolbar in your dialog
Put this in the
OnInitDialog function of your dialog class
ToolBar.Create(this, WS_CHILD | WS_VISIBLE | CBRS_TOP |
CBRS_TOOLTIPS |CBRS_FLYBY | CBRS_BORDER_BOTTOM);
AFX_IDW_CONTROLBAR_LAST,0, reposQuery, rcClientNow);
CPoint ptOffset(rcClientNow.left - rcClientStart.left,
CWnd* pwndChild = GetWindow(GW_CHILD);
pwndChild = pwndChild->GetNextWindow();
rcWindow.right += rcClientStart.Width() - rcClientNow.Width();
rcWindow.bottom += rcClientStart.Height() - rcClientNow.Height();
return TRUE; }
You have to make
CToolBar toolbar as a member variable in your dialog. Then create a toolbar in the resource and set it's ID as
IDR_TOOLBAR1. You can give it any name that you want ofcourse, but make sure to change the
LoadToolBar statement at the top. ( 3rd line of code ).
To change the shape of your dialog
Sometimes , you might want to change the shape of your dialog. For example,you might want it to look like an ellipse or any other shape. The following is a code snippet which allows you to make your dialog have the shape of a ellipse. For additional shapes, you can take a look at the MSDN for the following functions.
m_rgn.CreateEllipticRgn(0, 0, rcDialog.Width(), rcDialogHeight());
SetWindowRgn(GetSafeHwnd(), (HRGN) m_rgn, TRUE);
To move a captionless dialog
You might not want the caption bar to be present in the dialog at some point of time. You can do that easily by changing the dialog's properties in the resource editor. Right click on the dialog and select "Properties" from the menu that you see. In the "Styles" tab of the dialog that you see, you just have to uncheck the checkbox called "Title Bar". Thats easy. Isnt it ?
But then, you might want to move the dialog box too. Thats not possible without code as you dont have the caption bar which the user can use to drag the dialog. To accomplish just that, Add
WM_LBUTTONDOWN and the
WM_NCHITTEST handlers and use the following code.
void CYourDialog::OnLButtonDown(UINT nFlags, CPoint point)
PostMessage( WM_NCLBUTTONDOWN, HTCAPTION,
MAKELPARAM( point.x, point.y));
LONG CYourDialog::OnNcHitTest( UINT uParam, LONG lParam )
int xPos = LOWORD(lParam);
int yPos = HIWORD(lParam);
UINT nHitTest = CDialog::OnNcHitTest(CSize(xPos, yPos));
return (nHitTest == HTCLIENT) ? HTCAPTION : nHitTest;
And now, you are able to move the dialog by clicking anywhere in the client area and dragging it.
To fill the dialog box with a user specified colour
In the Header file of your Dialog,add a
CBrush variable ( member variable ). Assuming that you have declared it as such.
function of your dialog, add this code to create the brush.
message in your dialog and in the
function, add this code.
Or as an alternative to this, you can simply call
function in the
of the application if you want the dialog to have one standard colour. You can use
when you want all your child dialogs to have the same color or you can use the above said method to have different colors for different child dialogs that you are going to create.
To maximize a dialog by default
Put this line of code in the
OnInitDialog function of your dialog just before the return statement.
SendMessage(WM_SYSCOMMAND, SC_MAXIMIZE, NULL);
To prevent resizing of your dialog after a certain limit
Assuming that you have given the user a flexibility to resize the dialog.But you dont want him/her to resize after a particular size. To do that,handle the
WM_SIZING message like this. To add the handler, you can right click on your dialog's class name in the ClassView pane of your Workspace window. Select the Add Windows Message Handler... option and in the window that opens, select
WM_SIZING from the messages that you see. And click the "Add and Edit" button. Then, in the handler function, add this code.
void CYourDialog::OnSizing(UINT fwSide, LPRECT pRect)
if(pRect->right - pRect->left <=200)
pRect->right = pRect->left + 200;
if(pRect->bottom - pRect->top <=200)
pRect->bottom = pRect->top + 200;
To add a status bar in your dialog
CStatusBar member variable to your dialog. For example, you've declared it like this.
Assuming that i want only the status of the CAPS lock and the NUMS lock to be shown in the status bar,i've added the following variable.
static UINT BASED_CODE indicators =
function of the dialog, add the following code.
Thats all as of now folks. I'll keep updating this article as and when possible. All Luck and have a great time.