SetBkMode

本文深入探讨了MFC框架中CDC类的SetBkMode和SetBkColor函数的作用,解释了在不同背景下如何使用这些函数来调整文本和图形的显示效果,包括如何保持背景颜色不变的同时输出特定颜色的文本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

编辑本段基本概述

原型:

一是 MFC里面封装的CDC类:
CDC::SetBkMode
int SetBkMode( int nBkMode );
API函数SetBkMode声明如下:
int SetBkMode(
HDC hdc, // handle of device context
int iBkMode // flag specifying background mode
);

参数:

hDC是当前设备的句柄。
mode是要设置的模式,其值可以为OPAQUE和TRANSPARENT。
输出的字符串时,发现存在背景色,这样的输出是破坏背景的。那需要使用什么方法来保持背景不变,而又能输出红色的字符串呢?比如按钮的文字颜色是黑色的,而背景是灰色的。这就需要使用SetBkMode函数来设置DrawText函数的输出方式,显示设备共有两种输出方式:OPAQUE和TRANSPARENT。OPAQUE的方式是用当前背景的画刷的颜色输出显示文字的背景,而TRANSPARENT是使用透明的输出,也就是文字的背景是不改变的。
对透明的理解:在 窗体或控件输出的字符串或图形是有背景色的。而 窗体或控件也是有背景色的。这两种背景色一般是不同的, 这样输出图形或字符串时,两种背景( 窗体或控件的背景色、符串或图形的背景色)重叠在一起了,影响美观,为了不让这两种背景重叠,可以设置背景模式为TRANSPARENT(透明的),即让输出的字符串或图形的背景色不可见。可以这样理解:把输出的字符串或图形的背景色看成是一物体, 窗体或控件看成另一物体,字符串或图形在窗体或控件前面挡住窗体或控件了(因为输出 的字符串或图形是画在窗体或控件上的,所以可以这么类比),但是透过字符串或图形能看到窗体或控件。即字符串或图形的背 景色所代表的物体是透明的。

编辑本段SetBkMode与SetBkColor理解

