要通過CreateWindow函數(shù)創(chuàng)建窗體,需要按照以下步驟進行:
#include <Windows.h>
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
WNDCLASS wc = { 0 };
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND);
wc.lpszClassName = L"MyWindowClass";
RegisterClass(&wc);
HWND hWnd = CreateWindow(L"MyWindowClass", L"My Window",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
800, 600, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
MessageBox(NULL, L"Failed to create window", L"Error", MB_OK);
return 0;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
在上述代碼中,hInstance是應用程序?qū)嵗木浔?,nCmdShow是啟動窗口時指定的顯示方式。CreateWindow函數(shù)的參數(shù)依次是窗口類名、窗口標題、窗口樣式、初始位置、大小、父窗口句柄、菜單句柄、應用程序?qū)嵗浔?、附加?shù)據(jù)。