要通过CreateWindow函数创建窗体,需要按照以下步骤进行:
#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是应用程序实例的句柄,nCmdShow是启动窗口时指定的显示方式。CreateWindow函数的参数依次是窗口类名、窗口标题、窗口样式、初始位置、大小、父窗口句柄、菜单句柄、应用程序实例句柄、附加数据。