以前总不是太明白这几者之间的关系:SetBkMode, SetBkColor, wndclass.hbrBackground,
说实在的, 还是拿程序说话, 写个程序就能明白了……
我们以前一次的一个程序来说明:
/* 画笔练习程序, by netrookie, ChinaUnix.
* 请参考《windows程序设计》第五章:图形基础,画笔部分
* 此程序通过一个switch结构, 告诉我们怎样创建画笔和删除画笔。
*/
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCEhInstance,
HINSTANCE hPrevInstance,
PSTR szCmdLine,
int iCmdShow) {
static TCHAR szAppName[] = TEXT("MyPen");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = GetStockObject(GRAY_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if(!RegisterClass(&wndclass)) {
MessageBox(NULL, TEXT("Register failure..."),
szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName,
szAppName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg, NULL, 0, 0)) {
DispatchMessage(&msg);
TranslateMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd,
UINT message,
WPARAM wParam,
LPARAM lParam) {
static int cxClient, cyClient, i;
HDC hdc;
PAINTSTRUCT ps;
HPEN hPen;
LOGPEN logPen;
switch(message) {
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
i = 0;
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
SetBkColor(hdc, RGB(0, 255, 255));
// 注释运行和不注释它运行
// SetBkMode(hdc, TRANSPARENT);
switch(i) {
case 0:
hPen = CreatePen(PS_SOLID, 2, RGB(10, 10, 10));
break;
case 1:
hPen = CreatePen(PS_DASH, 1, RGB(20, 20, 20));
break;
case 2:
hPen = CreatePen(PS_DOT, 1, RGB(255, 30, 30));
break;
case 3:
hPen = CreatePen(PS_DASHDOT, 1, RGB(40, 40, 40));
break;
case 4:
hPen = CreatePen(PS_DASHDOTDOT, 1, RGB(50, 50, 50));
break;
case 5:
logPen.lopnStyle = PS_SOLID;
logPen.lopnColor = RGB(0, 60, 60);
/*
* 此处是非常有意思的,下面有说明
*/
logPen.lopnWidth.x = 5;
hPen = CreatePenIndirect(&logPen);
break;
default:
hPen = GetStockObject(BLACK_PEN);
break;
}
SelectObject(hdc, hPen);
MoveToEx(hdc, 0, (i + 1) * cyClient / 10, NULL);
LineTo(hdc, cxClient, (i + 1) * cyClient / 10);
TextOut(hdc, i* cxClient / 7, 4 * cyClient / 5, "windows", lstrlen("windows"));
if(i++ <= 5)
InvalidateRect(hwnd, NULL, FALSE);
EndPaint(hwnd, &ps);
// 删除GDI对象, 释放内存!!
DeleteObject(hPen);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
一运行终于明白了
#define _CRT_SECURE_NO_WARNINGS #pragma comment(linker, "/subsystem:windows /entry:mainCRTStartup") #include <graphics.h> // EasyX图形库头文件 #include <conio.h> #include <stdio.h> #include <stdlib.h> #include <windows.h> #include <time.h> // 宏定义 #define TRUE 1 #define FALSE 0 #define MAX_SIZE 7 // 最大拼图规模 #define N 9 // 拼图块种类数 #define ROWS 3 // 行数 #define COLS 3 // 列数 // 全局变量 int WINDOW_WIDTH = 1000; // 窗口宽度 int WINDOW_HEIGHT = 1000; // 窗口高度 int BUTTON_X = 300; // 按钮X坐标 int BUTTON_Y = 200; // 按钮Y坐标 int BUTTON_WIDTH = 200; // 按钮宽度 int BUTTON_HEIGHT = 50; // 按钮高度 int degrees[] = { 3,4,5,6,7 }; // 难度级别对应的拼图尺寸 char str[N][1000]; // 存储传统文化描述的数组 int key = 0; // 全局键值变量 ExMessage msg; // 消息结构体变量 // 新增计时相关全局变量 time_t start_time; // 游戏开始时间 int is_time_up = FALSE; // 超时标志 const int TIMELIMIT = 120; // 2分钟倒计时(秒) // 可选图片结构体 typedef struct { const char* name; // 图片名称 int num; // 图片编号 } Pictures; // 图片资源数组 Pictures picture[] = { {"京剧脸谱",1}, {"中国结",2}, {"剪纸",3}, {"书法",4}, {"国画",5}, {"陶瓷",6}, {"刺绣",7}, {"皮影",8}, {"灯笼",9} }; // 拼图游戏结构体 typedef struct { int size; //拼图规模 int matrix[MAX_SIZE][MAX_SIZE]; //拼图块版块 int emptyRow, emptyCol; //空白格位置 int moves; //移动步数 int picIndex; //选择的图片索引 int species[N]; //选择的图片 } Blocks; void ReadData();//数据读取函数(从文件读取传统文化描述) void WriteSingleSelectedData(int i);//显示单个传统文化描述 void WriteAllData();//显示所有传统文化描述 void GetImage(const char* filename, int lx, int ly);//加载并显示图片 void cut_image(const char* filename, int size);//切割图片函数(备用) int isClick(int x, int y);//按钮点击检测函数 void Button(int, int, const char* word);//绘制按钮函数 int ModeSelection();//模式选择界面 int Selectpicture();//图片选择界面 void Information(int key);//游戏说明界面 void InitPuzzle(Blocks* game, int size, int picIndex);//初始化拼图游戏 int CheckWin(Blocks* game);//检查是否胜利 int MoveTile(Blocks* game, int direction);//移动拼图块 void DrawPuzzle(Blocks* game);//绘制拼图界面 void ShowWinScreen(Blocks* game);//显示胜利界面 void ShowTimeOverScreen();//显示超时界面 void PuzzlePage(int degree, int picIndex);//拼图游戏主循环 void Menu();//菜单 int main() { // 初始化随机数种子 srand((unsigned)time(NULL)); // 读取传统文化描述数据 ReadData(); // 进入主菜单循环 while (TRUE) { Menu(); } closegraph();//关闭图形窗口 return 0; } //数据读取函数(从文件读取传统文化描述) void ReadData() { FILE* file = fopen("data.txt", "r"); if (file == NULL) { // 文件打开失败时使用默认文本 for (int i = 0; i < N; i++) { sprintf(str[i], "元素 %d 描述加载失败", i + 1); } return; } // 逐行读取描述 for (int i = 0; i < N; i++) { if (fgets(str[i], sizeof(str[i]), file) != NULL) { // 去除换行符 size_t len = strlen(str[i]); if (len > 0 && str[i][len - 1] == '\n') { str[i][len - 1] = '\0'; } } } fclose(file); } //显示单个传统文化描述 void WriteSingleSelectedData(int i) { outtextxy(0, 0, str[i]); } //显示所有传统文化描述 void WriteAllData() { for (int i = 0; i < N; i++) { outtextxy(i, i * 5, str[i]); } } //加载并显示图片 void GetImage(const char* filename, int lx, int ly) { IMAGE image; loadimage(&image, filename); putimage(lx, ly, &image); } //切割图片函数 void cut_image(const char* filename, int size) { IMAGE img; loadimage(&img, filename); // 加载图片 if (img.getwidth() == 0 || img.getheight() == 0) { // 加载失败 outtextxy(100, 100, "图片加载失败!"); return; } // 获取图片宽度和高度 int width = img.getwidth(); int height = img.getheight(); //目标宽高 int targetWidth = 200; int targetHeight = 200; // 定义切割块的大小 const int blockWidth = width / size; const int blockHeight = height / size; // 分配二维指针数组 IMAGE*** blocks = (IMAGE***)malloc(size * sizeof(IMAGE*)); for (int i = 0; i < size; i++) { blocks[i] = (IMAGE**)malloc(size * sizeof(IMAGE*)); for (int j = 0; j < size; j++) { blocks[i][j] = (IMAGE*)malloc(sizeof(IMAGE)); /*切割*/ // 初始化每个块的大小 blocks[i][j] = new IMAGE(blockWidth, blockHeight); // 从原图中截取对应区域 // 切换到img作为绘图设备 SetWorkingImage(&img); getimage(blocks[i][j], j * blockWidth, i * blockHeight, blockWidth, blockHeight); SetWorkingImage(); // 恢复到窗口 Resize(blocks[i][j], targetWidth, targetHeight); } } for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { //putimage(200 + blockWidth * i, 200 + blockHeight * j, blocks[i][j]); if (i == size - 1 && i == j) { setfillcolor(RGB(30, 30, 30)); solidrectangle(100 + targetWidth * i, 100 + targetHeight * j, targetWidth + 100 + targetWidth * i, targetHeight + 100 + targetHeight * j); return; } putimage(100 + targetWidth * i, 100 + targetHeight * j, blocks[j][i]); setlinecolor(RGB(0, 0, 0)); rectangle(100 + targetWidth * i, 100 + targetHeight * j, targetWidth + 100 + targetWidth * i, targetHeight + 100 + targetHeight * j); } } //释放内存 for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { delete blocks[i][j]; } free(blocks[i]); } free(blocks); } //按钮点击检测函数 int isClick(int x, int y) { // 检查鼠标消息队列中的所有消息 while (peekmessage(&msg, EX_MOUSE)) { if (msg.message == WM_LBUTTONDOWN) { // 检查是否在按钮区域内 int inArea = (msg.x >= BUTTON_X + x && msg.y >= BUTTON_Y + y && msg.x <= BUTTON_X + x + BUTTON_WIDTH && msg.y <= BUTTON_Y + y + BUTTON_HEIGHT); // 移除已处理的消息 while (peekmessage(&msg, EX_MOUSE)); return inArea; } // 移除其他鼠标消息 peekmessage(&msg, EX_MOUSE, PM_REMOVE); } return FALSE; } //绘制按钮函数 void Button(int x, int y, const char* word) { //设置按钮样式 setbkmode(TRANSPARENT);//设置背景模式透明 setfillcolor(RGB(231, 250, 235)); solidroundrect(BUTTON_X + x, BUTTON_Y + y, BUTTON_X + x + BUTTON_WIDTH, BUTTON_Y + y + BUTTON_HEIGHT, 20, 20);//透明边框矩形图 // 设置文字样式 settextstyle(50, 15, "华文宋体"); setbkmode(TRANSPARENT); settextcolor(RGB(35, 30, 30)); // 计算文字居中位置输出 int w = (BUTTON_WIDTH - textwidth(word)) / 2; int h = (BUTTON_HEIGHT - textheight(word)) / 2; outtextxy(BUTTON_X + x + w, BUTTON_Y + y + h, word); } //模式选择界面 int ModeSelection() { cleardevice(); GetImage("picture/bk.jpg", 0, 0); // 五种难度按钮 const char* str[] = { "简单模式","初级模式","中级模式","高级模式","终极模式" }; for (int i = 0; i < 5; i++) { Button(100, i * 100, str[i]); } // 显示提示信息 settextcolor(RGB(39, 220, 220)); setbkmode(TRANSPARENT); settextstyle(35, 20, "华文宋体"); outtextxy(300, 120, "请点击选择游戏难度"); // 等待选择 while (TRUE) { // 处理所有鼠标消息 while (peekmessage(&msg, EX_MOUSE)) { if (msg.message == WM_LBUTTONDOWN) { // 检查点击了哪个按钮 for (int i = 0; i < 5; i++) { int buttonTop = BUTTON_Y + i * 100; int buttonBottom = buttonTop + BUTTON_HEIGHT; if (msg.x >= BUTTON_X && msg.x <= BUTTON_X + BUTTON_WIDTH && msg.y >= buttonTop && msg.y <= buttonBottom) { return degrees[i]; } } } // 移除已处理的消息 peekmessage(&msg, EX_MOUSE, PM_REMOVE); } Sleep(10); } } //图片选择界面 int Selectpicture() { cleardevice(); GetImage("picture/bk.jpg", 0, 0); const int startX = 0, startY = 10; int selected = -1; // 3x3网格显示9种传统文化图片选项 for (int i = 0; i < 9; i++) { int row = i / 3, col = i % 3; Button(startX + col * 180, startY + row * 150, picture[i].name); } // 显示提示信息 settextcolor(RGB(39, 220, 220)); setbkmode(TRANSPARENT); settextstyle(35, 20, "华文宋体"); outtextxy(350, 120, "请点击选择拼图图片"); // 等待选择 while (selected == -1) { // 处理所有鼠标消息 while (peekmessage(&msg, EX_MOUSE)) { if (msg.message == WM_LBUTTONDOWN) { // 检查点击了哪个图片按钮 for (int i = 0; i < 9; i++) { int row = i / 3, col = i % 3; int buttonX = BUTTON_X + col * 200; int buttonY = BUTTON_Y + row * 150; if (msg.x >= buttonX && msg.x <= buttonX + BUTTON_WIDTH && msg.y >= buttonY && msg.y <= buttonY + BUTTON_HEIGHT) { selected = i; break; } } } // 移除已处理的消息 peekmessage(&msg, EX_MOUSE, PM_REMOVE); } Sleep(10); } return selected; } //游戏说明界面 void Information(int key) { cleardevice(); GetImage("picture/bk.jpg", 0, 0); // 设置文本样式 settextcolor(RGB(255, 215, 0)); // 金色 setbkmode(TRANSPARENT); settextstyle(25, 10, "华文宋体"); // 游戏说明文本 const char instructions[] = "这是一个传统文化主题的数字拼图游戏。\n" "目标是通过移动拼图块,使所有数字按顺序排列,右下角为空白格。\n" "每个数字对应一种中国传统文化元素:\n" "1: 京剧脸谱\n2: 中国结\n3: 剪纸\n" "4: 书法\n5: 国画\n6: 陶瓷\n" "7: 刺绣\n8: 皮影\n9: 灯笼\n\n" "操作方式:\n" "方向键 - 移动拼图\n" "ESC - 退出游戏\n" "对应数字- 查看传统文化元素介绍\n" "点击右下角图标返回主菜单..."; // 计算文本位置使其居中 int startX = 50, startY = 100; int lineHeight = 50; // 分行输出文本 char* context = NULL; char* line = strtok_s((char*)instructions, "\n", &context); while (line != NULL) { outtextxy(startX, startY, line); startY += lineHeight; line = strtok_s(NULL, "\n", &context); } // 等待按键并处理消息 while (true) { // 检查按键事件 if (kbhit()) { if (_getch()) { // 返回前清除消息队列 while (peekmessage(&msg, EX_MOUSE | EX_KEY, PM_REMOVE)); return; // 直接返回,不调用Menu() } } // 检查鼠标点击"返回"按钮 Button(400, 500, "返回主菜单"); if (isClick(400, 500)) { return; // 返回调用者 } Sleep(10); // 避免CPU占用过高 } } //初始化拼图游戏 void InitPuzzle(Blocks* game, int size, int picIndex) { game->size = size; game->picIndex = picIndex; game->moves = 0; // 初始化拼图数据 int num = 1; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { game->matrix[i][j] = (i == size - 1 && j == size - 1) ? 0 : num++; } } game->emptyRow = size - 1; game->emptyCol = size - 1; // 随机打乱拼图(减少循环次数,避免卡顿) for (int i = 0; i < size * size * 20; i++) { // 原为100,现为20 int dir = rand() % 4; int newRow = game->emptyRow, newCol = game->emptyCol; switch (dir) { case 0: newRow--; break; // 上 case 1: newRow++; break; // 下 case 2: newCol--; break; // 左 case 3: newCol++; break; // 右 } // 检查移动是否有效 if (newRow >= 0 && newRow < size && newCol >= 0 && newCol < size) { // 交换空白格和目标格 game->matrix[game->emptyRow][game->emptyCol] = game->matrix[newRow][newCol]; game->matrix[newRow][newCol] = 0; game->emptyRow = newRow; game->emptyCol = newCol; } } // 记录开始时间 start_time = time(NULL); is_time_up = FALSE; // 重置超时标志 } //检查是否胜利 int CheckWin(Blocks* game) { int num = 1; for (int i = 0; i < game->size; i++) { for (int j = 0; j < game->size; j++) { // 检查右下角是否为空白格 if (i == game->size - 1 && j == game->size - 1) { if (game->matrix[i][j] != 0) return FALSE; } else { // 检查其他格子是否按顺序排列 if (game->matrix[i][j] != num++) return FALSE; } } } return TRUE; } //移动拼图块 int MoveTile(Blocks* game, int direction) { int newRow = game->emptyRow; int newCol = game->emptyCol; // 计算目标位置 switch (direction) { case 0: newRow--; break; // 上 case 1: newRow++; break; // 下 case 2: newCol--; break; // 左 case 3: newCol++; break; // 右 default: return FALSE; } // 检查边界 if (newRow < 0 || newRow >= game->size || newCol < 0 || newCol >= game->size) { return FALSE; } // 交换空白格和目标格 game->matrix[game->emptyRow][game->emptyCol] = game->matrix[newRow][newCol]; game->matrix[newRow][newCol] = 0; game->emptyRow = newRow; game->emptyCol = newCol; game->moves++; return TRUE; } // 绘制拼图界面 void DrawPuzzle(Blocks* game) { cleardevice(); GetImage("picture/bk.jpg", 0, 0); // 设置文本样式 settextstyle(30, 15, "华文宋体"); setbkmode(TRANSPARENT); // 显示标题和步数 settextcolor(WHITE); outtextxy(50, 20, "传统文化拼图游戏"); char movesText[50]; sprintf(movesText, "步数: %d", game->moves); outtextxy(50, 60, movesText); int mainSize = 560; int blockSize = mainSize / game->size; int startX = (WINDOW_WIDTH - mainSize) / 10; int startY = (WINDOW_HEIGHT - mainSize) / 2; // 显示倒计时(调整位置,避免超出窗口) time_t current_time = time(NULL); int elapsed = (int)(current_time - start_time); int remaining = TIMELIMIT - elapsed; if (remaining < 0) remaining = 0; // 防止负数显示 char time_str[50]; sprintf(time_str, "剩余时间: %02d:%02d", remaining / 60, remaining % 60); // 时间小于10秒时显示红色警告 settextcolor(remaining <= 10 ? RED : WHITE); outtextxy(WINDOW_WIDTH - 300, 20, time_str); // 仅设置超时标志,不直接跳转界面 if (remaining == 0) { is_time_up = TRUE; } // 加载选中的图片 char filename[100]; sprintf(filename, "picture/%d.jpg", game->picIndex + 1); IMAGE img; loadimage(&img, filename); // 检查图片是否成功加载 if (img.getwidth() <= 0 || img.getheight() <= 0) { // 如果图片加载失败,使用默认数字块并显示提示 outtextxy(20, 120, "图片加载失败,使用数字模式"); for (int i = 0; i < game->size; i++) { for (int j = 0; j < game->size; j++) { int x = startX + j * blockSize; int y = startY + i * blockSize; if (game->matrix[i][j] == 0) { // 绘制空白格 setfillcolor(RGB(200, 200, 200)); fillrectangle(x, y, x + blockSize, y + blockSize); settextcolor(BLACK); outtextxy(x + blockSize / 2 - 10, y + blockSize / 2 - 10, "●"); } else { // 绘制拼图块 setfillcolor(RGB(231, 250, 235)); fillrectangle(x, y, x + blockSize, y + blockSize); // 显示数字 char numText[10]; sprintf(numText, "%d", game->matrix[i][j]); settextcolor(BLACK); outtextxy(x + blockSize / 2 - 10, y + blockSize / 2 - 10, numText); } // 绘制边框 setlinecolor(BLACK); rectangle(x, y, x + blockSize, y + blockSize); } } } else { IMAGE img; loadimage(&img, filename); // 加载图片 // 图片加载成功,切割图片并显示 int imgWidth = img.getwidth(); int imgHeight = img.getheight(); for (int i = 0; i < game->size; i++) { for (int j = 0; j < game->size; j++) { int x = startX + j * blockSize; int y = startY + i * blockSize; if (game->matrix[i][j] == 0) { //绘制空白格(显眼绿) setfillcolor(RGB(87, 166, 74)); fillrectangle(x, y, x + blockSize, y + blockSize); } else { // 计算当前拼图块在原图中的位置 int pieceIndex = game->matrix[i][j] - 1; // 0到size*size-1 int pieceRow = pieceIndex / game->size; int pieceCol = pieceIndex % game->size; // 计算在原图中的坐标和大小 int srcX = pieceCol * (imgWidth / game->size); int srcY = pieceRow * (imgHeight / game->size); int pieceWidth = imgWidth / game->size; int pieceHeight = imgHeight / game->size; // 切割并显示拼图块 IMAGE piece; loadimage(&piece, NULL, pieceWidth, pieceHeight); // 设置原图为当前绘图设备 SetWorkingImage(&img); // 从原图中切割指定区域 getimage(&piece, srcX, srcY, pieceWidth, pieceHeight); // 恢复默认绘图设备 SetWorkingImage(NULL); //缩放拼图块 Resize(&piece, blockSize, blockSize); putimage(x, y, &piece); } // 绘制边框 setlinecolor(BLACK); rectangle(x, y, x + blockSize, y + blockSize); } } } // 显示操作提示 settextstyle(20, 10, "华文宋体"); outtextxy(50, WINDOW_HEIGHT - 60, "方向键: 移动拼图 ESC: 退出游戏 L: 查看元素列表"); } // 显示胜利界面 void ShowWinScreen(Blocks* game) { cleardevice(); GetImage("picture/bk.jpg", 0, 0); // 设置胜利信息样式 settextstyle(50, 25, "华文行楷"); setbkmode(TRANSPARENT); settextcolor(RGB(255, 215, 0)); // 金色 // 显示胜利信息 outtextxy(WINDOW_WIDTH / 2 - 150, WINDOW_HEIGHT / 2 - 100, "恭喜你完成了拼图!"); // 显示步数 char movesText[50]; sprintf(movesText, "总步数: %d", game->moves); outtextxy(WINDOW_WIDTH / 2 - 100, WINDOW_HEIGHT / 2, movesText); // 显示用时 time_t end_time = time(NULL); int total_seconds = (int)(end_time - start_time); char timeText[50]; sprintf(timeText, "用时: %02d:%02d", total_seconds / 60, total_seconds % 60); outtextxy(WINDOW_WIDTH / 2 - 100, WINDOW_HEIGHT / 2 + 50, timeText); // 显示返回提示 settextstyle(30, 15, "华文宋体"); outtextxy(WINDOW_WIDTH / 2 - 100, WINDOW_HEIGHT / 2 + 100, "按任意键返回主菜单"); // 等待按键 _getch(); } // 显示超时界面 void ShowTimeOverScreen() { cleardevice(); GetImage("picture/bk.jpg", 0, 0); // 设置文字样式 settextstyle(50, 25, "华文行楷"); setbkmode(TRANSPARENT); settextcolor(RED); // 显示超时信息 outtextxy(WINDOW_WIDTH / 2 - 200, WINDOW_HEIGHT / 2 - 100, "时间到!拼图失败!"); // 显示返回提示 settextstyle(30, 15, "华文宋体"); outtextxy(WINDOW_WIDTH / 2 - 150, WINDOW_HEIGHT / 2 + 50, "按任意键返回主菜单"); // 等待按键 _getch(); } // 拼图游戏主循环 void PuzzlePage(int degree, int picIndex) { cleardevice(); GetImage("picture/bk.jpg", 0, 0); Blocks game; InitPuzzle(&game, degree, picIndex); /*char filename[100] = { 0 }; const char* picName = picture[picIndex]; sprintf_s(filename, "picture/%s%d.jpg", picName, rand() % 4 + 1);*/ while (TRUE) { BeginBatchDraw(); DrawPuzzle(&game); if (CheckWin(&game)) { ShowWinScreen(&game); return; } if (is_time_up) { ShowTimeOverScreen(); return; } // 处理键盘输入 if (peekmessage(&msg, EX_KEY)) { if (msg.message == WM_KEYDOWN) { switch (msg.vkcode) { case VK_UP: MoveTile(&game, 0); break; case VK_DOWN: MoveTile(&game, 1); break; case VK_LEFT: MoveTile(&game, 2); break; case VK_RIGHT: MoveTile(&game, 3); break; case 'L': // 显示传统文化元素列表 Information(picIndex); break; case VK_ESCAPE: return; // 返回上级菜单 } } } EndBatchDraw(); Sleep(50); // 控制刷新率 } } // 主菜单界面 void Menu() { // 初始化图形窗口 initgraph(WINDOW_WIDTH, WINDOW_HEIGHT); // 设置背景 GetImage("picture/bk.jpg", 0, 0); // 设置标题样式 settextcolor(RGB(39, 220, 220)); setbkmode(TRANSPARENT); settextstyle(100, 30, "华文行楷"); outtextxy(150, 50, "传统文化拼图游戏"); // 绘制菜单按钮 const char* word[] = { "开始游戏", "游戏说明", "退出" }; for (int i = 0; i < 3; i++) { Button(100, i * 150, word[i]); } // 处理菜单选择 while (1) { // 处理所有鼠标消息 while (peekmessage(&msg, EX_MOUSE)) { if (msg.message == WM_LBUTTONDOWN) { // 检查开始游戏按钮 if (msg.x >= BUTTON_X + 100 && msg.x <= BUTTON_X + 100 + BUTTON_WIDTH && msg.y >= BUTTON_Y + 0 && msg.y <= BUTTON_Y + 0 + BUTTON_HEIGHT) { int degree = ModeSelection(); int picIndex = Selectpicture(); PuzzlePage(degree, picIndex); return; } // 检查游戏说明按钮 else if (msg.x >= BUTTON_X + 100 && msg.x <= BUTTON_X + 100 + BUTTON_WIDTH && msg.y >= BUTTON_Y + 150 && msg.y <= BUTTON_Y + 150 + BUTTON_HEIGHT) { Information(key); return; } // 检查退出按钮 else if (msg.x >= BUTTON_X + 100 && msg.x <= BUTTON_X + 100 + BUTTON_WIDTH && msg.y >= BUTTON_Y + 300 && msg.y <= BUTTON_Y + 300 + BUTTON_HEIGHT) { closegraph(); exit(0); } } // 移除已处理的消息 peekmessage(&msg, EX_MOUSE, PM_REMOVE); } Sleep(10); } } 做到在游戏说明中,点击哪个数字,出现哪个数字对应的传统文化元素的介绍,点击返回可返回游戏说明。
最新发布
06-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值