Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Visual C++ and MFC Fundamentals Chapter 12: Dialog-Based Windows


If you are creating a Win32 application, to create a window, you can call either the
CreateWindow() or the CreateWindowEx() function. Their syntaxes are:

HWND CreateWindow(
LPCTSTR lpClassName,
LPCTSTR lpWindowName,
DWORD dwStyle,
int x,
int y,
int nWidth,
int nHeight,
HWND hWndParent,
HMENU hMenu,
HINSTANCE hInstance,
LPVOID lpParam
);

HWND CreateWindowEx(
DWORD dwExStyle,
LPCTSTR lpClassName,
LPCTSTR lpWindowName,
DWORD dwStyle,
int x,
int y,
int nWidth,
int nHeight,
HWND hWndParent,
HMENU hMenu,
HINSTANCE hInstance,
LPVOID lpParam
);

You can simply call this function and specify its arguments after you have registered the
window class. Here is an example:

INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASS WndCls;

...


RegisterClass(&WndCls);

CreateWindow(.. .);
}

If you are planning to use the window further in your application, you should retrieve the
result of the CreateWindow() or the CreateWindowEx() function, which is a handle to the
window that is being created. To do this, you can declare an HWND variable and
initialize it with the create function. This can be done as follows:

INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
HWND hWnd;
WNDCLASS WndCls;

...


RegisterClass(&WndCls);

hWnd = CreateWindow(.. .);
}
Free download pdf