Working with Window Controls

A window control is a predefined child window that enables a user to make selections, carry out commands, and perform I/O tasks. You can place a window control within a dialog box or in the client area of a normal window. Controls that are placed within dialog boxes provide a user with the means to type text, select options, and direct a dialog box to complete its action. Controls that are placed in normal windows provide a variety of services, such as the ability to choose commands, scroll, and view and edit text.

Although you can create your own window controls, Windows CE has several predefined window classes that you can use to add a standard window control to your application. The following table shows predefined window classes that are supported by Windows CE.

Window class Description
BUTTON Creates a button control, which notifies the parent window when a user selects the button.
COMBOBOX Creates a combo box — a combination of a list box and an edit control — that enables a user to select and edit items.
EDIT Creates an edit control, which lets a user view and edit text.
LISTBOX Creates a list box, which displays a list from which a user can select one or more items.
SCROLLBAR Creates a scroll bar control, which enables a user to scroll horizontally and vertically within a window.
STATIC Creates a static control, which often acts as a label for another control; static controls can display both text and images, such as icons.

Because window controls are child windows, you can create a window control by calling the CreateWindowEx function. This creates a single control in a normal window. To create a control in a dialog box, use the template for a dialog box that is contained in your application resource file. By using a resource file, you can create multiple controls at the same time. For more information about resources and resource files, see Using Resources.

Most compilers are bundled with automated tools, known as resource editors, to create resources. Using a resource editor is probably the most accurate and efficient way to add a control to a dialog box. However, because resource editors vary, providing instruction in the use of a resource editor is beyond the scope of this text.

To use a window control, you must include either the Windows.h or the Winuser.h header file in your application. The Windows.h file includes the Winuser.h file.

To create a window control in a normal window

  1. Define an identifier for the control in the application header file.

    A control identifier is a value that uniquely identifies the control that sends a message. In Windows CE, control identifiers are valid only for child windows.

  2. Call the CreateWindowEx function and specify the following parameters.

    Parameter Description Use
    DWORD dwExStyle Extended window style Specifies an extended window style.
    LPCTSTR lpClassName Class name Specifies a predefined window class. For example, to create a push button, specify "BUTTON."
    LPCTSTR lpWindowName Window text Specifies the text that you want to appear on the control.
    DWORD dwStyle Window style Specifies a control style. Each predefined window class has a corresponding set of control styles that enables an application to vary the appearance and behavior of the controls that the application creates. For example, the BUTTON window class supports styles for creating a push button, radio button, check box, or group box.
    int x x-coordinate Specifies the x-coordinate of the upper-left corner of the control, relative to the upper-left corner of the client area of the parent window.
    int y y-coordinate Specifies the y-coordinate of the upper-left corner of the control, relative to the upper-left corner of the client area of the parent window.
    int nWidth Width Specifies the control width.
    int nHeight Height Specifies the control height.
    HWND hWndParent Parent window Specifies the handle to the parent window.
    HMENU hMenu Identifier of the child window Specifies the control identifier.
    HINSTANCE hInstance Instance handle Specifies the application or module to be associated with the window.
    LPVOID lpParam Extra parameters Specifies NULL when you create a control.

After you call CreateWindowEx, Windows CE handles all repainting tasks. It also destroys all controls upon termination of the application. The following code example shows how to add a control to a normal window by using CreateWindowEx.

INITCOMMONCONTROLSEX  iccex;  // INITCOMMONCONTROLSEX structure

DWORD dwStyle = WS_VISIBLE | WS_CHILD | TVS_HASLINES | TVS_LINESATROOT |
                TVS_HASBUTTONS;

// Initialize the INITCOMMONCONTROLSEX structure.
iccex.dwSize = sizeof (INITCOMMONCONTROLSEX);
iccex.dwICC = ICC_TREEVIEW_CLASSES;

// Register tree-view control classes from the common control 
// DLL.
InitCommonControlsEx (&iccex);


// Create the tree-view control.
g_hwndTreeView = CreateWindowEx (
    0, 
    WC_TREEVIEW,          // Class name
    TEXT ("Tree View"),   // Window name
    dwStyle,              // Window style
    0,                    // x-coordinate of the upper-left corner
    0,                    // y-coordinate of the upper-left corner
    CW_USEDEFAULT,        // The width of the tree-view control window
    CW_USEDEFAULT,        // The height of the tree-view control window
    hwnd,                 // Window handle to the parent window
    (HMENU) IDC_TREEVIEW, // The tree-view control identifier
    g_hInst,              // The instance handle
    NULL);                // Specify NULL for this parameter when you 
                          // create a control.

To create a control in a dialog box

  1. Define an identifier for each control in a header file.

  2. Define a dialog box in the application resource file by using the DIALOG statement. The statement defines the position and dimensions of the dialog box on the screen, as well as the dialog box style, and has the following parameters.

    Parameter Description Use
    nameID Dialog box name Specifies a unique identifier for the dialog box.
    x x-coordinate Specifies the x-coordinate of the upper-left corner of the dialog box.
    y y-coordinate Specifies the y-coordinate of the upper-left corner of the dialog box.
    Width Dialog box width Specifies the width of the dialog box.
    Height Dialog box height Specifies the height of the dialog box.
    Option-statements Dialog box options Specifies one or more features of the dialog box. For example, use the CAPTION statement to add a title to the dialog box or the DISCARDABLE statement to remove the dialog box from memory when the dialog box is not in use. For a listing of option statements, see the DIALOG statement in the Windows CE API Reference.
    Control-statements Controls that are associated with the dialog box Specifies one or more controls by using the appropriate CONTROL statement.
  3. Call either the DialogBox function or the CreateDialog function and specify the identifier or name of the dialog box template and the address of the dialog box procedure.

    DialogBox creates a modal dialog box and CreateDialog creates a modeless dialog box. For more information about creating dialog boxes, see Working with Windows and Messages.

The following code example shows how to create a push button and a static control in a dialog box.

#include <windows.h>

#define IDD_ABOUT               103
#define IDC_STATIC              -1

IDD_ABOUT DIALOG DISCARDABLE  0, 0, 132, 55
STYLE DS_MODALFRAME | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "About CePad"
FONT 8, "MS Sans Serif"
BEGIN
  DEFPUSHBUTTON   "OK",IDOK,39,34,50,14
  CTEXT           "Microsoft Windows CE",IDC_STATIC,7,7,118,8
  CTEXT           "CePad Sample Application",IDC_STATIC,7,19,118,8
END

 Last updated on Tuesday, July 13, 2004

© 1992-2000 Microsoft Corporation. All rights reserved.