#include <graphics.h> #include <conio.h> #include <string> #include <sstream> // 将 std::wstring 直接输出 void DrawText(int x, int y, const std::wstring& text) { outtextxy(x, y, text.c_str()); } // 合并单元格并写字 void MergeCell(int startX, int startY, int endX, int endY, const std::wstring& text) { // 绘制合并后的单元格边框 setlinecolor(BLACK); rectangle(startX, startY, endX, endY); // 填充合并后的单元格背景色 setfillcolor(WHITE); bar(startX + 1, startY + 1, endX - 1, endY - 1); // 计算文字位置(居中) int textWidth = textwidth(text.c_str()); int textHeight = textheight(text.c_str()); int x = startX + (endX - startX - textWidth) / 2; int y = startY + (endY - startY - textHeight) / 2; // 输出文字 DrawText(x, y, text); } void drawCourseTable() { // 初始化图形窗口 initgraph(1200, 800); setbkcolor(WHITE); cleardevice(); // 设置字体 settextstyle(20, 0, _T("宋体")); settextcolor(BLACK); // 绘制课程表 // 绘制表格边框 rectangle(50, 50, 1150, 750); // 高度调整为 750 以适应六行 // 绘制列 for (int i = 0; i < 6; ++i) { line(50 + 200 * i, 50, 50 + 200 * i, 750); } // 绘制行(六行) for (int i = 0; i < 7; ++i) { // 7 条线,6 行 line(50, 50 + 100 * i, 1150, 50 + 100 * i); } // 第一列的第二行到第六行分成两列 for (int i = 1; i < 6; ++i) { line(50, 50 + 100 * i, 150, 50 + 100 * i); // 水平线 line(150, 50 + 100 * i, 150, 50 + 100 * (i + 1)); // 垂直线 } // 填充表头 MergeCell(50, 50, 250, 150, L"时间"); MergeCell(250, 50, 450, 150, L"周一"); MergeCell(450, 50, 650, 150, L"周二"); MergeCell(650, 50, 850, 150, L"周三"); MergeCell(850, 50, 1050, 150, L"周四"); MergeCell(1050, 50, 1150, 150, L"周五"); // 合并上午和下午的格子 MergeCell(50, 150, 250, 350, L"上午\n下午"); // 合并上午和下午 MergeCell(150, 150, 250, 250, L"1-2节"); MergeCell(150, 250, 250, 350, L"3-4节"); // 合并晚上的格子 MergeCell(50, 350, 250, 550, L"晚上"); // 合并晚上 MergeCell(150, 350, 250, 450, L"5-6节"); MergeCell(150, 450, 250, 550, L"7-8节"); //
时间: 2025-03-15 08:19:16 浏览: 47
### C++ 使用 `graphics.h` 绘制课程表
在 C++ 中,可以利用 Turbo C 或 Borland C 的图形库 `graphics.h` 来完成简单的二维绘图操作。以下是关于如何通过该库实现课程表绘制、单元格合并以及文字居中的详细说明。
#### 1. 初始化图形环境
为了使用 `graphics.h` 库的功能,程序需要初始化图形模式并检测可用的驱动器和图形模式。
```cpp
#include <graphics.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, ""); // 初始化图形模式
}
```
此部分代码用于设置图形驱动器和模式,并调用函数 `initgraph()` 开始图形处理[^3]。
#### 2. 创建表格网格
可以通过循环结构来绘制水平线和垂直线以形成表格框架。
```cpp
void drawTable(int rows, int cols, int cellWidth, int cellHeight) {
for (int i = 0; i <= rows; ++i) { // 水平线条
line(0, i * cellHeight, cols * cellWidth, i * cellHeight);
}
for (int j = 0; j <= cols; ++j) { // 垂直线条
line(j * cellWidth, 0, j * cellWidth, rows * cellHeight);
}
}
```
上述代码定义了一个名为 `drawTable` 的函数,它接受行数、列数、单元格宽度和高度作为参数,并基于这些数据构建一个矩形网格[^4]。
#### 3. 合并单元格
要模拟 Excel 风格的单元格合并效果,在实际应用中可通过跳过某些边界的重绘达到视觉上的合并效果。例如:
```cpp
void mergeCells(int startX, int startY, int spanCols, int spanRows, int cellWidth, int cellHeight) {
rectangle(startX * cellWidth, startY * cellHeight,
(startX + spanCols) * cellWidth - 1, (startY + spanRows) * cellHeight - 1);
}
```
这里定义了另一个辅助函数 `mergeCells`,其功能是在指定位置上画出覆盖多个标准单元格的大矩形框,从而隐藏内部边界线,营造出“合并”的外观[^5]。
#### 4. 输出居中文本
为了让文本能够自动调整到对应区域中心点处显示出来,则需计算目标区域内坐标偏移量后再执行字符串打印命令。
```cpp
void centerTextInCell(const char* text, int colIndex, int rowIndex, int cellWidth, int cellHeight) {
int strLen = strlen(text);
int x = colIndex * cellWidth + ((cellWidth / 2) - (strLen * 6) / 2); // 字符平均宽约6像素
int y = rowIndex * cellHeight + (cellHeight / 2) + 8; // 数字'8'为字体基线下方空白区大小估计值
settextstyle(DEFAULT_FONT, HORIZ_DIR, 1); // 设置默认字体样式
outtextxy(x, y, text); // 将文本放置于计算后的坐标点
}
```
这段逻辑实现了动态定位每一块独立空间内的核心部位,并把给定的文字串精确嵌入进去的效果[^6]。
#### 完整示例代码
综合以上各部分内容可得如下完整案例演示脚本:
```cpp
#include <graphics.h>
#include <string.h>
#include <stdio.h>
// 表格绘制函数...
void drawTable(int rows, int cols, int cellWidth, int cellHeight);
// 单元格合并函数...
void mergeCells(int startX, int startY, int spanCols, int spanRows, int cellWidth, int cellHeight);
// 居中文本输出函数...
void centerTextInCell(const char* text, int colIndex, int rowIndex, int cellWidth, int cellHeight);
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
const int ROWS = 7, COLS = 5;
const int CELL_WIDTH = 80, CELL_HEIGHT = 40;
drawTable(ROWS, COLS, CELL_WIDTH, CELL_HEIGHT);
// 示例:合并第0行从第1列至第3列之间的三个单元格
mergeCells(1, 0, 3, 1, CELL_WIDTH, CELL_HEIGHT);
centerTextInCell("Monday", 1, 0, CELL_WIDTH * 3, CELL_HEIGHT);
getch();
closegraph(); // 关闭图形窗口
return 0;
}
void drawTable(int rows, int cols, int cellWidth, int cellHeight){
...
}
void mergeCells(int startX, int startY, int spanCols, int spanRows, int cellWidth, int cellHeight){
...
}
void centerTextInCell(const char* text, int colIndex, int rowIndex, int cellWidth, int cellHeight){
...
}
```
阅读全文
相关推荐

















