CreateWindowEx function (User32)

Namespace: User32Header: Winuser.h

Syntax

HWND CreateWindowExW(
    DWORD     dwExStyle,
    LPCWSTR   lpClassName,
    LPCWSTR   lpWindowName,
    DWORD     dwStyle,
    int       X,
    int       Y,
    int       nWidth,
    int       nHeight,
    HWND      hWndParent,
    HMENU     hMenu,
    HINSTANCE hInstance,
    LPVOID    lpParam
);
Copy

The Unicode version (CreateWindowExW) is recommended; an ANSI version (CreateWindowExA) also exists.

Parameters

NameTypeDescription
dwExStyleDWORDExtended window style flags. See Extended Window Styles.
lpClassNameLPCWSTRPointer to a null‑terminated string or a class atom created by RegisterClassEx.
lpWindowNameLPCWSTRPointer to a null‑terminated string that specifies the window’s title.
dwStyleDWORDWindow style flags. See Window Styles.
XintInitial horizontal position of the window.
YintInitial vertical position of the window.
nWidthintWidth of the window.
nHeightintHeight of the window.
hWndParentHWNDHandle to the parent or owner window.
hMenuHMENUHandle to a menu, or child‑window identifier.
hInstanceHINSTANCEHandle to the instance of the module creating the window.
lpParamLPVOIDPointer to a value passed to the window via WM_CREATE.

Return Value

If the function succeeds, the return value is a handle to the new window (HWND). If it fails, the return value is NULL. To get extended error information, call GetLastError.

Remarks

  • The window class must be registered before calling CreateWindowEx.
  • Use AdjustWindowRectEx to calculate the required size of the window rectangle based on the desired client area.
  • After creation, the system sends a WM_CREATE message to the window procedure before the function returns.
  • To display the window, call ShowWindow and UpdateWindow after creation.

Examples

#include <windows.h>

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg) {
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
    }
    return DefWindowProc(hwnd, msg, wParam, lParam);
}

int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE, PWSTR, int nCmdShow)
{
    const wchar_t CLASS_NAME[] = L"SampleWindowClass";

    WNDCLASSEXW wc = {0};
    wc.cbSize        = sizeof(WNDCLASSEXW);
    wc.lpfnWndProc   = WndProc;
    wc.hInstance     = hInst;
    wc.lpszClassName = CLASS_NAME;
    wc.hCursor       = LoadCursor(nullptr, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);

    RegisterClassExW(&wc);

    HWND hwnd = CreateWindowExW(
        0,                              // Optional window styles.
        CLASS_NAME,                     // Window class
        L"CreateWindowEx Sample",       // Window text
        WS_OVERLAPPEDWINDOW,            // Window style

        // Size and position
        CW_USEDEFAULT, CW_USEDEFAULT, 500, 400,

        nullptr,       // Parent window    
        nullptr,       // Menu
        hInst,         // Instance handle
        nullptr);      // Additional application data

    if (hwnd == nullptr) return 0;

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    MSG msg = {0};
    while (GetMessage(&msg, nullptr, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return (int)msg.wParam;
}

Copy

See Also