#include <Windows.h>
HINSTANCE hinstance;//应用程序实例句柄
LRESULT CALLBACK WindowsProc(HWND hwnd, UINT uint, WPARAM wparam, LPARAM lparam);
void CreateWin32(wchar_t WinClass);
int _stdcall WinMain(HINSTANCE hinstance,
HINSTANCE hperinstance, LPSTR cmdline, int cmdshow) {
CreateWin32(L"Win32");
MSG msg;
while (GetMessageW(&msg, NULL, 0, 0))
{
DispatchMessage(&msg);//分发消息窗口 窗口函数处理消息
TranslateMessage(&msg);//虚拟键消息转换字符串
}
return 0;
}
//窗口回调函数 UINT uint 里面存放全部窗口消息
LRESULT CALLBACK WindowsProc(HWND hwnd, UINT uint, WPARAM wparam, LPARAM lparam)
{
return DefWindowProc(hwnd, uint, wparam, lparam);//其他消息默认处理
}
void CreateWin32(wchar_t WinClass) {
WNDCLASSW Win32;
Win32.style = CS_HREDRAW | CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS;//窗口类风格
Win32.lpfnWndProc = WindowsProc;//窗口回调函数
Win32.hInstance = hinstance;
Win32.hIcon = NULL;
Win32.hCursor = LoadCursor(hinstance, IDC_HELP);
Win32.hbrBackground = CreateSolidBrush(RGB(255, 255, 0));
Win32.cbClsExtra = 0;
Win32.cbWndExtra = 0;
Win32.lpszClassName = WinClass;//窗口名字
Win32.lpszMenuName = NULL;
//注册窗口失败返回0
if (!RegisterClass(&Win32)) {
MessageBox(NULL, L"游戏初始化失败", L"梦幻西游", MB_OK);
return -1;
}
//创建窗口
HWND hwnd = CreateWindow(WinClass, L"第一个Windows程序", WS_OVERLAPPEDWINDOW, 100, 100, 640, 480, NULL, NULL, hinstance, NULL);
if (hwnd == NULL) {
MessageBox(NULL, L"游戏窗口创建失败", L"提示", MB_OK);
return -1;
}
//显示窗口
ShowWindow(hwnd, SW_SHOW);
//更新窗口
UpdateWindow(hwnd);
}
运行结果