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 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 placed in normal windows provide a variety of services, such as choosing commands, scrolling, and viewing and editing 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 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 list box and 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, create a window control by calling the CreateWindowsEx function. This creates a single control in a normal window. To create a control in a dialog box, use the dialog box template 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, called 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 for using 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. Windows.h includes Winuser.h.

    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 a control sending the 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 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 it creates. For example, the BUTTON class supports styles to create 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 parent window client area
int y y coordinate Specifies the upper-left corner y coordinate of the control relative to the upper-left corner of the parent window client area
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, hWnd
HMENU hMenu Child window identifier 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 creating a control

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

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

hwndTreeView = CreateWindow (
      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 treeview control window
      CW_USEDEFAULT,        // The height of the treeview control window
      hwnd,                 // Window handle to parent window
      (HMENU)IDC_TREEVIEW,  // The treeview control identifier
      hInst,                // The instance handle
      NULL);                // Specify NULL for this parameter when 
                            // creating 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 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 width Specifies the dialog box width
Height Dialog height Specifies the dialog box height
Option-statements Dialog box options Specifies one or more features of the dialog box. For example, use CAPTION to add a title to the dialog box or DISCARDABLE to remove the dialog box from memory when not in use. For a listing of option statements, see the DIALOG statement in the Windows CE API Reference.
Control-statements Controls associated with the dialog box Specifies one or more controls using the appropriate CONTROL statement

  1. 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 static text 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 CE Pad"
FONT 8, "MS Sans Serif"
BEGIN
  DEFPUSHBUTTON   "OK",IDOK,39,34,50,14
  CTEXT           "Microsoft Window CE",IDC_STATIC,7,7,118,8
  CTEXT           "CePad Sample Application",IDC_STATIC,7,19,118,8
END