You can create a window by using the CreateWindow function. This function tells Windows to create a window that has the specified style and belongs to the specified class. CreateWindow takes several parameters:
Name of the window class
Window title
Window style
Window position
Parent-window handle
Menu handle
Instance handle
32 bits of additional data
The following example creates a window belonging to the GenericWClass window class (created in the sample code shown in Section 2.2.4.1, “Filling a WNDCLASS Structure”):
/* Create a main window for this application instance. */
hWnd = CreateWindow(
"GenericWClass", /* see RegisterClass call */
"Generic Sample Application", /* text for title bar */
WS_OVERLAPPEDWINDOW, /* window style */
CW_USEDEFAULT, /* default horz position */
CW_USEDEFAULT, /* default vert position */
CW_USEDEFAULT, /* default width */
CW_USEDEFAULT, /* default height */
NULL, /* overlapped windows have no parent */
NULL, /* use window class menu */
hinstCurrent, /* this instance owns this window */
NULL /* pointer not needed */
);
This example creates an overlapped window that has the style WS_OVERLAPPEDWINDOW and that belongs to the window class created by the code in the preceding example.
The first parameter of the CreateWindow function specifies the name of the window class Windows should use when creating the window. In this example, the window class name is GenericWClass. The second parameter of CreateWindow specifies the window caption as "Generic Sample Application".
The WS_OVERLAPPEDWINDOW style specifies that the window is a normal “overlapped” window, and the next four CreateWindow parameters specify the position and dimensions of the window. Since the CW_USEDEFAULT value is specified for the position, width, and height parameters, Windows places the window at a default position and gives it a default width and height. The default position and dimensions depend on the system and on how many other applications have been started. (Note that Windows does not display the window until the application calls the ShowWindow function.)
When you create a window, you can specify its parent window (used with control windows and child windows) in the hwndParent parameter. Because an overlapped window does not have a parent window, this parameter is set to NULL. If you specify a menu in the hmenu parameter when you create a window, the menu overrides the class menu (if any) for the window. Because this window is to use the class menu, this parameter is also set to NULL.
You must specify the instance of the application that is creating the window. Windows uses this instance to make sure that the window procedure supporting the window uses the data for this instance.
The last parameter, lpvParam, is for additional data to be used by the window procedure when the window is created. In this case, the window takes no additional data, so the parameter is set to NULL.
When CreateWindow successfully creates the window, it returns a handle of the new window. You can then use the handle to carry out tasks, such as showing the window or updating its client area.
If CreateWindow cannot create the window, it returns NULL. Whenever your application creates a window, it should check for a NULL handle and respond appropriately. For example, in the WinMain function, if the application's main window cannot be created, the application should be terminated—that is, WinMain should return control to Windows.