Registering Window Classes

A window class is supported by a window procedure. You can register a window class by using either the RegisterClassA or RegisterClassW function. If you register the window class by using RegisterClassA, the application tells the system that the windows of the created class expect messages with text or character parameters to use the ANSI character set; if you register it by using RegisterClassW, the application requests that the system pass text parameters of messages as Unicode. The IsWindowUnicode function enables applications to query the nature of each window.

The following example shows how to register an ANSI window class and a Unicode window class and how to write the window procedures for both cases. For the purposes of this example, all functions and structures are shown with the specific A or W data types. Using the techniques explained in Using Generic Data Types, you could alternatively write this example by using generic data types, so that it could be compiled as either ANSI or Unicode, depending on whether UNICODE is defined.

/* Register an ANSI window class. */

WNDCLASSA AnsiWndCls;

AnsiWndCls.style = CS_DBLCLKS | CS_PARENTDC;

AnsiWndCls.lpfnWndProc = (WNDPROC)AnsiWndProc;

AnsiWndCls.cbClsExtra = 0;

AnsiWndCls.cbWndExtra = 0;

AnsiWndCls.hInstance = hmodUser;

AnsiWndCls.hIcon = NULL;

AnsiWndCls.hCursor = LoadCursor(NULL, (LPTSTR)IDC_IBEAM);

AnsiWndCls.hbrBackground = NULL;

AnsiWndCls.lpszMenuName = NULL;

AnsiWndCls.lpszClassName = "TestAnsi";

RegisterClassA(&AnsiWndCls);

/* Register a Unicode window class. */

WNDCLASSW UnicodeWndCls;

UnicodeWndCls.style = CS_DBLCLKS | CS_PARENTDC;

UnicodeWndCls.lpfnWndProc = (WNDPROC)UniWndProc;

UnicodeWndCls.cbClsExtra = 0;

UnicodeWndCls.cbWndExtra = 0;

UnicodeWndCls.hInstance = hmodUser;

UnicodeWndCls.hIcon = NULL;

UnicodeWndCls.hCursor = LoadCursor(NULL,(LPTSTR)IDC_IBEAM);

UnicodeWndCls.hbrBackground = NULL;

UnicodeWndCls.lpszMenuName = NULL;

UnicodeWndCls.lpszClassName = L"TestUnicode";

RegisterClassW(&UnicodeWndCls);

The following example shows the difference between handling the WM_CHAR message in an ANSI window procedure and a Unicode window procedure.

/* ANSI Window Procedure */

LONG AnsiWndProc(HWND hwnd, UINT message,

WPARAM wParam, LPARAM lParam)

{

/* Dispatch the messages that can be received. */

switch (message) {

case WM_CHAR:

/*

* wParam - the value of the key

* lParam - (not used in this example)

*/

if (lstrcmpA("Q", (LPCSTR) wParam)) {

/*.

/*.

/*.

}

else {

./*

/*.

/*.

}

break;

./*

./* process other messages */

./*

}

}

/* Unicode Window Procedure */

LONG UniWndProc(HWND hwnd, UINT message,

WPARAM wParam, LPARAM lParam)

{

/* Dispatch the messages that can be received. */

switch (message) {

case WM_CHAR:

/*

* wParam - the value of the key

* lParam - (not used in this example)

*/

if (lstrcmpW(L"Q", (LPCWSTR) wParam)) {

/*.

/*.

/*.

}

else {

/*.

/* .

/* .

}

break;

/* .

/* process other messages */

/* .

}

}

All text in messages received by AnsiWndProc will be composed of ANSI characters, and all text in messages received by UniWndProc will be composed of Unicode characters.