/* -----------------------------------------
本文件不需要提交、不允许改动
----------------------------------------- */
#pragma once
void cct_cls();
void cct_gotoxy(const int X, const int Y);
这是cmd_console_tools.h/* -----------------------------------------
本文件不需要提交、不允许改动
----------------------------------------- */
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <Windows.h>
#include "cmd_hdc_tools.h"
#include "cmd_console_tools.h"
//using namespace std;
extern "C" WINBASEAPI HWND WINAPI GetConsoleWindow(); //VS中此处会有波浪线,不影响正常使用
/* 已定义的静态全局变量 */
static HWND hWnd = GetConsoleWindow();
static HDC hdc = NULL;
static int _BgColor_, _FgColor_, _Width_, _High;
/***************************************************************************
函数名称:
功 能:在(x1,y1)-(x2,y2)之间画出一个像素点的连线
输入参数:const int x1:起点x坐标,左上角为(0,0)
const int y1:起点y坐标,左上角为(0,0)
const int x2:终点y坐标,左上角为(0,0)
const int y2:终点y坐标,左上角为(0,0)
返 回 值:
说 明:颜色直接用当前设定
***************************************************************************/
static void hdc_base_line(int x1, int y1, const int x2, const int y2)
{
MoveToEx(hdc, x1, y1, NULL);
LineTo(hdc, x2 + 1, y2 + 1);
#if 0
if (x1 == x2 && y1 == y2) { //一个点的情况
LineTo(hdc, x2 + 1, y2 + 1);
}
else if (x1 == x2 && abs(y1 - y2) == 1) { // 上下两个点的情况
LineTo(hdc, x2 + 1, y2 + 1);
}
else if (abs(x1-x2)==1 && y1 == y2) { //左右两个点的情况
LineTo(hdc, x2 + 1, y2 + 1);
}
else
LineTo(hdc, x2 + 1, y2 + 1);
#endif
}
/***************************************************************************
函数名称:
功 能:初始化
输入参数:const int bgcolor:背景色
const int fgcolor:前景色
const int width :屏幕宽度(点阵)
const int high :屏幕高度(点阵)
返 回 值:
说 明:
***************************************************************************/
void hdc_init(const int bgcolor, const int fgcolor, const int width, const int high)
{
/* 先释放,防止不release而再次init(hdc_release可重入) */
hdc_release();
/* 窗口init后,用一个静态全局量记录,后续hdc_cls()会用到 */
_BgColor_ = bgcolor;
_FgColor_ = fgcolor;
_Width_ = width;
_High = high;
hdc = GetDC(hWnd);
// cct_setcursor(CURSOR_INVISIBLE);
// cct_setcolor(bgcolor, fgcolor);
// cct_setfontsize("新宋体", 16);
// cct_setconsoleborder(width / 8 + !!(width % 8), high / 16 + !!(high % 16)); //将点阵的宽度及高度转换为特定字体的行列数,!!的含义:如果不是8/16的倍数,行列多+1
// cct_cls();
}
/***************************************************************************
函数名称:
功 能:释放画图资源
输入参数:
返 回 值:
说 明:可重入
***************************************************************************/
void hdc_release()
{
if (hdc) {
ReleaseDC(hWnd, hdc);
hdc = NULL;
// cct_setcursor(CURSOR_VISIBLE_NORMAL);
}
}
/***************************************************************************
函数名称:
功 能:设置画笔颜色,传入RGB值
输入参数:
返 回 值:
说 明:
***************************************************************************/
void hdc_set_pencolor(const int RGB_value)
{
SelectObject(hdc, GetStockObject(DC_PEN));
SetDCPenColor(hdc, RGB_value);
}
/***************************************************************************
函数名称:
功 能:设置画笔颜色,传入RGB三色,值0-255
输入参数:
返 回 值:
说 明:
***************************************************************************/
void hdc_set_pencolor(const unsigned char red, const unsigned char green, const unsigned char blue)
{
hdc_set_pencolor(RGB(red, green, blue));
}
/***************************************************************************
函数名称:
功 能:清除屏幕上现有的图形
输入参数:
返 回 值:
说 明:
***************************************************************************/
void hdc_cls()
{
/* 发现一定要换一种颜色初始化才能清除像素点,找到更简便方法的同学可以通知我 */
hdc_init(_BgColor_, (_FgColor_ + 1)%16, _Width_, _High);
/* 在thdc_init中会修改_FgColor_的值,因此先+1,再+15,正好保持thdc_cls前的颜色 */
hdc_init(_BgColor_, (_FgColor_ + 15)%16, _Width_, _High);
/* 部分机器上运行demo时,会出现hdc_cls()后第一根线有缺线的情况,加延时即可
如果部分机器运行还有问题,调高此延时值 */
Sleep(30);
}
/***************************************************************************
函数名称:
功 能:给出左上角坐标及宽度、高度,画出一个长方形
输入参数:const int left_up_x :左上角x
const int left_up_y :左上角y
const int width :宽度
const int high :高度
bool filled :是否需要填充(默认填充)
const int RGB_value :颜色(默认值则不改变)
返 回 值:
说 明:
***************************************************************************/
void hdc_rectangle(const int x1, const int y1, const int width, const int high, const int RGB_value)
{
const int x2 = x1 + (width < 1 ? 1 : width) - 1; //最小宽度为1
const int y2 = y1 + (high < 1 ? 1 : high) -1; //最小高度为1
/* 如果坐标没有在 hdc_init 初始化的区间内,则直接返回 */
if (x1 < 0 || x1 >= _Width_ || x2 < 0 || x2 >= _Width_ || y1 < 0 || y1 >= _High || y2 < 0 || y2 >= _High)
return;
/* 如果给了新的颜色,则需要设置 */
if (RGB_value != INVALID_RGB)
hdc_set_pencolor(RGB_value);
#if 1
for (int y = y1; y <= y2; y++) {
MoveToEx(hdc, x1, y, NULL);
LineTo(hdc, x2+1, y);
}
#elif 0
if (abs(x1-x2)<=abs(y1-y2)) {
/* 用y1~y2条竖线模拟画出矩形 */
for (int i = x1; i <= x2; i++) {
MoveToEx(hdc, x1, y1, NULL);
LineTo(hdc, x2 + 1, y2 + 1);
// hdc_base_line(i, y1, i, y2);
}
}
else {
/* 用y1~y2条横线模拟画出矩形 */
for (int i = y1; i <= y2; i++) {
MoveToEx(hdc, x1, y1, NULL);
LineTo(hdc, x2 + 1, y2 + 1);
// hdc_base_line(x1, i, x2, i);
}
}
#elif 0
/* 如果x1==x2,此时矩形变为一个宽度为1的直线,要特殊处理 */
if (x1 == x2) {
hdc_base_line(x1, y1, x2, y2);
}
else {
/* 用y1~y2条横线模拟画出矩形 */
for (int i = y1; i <= y2; i++)
hdc_base_line(x1, i, x2, i);
}
#endif
}
这是cmd_hdc_tools.cpp/* -----------------------------------------
本文件不需要提交、不允许改动
----------------------------------------- */
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <conio.h>
#include <windows.h>
using namespace std;
/* 静态全局变量(同一项目中的其它cpp不能访问本变量) */
static HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE); //取标准输出设备对应的句柄
/***************************************************************************
函数名称:cct_cls
功 能:完成与system("cls")一样的功能,但效率高
输入参数:
返 回 值:
说 明:清除整个屏幕缓冲区,不仅仅是可见窗口区域(使用当前颜色)
***************************************************************************/
void cct_cls()
{
COORD coord = { 0, 0 };
CONSOLE_SCREEN_BUFFER_INFO binfo; /* to get buffer info */
DWORD num;
/* 取当前缓冲区信息 */
GetConsoleScreenBufferInfo(hout, &binfo);
/* 填充字符 */
FillConsoleOutputCharacter(hout, (TCHAR)' ', binfo.dwSize.X * binfo.dwSize.Y, coord, &num);
/* 填充属性 */
FillConsoleOutputAttribute(hout, binfo.wAttributes, binfo.dwSize.X * binfo.dwSize.Y, coord, &num);
/* 光标回到(0,0) */
SetConsoleCursorPosition(hout, coord);
return;
}
/***************************************************************************
函数名称:cct_gotoxy
功 能:将光标移动到指定位置
输入参数:HANDLE hout:输出设备句柄
int X :指定位置的x坐标
int Y :指定位置的y坐标
返 回 值:无
说 明:此函数不准修改
***************************************************************************/
void cct_gotoxy(const int X, const int Y)
{
COORD coord;
coord.X = X;
coord.Y = Y;
SetConsoleCursorPosition(hout, coord);
}这是cmd_console_tools.cpp/* -----------------------------------------
本文件不需要提交、不允许改动
----------------------------------------- */
#pragma once
#include <Windows.h> //RGB需要
/* 定义用于汉诺塔的全局只读变量(该值为10,不准动) */
const int MAX_LAYER = 10;
/* 定义状态栏的XY坐标(不准动) */
const int Status_Line_X = 0;
const int Status_Line_Y = 37;
/* 定义用于字符方式显示(横向/纵向数组)时的全局只读变量 */
const int MenuItem4_Start_X = 0, MenuItem4_Start_Y = 17; //菜单4中横向显示的"初始***"的起始位置
const int MenuItem8_Start_X = 0, MenuItem8_Start_Y = 34; //菜单4中横向显示的"初始***"的起始位置
const int MenuItem9_Start_X = MenuItem8_Start_X, MenuItem9_Start_Y = MenuItem8_Start_Y; //菜单9中横向显示的"初始***"的起始位置
const int Underpan_A_X_OFFSET = 11, Underpan_A_Y_OFFSET = - 2; //菜单4/8/9中纵向显示的"A"和横向显示的"初始***"的相对位置差
const int Underpan_Distance = 15; //字符自动的间隔,单位是字符
/* 定义用于图形方式显示的全局只读变量(人为保证和字符显示区不重叠) */
const int HDC_Init_Delay = 1000; //画三个底盘、三个立柱、初始的n个盘子时,中间的延时,单位ms
const int HDC_Start_X = 100; //相对位置的起点x坐标(A柱底盘左上角)
const int HDC_Start_Y = 256; //相对位置的起点y坐标(A柱底盘左上角),不要超过
const int HDC_Base_Width = 8;//基本宽度w,单位像素点,人为保证是 HDC_Step_X 的倍数(柱子的宽度=w,每个盘子的宽度分别是3w~21w)
const int HDC_Base_High = 16;//基本高度h,单位像素点,人为保证是 HDC_Step_Y 的倍数(底座的高度=h,每个盘子的高度=h)
const int HDC_Top_Y = 20; //设置顶部Y坐标,盘子移动是,到该坐标即停止,人为保证 (HDC_Start_Y - HDC_Top_Y)是 HDC_Step_Y 的倍数(最小值为0,表示移动到顶部)
const int HDC_Underpan_Distance = 100; //两个底盘之间的间隔,单位为像素点,人为保证是 HDC_Step_X 的倍数即可
const int HDC_Step_X = 1; //每个盘子左右移动时的步进距离,单位为像素点,取值范围(1-8)
const int HDC_Step_Y = 1; //每个盘子上下移动时的步进距离,单位为像素点,取值范围(1-16)
/* 定义底座和盘子的颜色*/
const int HDC_COLOR[MAX_LAYER + 2] = {
RGB(12,12,12), //背景底色
RGB(0,55,218), //1#盘(最小的盘子)的颜色
RGB(19,161,14),
RGB(58,150,221),
RGB(197,15,31),
RGB(136,23,152),
RGB(193,156,0),
RGB(204,204,204),
RGB(118,118,118),
RGB(59,120,255),
RGB(22,198,12), //10#盘(最大的盘子)的颜色
RGB(249,241,165) //基座颜色(底盘+立柱)
}; //0是底色,1-10是10个盘子的颜色,11是基座的颜色
这是hanoi_const_value.h完成剩余代码
最新发布