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:
The name of the window class
The window title
The window's style
The window position
The parent window handle
The menu handle
The instance handle
Thirty-two bits of additional data
The following example creates a window belonging to the GenericWClass window class:
/* Create a main window for this application instance. */
hWnd = CreateWindow(
1 "GenericWClass", /* See RegisterClass() call. */
2 "Generic Sample Application",/* Text for window title bar. */
3 WS_OVERLAPPEDWINDOW, /* Window style. */
4 CW_USEDEFAULT, /* Default horizontal position. */
CW_USEDEFAULT, /* Default vertical position. */
CW_USEDEFAULT, /* Default width. */
CW_USEDEFAULT, /* Default height. */
5 NULL, /* Overlapped windows have no parent. */
6 NULL, /* Use the window class menu. */
7 hInstance, /* This instance owns this window. */
8 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. In this example:
1 | 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. |
2 | The second parameter of CreateWindow specifies the window caption as Generic Sample Application. |
3 | The WS_OVERLAPPEDWINDOW style specifies that the window is a normal “overlapped” window. |
4 | 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 will place the window at a default position and give 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 you call the ShowWindow function.) |
5 | When you create a window, you can specify its parent (used with controls and child windows). Because an overlapped window does not have a parent, this parameter is set to NULL. |
6 | If you specify a menu when you create a window, the menu overrides the class menu (if any) for the window. Because this window will use the class menu, this parameter is set to NULL. |
7 | You must specify the instance of the application that is creating the window. Windows uses this instance to make sure that the window function supporting the window uses the data for this instance. |
8 | The last parameter is for additional data to be used by the window function when the window is created. This window takes no additional data, so the parameter is set to NULL. |
When CreateWindow successfully creates the window, it returns a handle to the new window. You can use the handle to carry out tasks on the window, such as showing it or updating its client area.
If CreateWindow cannot create the window, it returns NULL. Whenever you create a window, you should check for a NULL handle and respond appropriately. For example, in the WinMain function, if you cannot create your application's main window, you should terminate the application; that is, return control to Windows.