Chapter 12: Dialog-Based Windows Visual C++ and MFC Fundamentals
virtual LRESULT WindowProcedure(UINT message, WPARAM wParam, LPARAM lParam);
To process the messages, and because there can be so many of them, the window
procedure typically uses a switch control to list all necessary messages and process each
one in turn (some of the messages are those we reviewed in Lesson 4). After processing a
message, its case must return a value indicating whether the message was successfully
processed or not and how the message was processed.
Regardless of the number of messages you process, there will still be messages that you
did not deal with. It could be because they were not sent even though they are part of the
Windows control(s) used in your application. If you did not process some messages, you
should/must let the operating system take over and process it. This is done because the
operating system is aware of all messages and it has a default behavior or processing for
each one of them. Therefore, you should/must return a value for this to happen. The value
returned is typically placed in the default section of the switch condition and must simply
be a DefWindowProc() function. For a Win32 application, its syntax is:
LRESULT DefWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
For an MFC application, the syntax used for this function is:
virtual LRESULT DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam);
This function is returned to Windows, saying "There are messages I couldn't process. Do
what you want with them". The operating system would simply apply a default
processing to them. The values returned by the DefWindowProc() function should be the
same passed to the procedure.
The most basic message you can process is to make sure a user can close a window after
using it. This can be done with a function called PostQuitMessage(). Its syntax is:
VOID PostQuitMessage(int nExitCode);
This function takes one argument which is the value of the LPARAM argument. To
close a window, you can pass the argument as WM_QUIT.
The name of the window procedure must be assigned to the lpfnWndProc member
variable of the WNDCLASS variable.
Because we are using MFC to visually build our applications, you usually will not need
to define a window procedure to process Windows messages, unless the control you are
using is lacking a message that you find relevant. The Windows controls we will use in
this book have messages and notifications that apply the most regular behaviors they
need to offer. If you do not process all messages of a control, which will happen most of
the time, their default behavior are part of the AfxWndProc procedure. Therefore, you
can simply assign it to the lpfnWndProc member variable of your WNDCLASS variable:
CMainFrame::CMainFrame()
{
// Declare a window class variable
WNDCLASS WndCls;
WndCls.style = CS_VREDRAW | CS_HREDRAW;
WndCls.lpfnWndProc = AfxWndProc;
WndCls.cbClsExtra = 0;
WndCls.cbWndExtra = 0;