Every window is a member of a window class. A window class is a template for creating a window. When you write an application, you must register all window classes that are used to create windows. To simplify the process of creating windows, Windows CE includes several system-defined window classes; because Windows CE registers these classes automatically, you can immediately create windows with them.
You create windows with the CreateWindow or CreateWindowEx function. The only difference between these functions is that CreateWindowEx supports the extended style parameter, dwExStyle, while CreateWindow does not. These functions take a number of parameters that specify the attributes of the window being created. In Windows CE, CreateWindow is implemented as a macro that calls CreateWindowEx.
Windows CE includes additional functions, including DialogBox, CreateDialog, and MessageBox, for creating special-purpose windows such as dialog boxes and message boxes.
The CreateWindowEx function has the following syntax:
HWND
CreateWindowEx(
DWORD dwExStyle, //Extended style parameter
LPCWSTR lpClassName, //Class name parameter
LPCWSTR lpWindowName, //Window name parameter
DWORD dwStyle, //Style parameter
int X, //Horizontal parameter
int Y, //Vertical parameter
int nWidth, //Width parameter
int nHeight, //Height parameter
HWND hwndParent, //Parent parameter
HMENU hMenu, //Menu parameter
HINSTANCE hInstance, //Instance handle parameter
LPVOID lpParam); //Creation data parameter
The following table shows the window attributes in CreateWindowEx.
Window attribute |
Description |
Extended style | The dwExStyle parameter specifies one or more window extended styles. These have their own set of WS_EX_* flags and should not be confused with the WS_* flags. |
Class name | Every window belongs to a window class. Except for built-in classes, such as controls, an application must register a window class before creating any windows of that class. The lpClassName parameter specifies the name of the class used as a template for creating the window. |
Window name | The window name, also called window text, is a text string associated with a window. The lpWindowName parameter specifies the window text for the newly created window. Windows use this text in different ways. A main window, dialog box, or message box typically displays its window text in its title bar. A button control, edit control, or static control displays its window text within the rectangle occupied by the control. A list box, combo box, or scroll bar control does not display its window name. All windows have the text attribute, even if they do not display the text. |
Style | The dwStyle parameter specifies one or more window styles. A window style is a named constant that defines an aspect of the window's appearance and behavior. For example, a window with the WS_BORDER style has a border around it. Some window styles apply to all windows; others apply only to windows of specific window classes. For a list of message box styles supported by Windows CE, see Window and Control Styles. |
Horizontal and vertical coordinates | The x and y parameters specify the horizontal and vertical screen coordinates, respectively, of the window's upper-left corner. |
Width and height coordinates | The nWidth and nHeight parameters determine the width and height of the window in device units. |
Parent | The hwndParent parameter specifies the parent or the owner of a window, depending on the style of the flags passed in.
If neither the WS_POPUP nor WS_CHILD style is specified, the hwndParent parameter might be a valid window handle or NULL. If the parameter is NULL, the new window is a top-level window without a parent or owner. If it is non-NULL, the new window is created as a child of the specified parent window. If WS_CHILD is specified, the hwndParent parameter must be a valid window handle. The new window is created as a child of the parent window. If the WS_POPUP style is specified, the new window is created as a top-level window and the hwndParent parameter specifies the owner window. If WS_POPUP is specified and the parameter is NULL, the new window is partially owned by Windows CE. The WS_POPUP style overrides the WS_CHILD style. |
Menu | Windows CE does not support menu bars. In Windows CE, you can use the hMenu parameter to identify a child window. Otherwise, it must be NULL. |
Instance handle | The hInstance parameter identifies the handle of the specific instance of the application that creates the window. |
Creation data | Every window receives a WM_CREATE message when it is created. The lpParam parameter is passed as one of the message parameters. Although it can be any value, it is most commonly a pointer to a structure containing data that is required to create a particular window. |
The class name for a new window class has to be a Unicode string. You can use the TEXT macro to cast a string as Unicode, as in TEXT("classname").
The system does not automatically display the main window after creating it. Rather, the application's WinMain function uses the ShowWindow function to display the window. An application uses the SetWindowText function to change the window text after it creates the window. It uses the GetWindowTextLength and GetWindowText functions to retrieve the window text from a window.
For an example of how to call the CreateWindowEx function, see Creating a Sample Application.