创建Win32窗口程序
创建Win32窗口程序的步骤
- 创建Win32窗口程序
- 创建win32项目
- 包含win32窗口程序头文件
- WinMain()函数
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
return 0;
}
- 构造窗口
- 构造窗口指针
//WinMain()
WNDCLASSEX wndclass;
wndclass.cbClsExtra = 0;
wndclass.cbSize = sizeof(WNDCLASSEX);
wndclass.cbWndExtra = 0;
wndclass.hbrBackground = NULL;
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hIcon = NULL;
wndclass.hIconSm = NULL;
wndclass.hInstance = hInstance;
wndclass.lpfnWndProc = NULL;
wndclass.lpszClassName = L"GLWindow";
wndclass.lpszMenuName = NULL;
wndclass.style = CS_VREDRAW | CS_HREDRAW;
- 注册窗口
//WinMain()
ATOM atom = RegisterClassEx(&wndclass);
if (!atom)
{
MessageBox(NULL, L"Notice", L"Error", MB_OK);
return 0;
}
- 创建和显示窗口
先创建窗口,在显示窗口,最后为了窗口能显示正确,在刷新一下窗口。
//WinMain()
//窗口大小为800x600,这里的“窗口大小”包含窗口的外边框。而不是窗口内部,除去边框的视口大小
HWND hwnd = CreateWindowEx(NULL, L"GLWindow", L"OpenGL Window", WS_OVERLAPPEDWINDOW,
100, 100, 800, 600,
NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
- 用户操作
LRESULT CALLBACK GLWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CLOSE:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
//WinMain()
wndclass.lpfnWndProc = GLWindowProc;
- 让程序运行起来
//WinMain()
MSG msg;
while (true)
{
if (PeekMessage(&msg,NULL,NULL ,NULL,PM_REMOVE))
{
if (msg.message == WM_QUIT)
{
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
- 修改窗口中视口的大小为指定的大小。比如800x600,通过计算后,windowWidth和windowHeight为刚才指定的视口大小加上边框。然后通过createwindowex创建窗口。
RECT rect;
rect.left = 0;
rect.right = 800;
rect.top = 0;
rect.bottom = 600;
AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, NULL);
int windowWidth = rect.right - rect.left;
int windowHeight = rect.bottom - rect.top;