CreateWindowEx function (User32)
Namespace: User32
Header: 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
Name | Type | Description |
---|---|---|
dwExStyle | DWORD | Extended window style flags. See Extended Window Styles. |
lpClassName | LPCWSTR | Pointer to a null‑terminated string or a class atom created by RegisterClassEx . |
lpWindowName | LPCWSTR | Pointer to a null‑terminated string that specifies the window’s title. |
dwStyle | DWORD | Window style flags. See Window Styles. |
X | int | Initial horizontal position of the window. |
Y | int | Initial vertical position of the window. |
nWidth | int | Width of the window. |
nHeight | int | Height of the window. |
hWndParent | HWND | Handle to the parent or owner window. |
hMenu | HMENU | Handle to a menu, or child‑window identifier. |
hInstance | HINSTANCE | Handle to the instance of the module creating the window. |
lpParam | LPVOID | Pointer 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
andUpdateWindow
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