Working with Window Controls (Windows CE 5.0)

Windows CE 5.0
Send Feedback

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, Microsoft® 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 classDescription
BUTTONCreates a button control, which notifies the parent window when a user selects the button.
COMBOBOXCreates a combo box — a combination of a list box and an edit control — that enables a user to select and edit items.
EDITCreates an edit control, which lets a user view and edit text.
LISTBOXCreates a list box, which displays a list from which a user can select one or more items.
SCROLLBARCreates a scroll bar control, which enables a user to scroll horizontally and vertically within a window.
STATICCreates 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.
    ParameterDescriptionUse
    DWORD dwExStyleExtended window styleSpecifies an extended window style.
    LPCTSTR lpClassNameClass nameSpecifies a predefined window class. For example, to create a push button, specify BUTTON.
    LPCTSTR lpWindowNameWindow textSpecifies the text that you want to appear on the control.
    DWORD dwStyleWindow styleSpecifies 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 xx-coordinateSpecifies 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 yy-coordinateSpecifies 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 nWidthWidthSpecifies the control width.
    int nHeightHeightSpecifies the control height.
    HWND hWndParentParent windowSpecifies the handle to the parent window.
    HMENU hMenuIdentifier of the child windowSpecifies the control identifier.
    HINSTANCE hInstanceInstance handleSpecifies the application or module to be associated with the window.
    LPVOID lpParamExtra parametersSpecifies 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.
    ParameterDescriptionUse
    nameIDDialog box nameSpecifies a unique identifier for the dialog box.
    xx-coordinateSpecifies the x-coordinate of the upper-left corner of the dialog box.
    yy-coordinateSpecifies the y-coordinate of the upper-left corner of the dialog box.
    WidthDialog box widthSpecifies the width of the dialog box.
    HeightDialog box heightSpecifies the height of the dialog box.
    Option-statementsDialog box optionsSpecifies 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-statementsControls that are associated with the dialog boxSpecifies 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

See Also

GWES Application Development


Send Feedback on this topic to the authors

Feedback FAQs

© 2006 Microsoft Corporation. All rights reserved.


Show: