Creating the Window

The window class defines general characteristics of a window, thus allowing the same window class to be used for creating many different windows. When you actually create a window by calling CreateWindow, you specify more detailed information about the window. Rather than using a data structure as RegisterClass does, the CreateWindow call requires all the information to be passed as parameters to the function. Here's the CreateWindow call in HELLOWIN.C:

hwnd = CreateWindow (szAppName, // window class name

"The Hello Program", // window caption

WS_OVERLAPPEDWINDOW, // window style

CW_USEDEFAULT, // initial x position

CW_USEDEFAULT, // initial y position

CW_USEDEFAULT, // initial x size

CW_USEDEFAULT, // initial y size

NULL, // parent window handle

NULL, // window menu handle

hInstance, // program instance handle

NULL) ; // creation parameters

The Microsoft C compiler recognizes the // symbol for single-line comments. The comments describe the parameters to the CreateWindow function.

Although you need to register a window class only for the first instance of a program, you must create a window separately for each instance. Each instance has its own window, and all the windows are based on the same window class.

The parameter marked ”window class name“ is szAppName, which contains the string ”HelloWin“—the name of the window class we just registered. This is how the window is associated with the window class.

The window created by this program is a normal overlapped window with a caption bar, a system menu box to the left of the caption bar, minimize and maximize icons to the right of the caption bar, and a thick window-sizing border. That's a standard style of windows, and it has the WINDOWS.H name WS_OVERLAPPEDWINDOW, which appears as the ”window style“ parameter. The ”window caption“ is the text that will appear in the caption bar.

The parameters marked ”initial x position“ and ”initial y position“ specify the initial position of the upper left corner of the window relative to the upper left corner of the screen. By using the identifier CW_USEDEFAULT for these parameters, we're indicating we want Windows to use the default position for an overlapped window. (CW_USEDEFAULT is defined as 0x8000.) By default, Windows positions successive overlapped windows at stepped horizontal and vertical offsets from the upper left corner of the display.

Similarly, the ”initial x size“ and ”initial y size“ parameters specify the width and height of the window. The CW_USEDEFAULT identifier again indicates that we want Windows to use a default size for the window. The default size extends to the right side of the display and above the icon area at the bottom of the screen.

The parameter marked ”parent window handle“ is set to NULL because this window has no parent window. (When a parent-child relationship exists between two windows, the child window always appears on the surface of its parent.) The ”window menu handle“ is also set to NULL because the window has no menu. The ”program instance handle“ is set to the instance handle passed to the program as a parameter of WinMain. Finally, a ”creation parameters“ pointer is set to NULL. You could use this pointer to access some data that you might later want to reference in the program.

The CreateWindow call returns a handle to the created window. This handle is saved in the variable hwnd, which is defined to be of type HWND (handle to a window). Every window in Windows has a handle. Your program uses the handle to refer to the window. Many Windows functions require hwnd as a parameter so that Windows knows to which window the function applies. If a program creates many windows, each has a different handle. The handle to a window is one of the most important handles a Windows program (pardon the expression) handles.