Creating a Window

Other versions of this page are also available for the following:

Windows Mobile Not SupportedWindows Embedded CE Supported

8/28/2008

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 of the window classes that are used to create windows. To simplify the process of creating windows, Windows Embedded CE includes several system-defined window classes; because Windows Embedded CE registers these classes automatically, you can create windows with them immediately.

You create windows with the CreateWindow or CreateWindowEx function. The only difference between these functions is that CreateWindowEx supports the extended style parameter, dwExStyle, but CreateWindow does not. These functions take a number of parameters that specify the attributes of the window that is being created. In Windows Embedded CE, CreateWindow is implemented as a macro that calls CreateWindowEx.

Note

In multithreaded applications, the main thread must create the main application before creating threads for any child windows.

Windows Embedded CE includes additional functions for creating special-purpose windows such as dialog boxes and message boxes, such as DialogBox, CreateDialog, and MessageBox.

The CreateWindowEx function uses the following syntax.

HWND
CreateWindowEx(
   DWORD       dwExStyle,       //Extended style
   LPCWSTR     lpClassName,     //Class name
   LPCWSTR     lpWindowName,    //Window name
   DWORD       dwStyle,         //Style
   int         X,               //Horizontal position
   int         Y,               //Vertical position
   int         nWidth,          //Width
   int         nHeight,         //Height
   HWND        hwndParent,      //Parent window
   HMENU       hMenu,           //Menu
   HINSTANCE   hInstance,       //Instance handle
   LPVOID      lpParam);        //Creation data

The CreateWindow function does not include the dwExStyle parameter. The following table shows the window attributes of CreateWindowEx.

Window attribute Description

Extended style

The dwExStyle parameter specifies one or more window extended styles. These styles have their own set of WS_EX_* flags, which 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 that is used as a template for creating the window.

Window name

The window name, also known as 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 that is 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 that are supported by Windows Embedded 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 upper-left corner of the window.

Width and height coordinates

The nWidth and nHeight parameters determine the width and height, respectively, of the window, in device units.

Parent

The hwndParent parameter specifies the parent window or the owner of a window, depending on the style of the flags that are passed in.

If neither the WS_POPUP style nor the WS_CHILD style is specified, the hwndParent parameter might be either a valid window handle or NULL. If the parameter is NULL, the new window is a top-level window without a parent window or owner window. If the parameter is non-NULL, the new window is created as a child window of the specified parent window.

If the WS_CHILD style is specified, the hwndParent parameter must be a valid window handle. The new window is created as a child window 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 owned partially by Windows Embedded CE. The WS_POPUP style overrides the WS_CHILD style.

Menu

Windows Embedded CE does not support menu bars. In Windows Embedded 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 on 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"). You also can use the _T macro, as in __T("classname"). Another option is to declare the string as a Long string (L"classname"), which will make the string Unicode only.

The system does not display the main window automatically after the system creates the window. Instead, the WinMain function of the application 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.

See Also

Concepts

Working with Windows and Messages

Other Resources

GWES Application Development