添加模式选择开始菜单并修复中文界面乱码

This commit is contained in:
2026-04-24 17:29:10 +08:00
parent 678285206b
commit 8f651e896e
4 changed files with 257 additions and 110 deletions
+59 -61
View File
@@ -1,4 +1,3 @@
// Tetris.cpp : 程序入口、窗口初始化与消息处理
#include "stdafx.h"
#include "Tetris.h"
@@ -10,19 +9,11 @@ HINSTANCE hInst;
TCHAR szTitle[MAX_LOADSTRING];
TCHAR 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);
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
/**
* @brief Win32 程序入口函数,负责完成应用启动初始化并进入消息循环。
*
* 该函数会加载窗口标题与窗口类名称资源,注册窗口类,创建主窗口,
* 然后加载快捷键资源并持续分发系统消息,直到程序退出。
*
* @return int 程序退出时返回消息循环中的退出码。
*/
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
@@ -46,7 +37,7 @@ int APIENTRY _tWinMain(HINSTANCE hInstance,
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_TETRIS, szWindowClass, MAX_LOADSTRING);
lstrcpy(szTitle, _T("俄罗斯方块"));
lstrcpy(szTitle, _T("\u4fc4\u7f57\u65af\u65b9\u5757"));
MyRegisterClass(hInstance);
if (!InitInstance(hInstance, nCmdShow))
@@ -69,15 +60,6 @@ int APIENTRY _tWinMain(HINSTANCE hInstance,
return (int)msg.wParam;
}
/**
* @brief 注册主窗口所需的窗口类信息。
*
* 该函数会设置窗口样式、消息处理函数、图标、光标、背景画刷、
* 菜单资源和窗口类名,最后调用系统 API 完成注册。
*
* @param hInstance 当前程序实例句柄。
* @return ATOM 注册成功时返回窗口类原子值,失败时返回 0。
*/
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
@@ -98,16 +80,6 @@ ATOM MyRegisterClass(HINSTANCE hInstance)
return RegisterClassEx(&wcex);
}
/**
* @brief 创建主窗口并显示到屏幕上。
*
* 该函数会先根据游戏区域尺寸计算实际窗口大小,再创建主窗口,
* 如果创建成功则显示并刷新窗口,供后续消息循环与绘制逻辑使用。
*
* @param hInstance 当前程序实例句柄。
* @param nCmdShow 窗口显示方式参数。
* @return BOOL 创建并显示成功返回 TRUE,否则返回 FALSE。
*/
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
RECT rect = { 0, 0, WINDOW_CLIENT_WIDTH, WINDOW_CLIENT_HEIGHT };
@@ -139,25 +111,13 @@ BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
return TRUE;
}
/**
* @brief 主窗口消息处理函数,负责响应菜单、绘制和退出等系统消息。
*
* 该函数除菜单和绘制外,还负责初始化游戏、处理定时下落、
* 响应键盘操作以及管理暂停、重开和游戏结束状态。
*
* @param hWnd 当前窗口句柄。
* @param message 当前接收到的消息类型。
* @param wParam 消息附带的参数 1。
* @param lParam 消息附带的参数 2。
* @return LRESULT 消息处理结果。
*/
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
srand((unsigned int)time(nullptr));
Restart();
ReturnToMainMenu();
SetTimer(hWnd, GAME_TIMER_ID, GAME_TIMER_INTERVAL, nullptr);
InvalidateRect(hWnd, nullptr, FALSE);
break;
@@ -179,7 +139,10 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
}
break;
case WM_TIMER:
if (wParam == GAME_TIMER_ID && !suspendFlag && !gameOverFlag)
if (wParam == GAME_TIMER_ID &&
currentScreen == SCREEN_PLAYING &&
!suspendFlag &&
!gameOverFlag)
{
if (CanMoveDown())
{
@@ -206,9 +169,56 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
InvalidateRect(hWnd, nullptr, FALSE);
break;
case WM_KEYDOWN:
if (currentScreen == SCREEN_MENU)
{
switch (wParam)
{
case VK_UP:
case VK_LEFT:
case 'W':
case 'A':
menuState.selectedIndex--;
if (menuState.selectedIndex < 0)
{
menuState.selectedIndex = menuState.optionCount - 1;
}
InvalidateRect(hWnd, nullptr, FALSE);
break;
case VK_DOWN:
case VK_RIGHT:
case 'S':
case 'D':
menuState.selectedIndex++;
if (menuState.selectedIndex >= menuState.optionCount)
{
menuState.selectedIndex = 0;
}
InvalidateRect(hWnd, nullptr, FALSE);
break;
case VK_RETURN:
case VK_SPACE:
StartGameWithMode(menuState.selectedIndex);
InvalidateRect(hWnd, nullptr, FALSE);
break;
case VK_ESCAPE:
DestroyWindow(hWnd);
break;
default:
break;
}
break;
}
if (wParam == 'M')
{
ReturnToMainMenu();
InvalidateRect(hWnd, nullptr, FALSE);
break;
}
if (wParam == 'R')
{
Restart();
StartGameWithMode(currentMode);
InvalidateRect(hWnd, nullptr, FALSE);
break;
}
@@ -329,18 +339,6 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
return 0;
}
/**
* @brief “关于”对话框的消息处理函数。
*
* 该函数用于初始化关于对话框,并处理用户点击“确定”或“取消”时的关闭操作。
* 对于未处理的对话框消息,返回 FALSE 交由系统继续处理。
*
* @param hDlg 对话框窗口句柄。
* @param message 当前接收到的对话框消息类型。
* @param wParam 消息附带的参数 1。
* @param lParam 消息附带的参数 2。
* @return INT_PTR 消息已处理返回 TRUE,否则返回 FALSE。
*/
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
@@ -348,7 +346,7 @@ INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
switch (message)
{
case WM_INITDIALOG:
SetWindowText(hDlg, _T("关于俄罗斯方块"));
SetWindowText(hDlg, _T("\u5173\u4e8e\u4fc4\u7f57\u65af\u65b9\u5757"));
return (INT_PTR)TRUE;
case WM_COMMAND: