📄 winmain.cpp
字号:
//#pragma once
#include <windows.h>
#include "resource.h"
#include ".\game.h"
TCHAR szAppClassName[] = TEXT("liuchuan"); // WNCLASS名
TCHAR szAppTitleName[] = TEXT("忘记喧嚣______________________Battle City Demo"); // 窗口标题名
#define WIN_WIDTH 648 // 窗口宽
#define WIN_HEIGHT 565 // 高
// 全局变量
HINSTANCE hInst; // 当前实例
HWND hWnd;
Game *game;
// 此代码模块中包含的函数的前向声明:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstace(HINSTANCE hInstance, int nCmdShow);
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
// 主函数
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
//消息循环
MSG msg;
MyRegisterClass(hInstance);
// 执行应用程序初始化:
if (!InitInstace(hInstance, nCmdShow))
{
return FALSE;
}
while (true)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (WM_QUIT == msg.message)
{
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// 接口调用
game->RunGame();
}
if (game != NULL)
{
delete game;
game = NULL;
}
return (int)msg.wParam; // 清空资源
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbClsExtra = 0; // 额外屏幕空间
wcex.cbSize = sizeof(WNDCLASSEX); // 消息窗口大小
wcex.cbWndExtra = 0; // 额外窗口
wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); // GetStockObject返回系统内部的笔、笔刷、调色板、字体
wcex.hCursor = LoadCursor(NULL, IDC_ARROW); // 光标
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1)); // 大图标
wcex.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1)); // 小图标
wcex.hInstance = hInstance; // 应用程序实例句柄
wcex.lpfnWndProc = WndProc; // 回调函数
wcex.lpszClassName = szAppClassName; // 窗口类名
wcex.lpszMenuName = NULL; // 菜单
wcex.style = CS_HREDRAW | CS_VREDRAW; // 窗口样式
//注册窗口类
return RegisterClassEx(&wcex); //返回szAppClassName
}
BOOL InitInstace(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // 用实例句柄初始化全局变量
hWnd = CreateWindowEx(0, szAppClassName, szAppTitleName, WS_OVERLAPPEDWINDOW,
0, 0, WIN_WIDTH, WIN_HEIGHT, NULL, NULL, hInstance, NULL);
if (!hWnd) // 高质量编程:使用指针前先确定不为NULL
{
return FALSE;
}
game = new Game(hWnd);
MoveWindow(hWnd, 50, 50, WIN_WIDTH, WIN_HEIGHT, true);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0); // 按ESC键关闭消息
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -