MS CreateWindowEx function

CreateWindowEx function

Synopsis

HWND CreateWindowEx(
    DWORD     dwExStyle,
    LPCTSTR   lpClassName,
    LPCTSTR   lpWindowName,
    DWORD     dwStyle,
    int       x,
    int       y,
    int       nWidth,
    int       nHeight,
    HWND      hWndParent,
    HMENU     hMenu,
    HINSTANCE hInstance,
    LPVOID    lpParam
);

Parameters

dwExStyleExtended window style.
lpClassNamePointer to a null-terminated string or a class atom.
lpWindowNameWindow title.
dwStyleWindow style.
xInitial horizontal position.
yInitial vertical position.
nWidthInitial width.
nHeightInitial height.
hWndParentHandle to parent or owner window.
hMenuHandle to a menu, or child-window identifier.
hInstanceHandle to the instance of the module creating the window.
lpParamPointer to window-creation data.

Return value

If the function succeeds, the return value is a handle to the new window. If it fails, the return value is NULL. Call GetLastError for more information.

Remarks

Show tips

  • Use WS_OVERLAPPEDWINDOW for a typical top-level window.
  • Extended styles such as WS_EX_TOPMOST affect Z-order.
  • Pass a pointer to a CREATESTRUCT via lpParam to provide additional creation data.

Example

#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 WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nShowCmd)
{
    const wchar_t CLASS_NAME[]  = L"SampleWindowClass";

    WNDCLASS wc = {0};
    wc.lpfnWndProc   = WndProc;
    wc.hInstance     = hInst;
    wc.lpszClassName = CLASS_NAME;

    RegisterClass(&wc);

    HWND hWnd = CreateWindowEx(
        0,
        CLASS_NAME,
        L"CreateWindowEx Demo",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 500, 400,
        NULL, NULL, hInst, NULL);

    if (hWnd == NULL) return 0;

    ShowWindow(hWnd, nShowCmd);
    UpdateWindow(hWnd);

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