代码:
//回调函数
LRESULT CALLBACK WinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CREATE:
break;
case WM_DESTROY:
{
PostQuitMessage(0);
}
break;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
// 全局变量:
HINSTANCE hInst; // 当前实例
WCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
WCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
// 此代码模块中包含的函数的前向声明:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
HWND hWnd = 0;
MSG nMsg = {0};
//创建窗口类
WNDCLASS cs = { 0 };//窗口类
cs.cbClsExtra = 0;
cs.cbWndExtra = 0;
cs.hbrBackground = (HBRUSH)(COLOR_WINDOW + 2);
cs.hCursor = NULL;
cs.hIcon = NULL;
cs.hInstance = hInstance;
cs.lpfnWndProc = (WNDPROC)WinProc; //回调函数,相当于自定义窗体事件
cs.lpszClassName = L"Main";
cs.lpszMenuName = NULL;
cs.style = CS_VREDRAW | CS_HREDRAW;
//注册窗口类
RegisterClass(&cs);//实现注册类
//创建窗口
hWnd = CreateWindow(L"Main", L"Windows", WS_OVERLAPPEDWINDOW, 200, 200, 500, 500, NULL, NULL, hInstance, NULL);
//显示窗口(在界面上显示)
ShowWindow(hWnd, SW_SHOW);
//更新窗口
UpdateWindow(hWnd);
//消息循环
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
//销毁窗体、注销类
DestroyWindow(hWnd);
hWnd = NULL;
//UnregisterClass();
return 0;
}
运行效果如下:
创建流程:
1.创建窗口类
2.设置回调函数(用于监听事件或者相应对应事件)
3.注册窗口类
4.创建窗口
5.显示窗口
6.更新窗口
7.消息循环
8.销毁窗口,注销类