Win32 CreateWindow API Function

How To Use the Windows API to Create Windows with Function Examples

Article explaining the Win32 Windows Programming API CreateWindow function and the parameters that it takes, as well as how to use it for parent and child window creation

The Windows API function used to create parent (top-level) and child windows is called CreateWindow. As with many Win32 API functions it has many parameters, most of which are entirely relevant to the task in hand, so it is important to understand each parameter with reference to the others.

This article will explain the common setting used to create:

  • Application Windows
  • Parent Windows
  • Child Controls

The first two uses are very similar, with the exception that the second can be used to create non-application floating windows that are children of a specific application parent window (such as floating toolbars).

The Win32 API CreateWindow Parameter List

The CreateWindow function returns a Win32 HWND (window handle), if creation has been successful. The parameters that it takes are:

  • LPCSTR lpClassName - the class name, a string;
  • LPCSTR lpWindowName - the window title text, or child control text;
  • DWORD dwStyle - the WS_ and other (BS_, SS_, LBS_, CBS_ etc.) styles;
  • int x, y, width, height - the x and y position, and width and height of the window;
  • HWND hParent - the parent window handle;
  • HMENU hMenu - the menu handle or NULL, or a Child ID;
  • HANDLE hInstance - the application instance handle;
  • LPVOID lpParam - window creation data, usually NULL.

The lpWindowName parameter is just a string that is used as the title bar text, or text for buttons, default text for edit boxes, and so on.

Common lpClassName Window Classes & WS_ Styles

When this function is called for the creation of an application window, then the class name is the same as the one that has previously been specified in the WNDCLASS window class structure registered with the RegisterClass function during the application startup.

For other uses, it is usually set to one of the standard control styles, which include:

  • "edit" - edit boxes
  • "listbox" - list boxes
  • "button" - buttons
  • "static" - static controls

The appearance of these can be further modified using class styles which are put in the dwStyle parameter. These begin BS_ for button styles, LBS_ for list boxes, ES_ for edit boxes, and so on. For child windows, some common WS_ styles combinations are:

  • WS_CHILD - it is a child window;
  • WS_VISIBLE - it is initially visible, without this it is not;
  • WS_TABSTOP - it is part of a tab group that the user can navigate with the tab key.

For application windows, it is common to use WS_OVERLAPPED, which creates a windows with a caption, system menu, and resizable frame.

Using the x, y, width and height Parameters

These are specified in pels, in screen co-ordinates for top level windows, and client co-ordinates for child windows, where the top left of the client area is at position 0,0 and where the bottom and right can be derived from a call to GetClientRect, supplied with a RECT structure.

The hMenu Parameter and Child IDs

The hMenu parameter is the handle to the menu identified in the WNDCLASS window class structure registered with the RegisterClass function. However, it can also be used as a numerical child identifier for use with functions like SendDlgItemMessage.

To accomplish this, it is necessary to cast it to a menu handle:

  • (HMENU) nChildID

The hWndParent Parent Handle and Styles

If the WS_CHILD style has been used in the function call, in the dwStyle parameter, then the hWndParent must contain a valid application, or top level - includes dialog boxes - window.

Otherwise, it should contain the value NULL.

The hInstance Application Instance Handle

This is the same value that is used in the parent (application) window creation, during the WinMain function, in the hInstance parameter of that function.

Guy Lecky-Thompson, Self Portrait

Guy Lecky-Thompson - Guy W. Lecky-Thompson is the author of several technical and non-technical books, and writer at large. He has written for Dr. Dobbs ...

rss
Advertisement
Leave a comment

NOTE: Because you are not a Suite101 member, your comment will be moderated before it is viewable.
Submit
What is 5+1?
Advertisement
Advertisement