Chapter 12: Dialog-Based Windows Visual C++ and MFC Fundamentals
13.2.2..Other Dialog-Based Windows Messages........................................
WM_PAINT: After creating the dialog box, if you want to paint it or draw something on
it, such as changing its background color, use the WM_PAINT message. If you create a
dialog or form-based application, this message is automatically generated by the wizard.
If you add a dialog-based object to your application and need the OnPaint() event, you
would have to add it yourself
WM_CLOSE: As mentioned already, a regular dialog box, including a property sheet or
a wizard, is equipped with the system Close button on its title bar. This allows the
user to close it any time. As a programmer, one of your jobs is to control your
application, be able to predict “bad moves” from the user. From example, imagine you
create a dialog-based object without a Cancel button but with OK (like the most basic
message box). If the user clicks the system Close button , you may not know what the
user did. The WM_CLOSE message fires the OnClose() event when the user clicks the
system Close button. It is important to understand that WM_CLOSE is a message
and not a method, meaning it sends a message, it does not take an action. This implies
that the OnClose() event fires when the user makes an attempt to close the dialog but
before the dialog is actually closed.
You can use the OnClose() event to find out what has been done on the dialog prior to
the user’s attempt to closing it. You can also use it to deny closing the dialog, to warn the
user about something, or to do anything that you judge necessary. If you perform some
processing, such as validating some values, when the user attempts to close the dialog
box, if you still want to close the dialog, call the parent event handler with
CDialog::OnClose(). In fact, this line of code is added to the event if you generate it
using the wizard. If you want to conditionally close the dialog, you can write a
conditional statement that can check whether something is written in order to close it. If
you do not want to close the dialog box, do not call the parent CDialog::OnClose()
event.
WM_DESTROY: Once a dialog, in fact any (CWnd) window object, has been closed, it
must be destroy so the memory it was using can be reclaimed. If you want to do
something before the object is destroyed, use the WM_DESTROY to fire the
OnDestroy() event.
Practical Learning: Firing Windows Events
- Open the DialogMessages application created above
In the Class View tab, click CDynamicDlg to display it in the combo box of the
Properties window.
To access the WM_CLOSE messages, in the Properties window, click the Messages
button
- Click the WM_CLOSE item to display its combo box. Click the arrow of the combo
box and clickOnClose - To prevent the user from closing the dialog box using the system Close button,
implement the event as follows:
void CDynamicDlg::OnClose()
{