Creating Windows

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.

Note In Windows CE, CreateWindow is implemented as a macro which 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 prototype.

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 window attributes in CreateWindowEx are described in the following table.

Window attributes 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 these with the WS_* flags.
Class name Every window belongs to a window class. Except for built-in classes, like controls, an application must register a window class before creating any windows of that class. The lpClassName parameter specifies the name of the class that is used as a template for creating the window.
Window name The window name, which is also called window text, is a text string that is 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 more information about windows styles, see the "Window Styles" section later in this chapter.
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 flags passed in.

If neither WS_POPUP nor WS_CHILD is specified, the hwndParent parameter may 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 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 only 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 lpParm parameter is passed as one of the message parameters. Although it can be any value, it is most commonly a pointer to a structure that contains data that may be needed 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, for example, TEXT("classname").

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.