Visual C++ and MFC Fundamentals Chapter 12: Dialog-Based Windows
The styles are combined using the bitwise operator OR (|). The CS_HREDRAW and the
CS_VREDRAW styles can be combined and assigned to the style member variable as
follows:
CMainFrame::CMainFrame()
{
// Declare a window class variable
WNDCLASS WndCls;
WndCls.style = CS_VREDRAW | CS_HREDRAW;
WndCls.cbClsExtra = 0;
WndCls.cbWndExtra = 0;
WndCls.hInstance = AfxGetInstanceHandle();
}
On a regular basis, while the application is running, its controls will receive instructions
from the user. This happens when the user clicks a mouse button or presses a keyboard
keys. These actions produce messages that must be sent to the operating system to do
something. Since there can be various messages for different reasons at any time, the
messages are processed in a global function pointer called a window procedure. To define
this behavior, you can create a pointer to function, also called a callback function. In this
case, the function must return a 32-bit value specially intended for window procedures. It
is called LRESULT. The name of the function is not important but it must carry some
required pieces of information that make a message relevant and complete. For a Win32
application, the message must provide the following four pieces of information:
?? The control that sent the message: Every object you will need in your program,
just like everything in the computer, must have a name. The operating system
needs this name to identify every object, for any reason. An object in Microsoft
Windows is identified as a Handle. For Win32 controls, the handle is called
HWND
?? The type of message: The object that sends a message must let the operating
system know what message it is sending. As we saw in Lesson 4 on Messages,
there are various types of messages for different circumstances. Nevertheless, to
make matters a little easier, we saw that each message is a constant positive
natural number (unsigned int) identified with a particular name. The message
identifier is passed as UINT
?? Accompanying items: Because there are so many types of messages, you must
provide two additional pieces of information to help process the message. These
two items depend on the type of message and could be different from one type
of message to another. The first accompanying item is a 32-bit type (unsigned
int) identified as WPARAM. The second accompanying item is a 32-bit type of
value (long) identified as LPARAM. Remember that these two can be different
things for different messages. For a Win32 application, the messages can be
carried in a function defined as follows:
LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT uMsg, WPARAM wParam,
LPARAM lParam);
For a Win32 application, the hWnd argument is required because it specifies what
Windows control sent the message. On an MFC application, the class that manages the
controls knows what control sent the message, which means that you do not have to
specify the window handle. Therefore, the window procedure would be declared as
follows, omitting the HWND object because it is specified by the window that is sending
the message: