8.2 Creating a Control

In Windows, you can create a control either from within a dialog box or from within the client area of any other type of window.

This chapter discusses using controls in a standard window. For information about how to create controls within a dialog box, see Chapter 9, “Dialog Boxes.”

To create a control in a window other than a dialog box, use the CreateWindow function. When creating the control, specify its window class, style, parent window, and identifier. If CreateWindow is successful, it returns a control handle that you can use in subsequent functions to move, size, paint, or destroy a window, or to direct a window to carry out tasks.

The following example shows how to create a push button control:

hButtonWnd = CreateWindow(
    "BUTTON",                              /* control class      */
    "OK",                                  /* button label       */
    BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE, /* control styles     */
    20,                                    /* x-coordinate       */
    40,                                    /* y-coordinate       */
    30,                                    /* width in pixels    */
    12,                                    /* height in pixels   */
    hWnd,                                  /* parent window      */
    IDOK,                                  /* control identifier */
    hinst,                                 /* instance handle    */
    NULL);

This example creates a push button that belongs to the BUTTON window class and has the BS_PUSHBUTTON style. The push button is a child window and will be visible when first created. The WS_CHILD style is required, but you need not specify the WS_VISIBLE style if you plan to use the ShowWindow function to show the push button. CreateWindow places the button at the coordinates (20,40) in the parent window's client area. The width and height are 30 and 12 pixels, respectively. The parent window is identified by the hWnd handle. The constant IDOK identifies the push button.