Associating a Window Procedure with a Window Class

You associate a window procedure with a window class when registering the class. You must fill a WNDCLASS structure with information about the class, and the lpfnWndProc member must specify the address of the window procedure. To register the class, pass the address of WNDCLASS structure to the RegisterClass function. Once the window class is registered, the window procedure is automatically associated with each new window created with that class.

The following example shows how to associate the window procedure in the previous example with a window class.

int APIENTRY WinMain(

HINSTANCE hinstance, // handle of current instance

HINSTANCE hinstPrev, // handle of previous instance

LPSTR lpCmdLine, // address of command-line string

int nCmdShow) // show-window type

{

WNDCLASS wc;

// Register the main window class.

wc.style = CS_HREDRAW | CS_VREDRAW;

wc.lpfnWndProc = (WNDPROC) MainWndProc;

wc.cbClsExtra = 0;

wc.cbWndExtra = 0;

wc.hInstance = hinstance;

wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);

wc.hCursor = LoadCursor(NULL, IDC_ARROW);

wc.hbrBackground = GetStockObject(WHITE_BRUSH);

wc.lpszMenuName = "MainMenu";

wc.lpszClassName = "MainWindowClass";

if (!RegisterClass(&wc))

return FALSE;

//

// Process other messages.

//

}