首先必须包含在头文件里: #include
system("title C++"); //设置cmd窗口标题为C++
system("color 0B"); //设置颜色,0代表背景色,B代表前景色
system("date /t"); //打印当前日期
system("time /t"); //打印当前时间
system("mode con cols=48 lines=25"); //设置窗口宽度高度,下面代码中不一致主要是因为IDE用的DEV-C++,环境变量没有配置好,shutdown指令也是同理
另外,cmd的指令大部分不区分大小写,如ping和PING效果是一样的;
看完下面这个C++调用DOS命令实现定时关机的例子,应该能理解的清晰一点:
#include
#include
#include
#include
using namespace std;
int print(){
cout<<" ╪╪╪╪╪╪╧╧╧╧╧╧╧╧╪╪╪╪╪╪\n";
cout<<"╔═══╧╧ C++关机程序 ╧╧═══╗\n";
cout<<"║※1.实现10分钟内的定时关闭计算机 ║\n";
cout<<"║※2.立即关闭计算机 ║\n";
cout<<"║※3.注销计算机 ║\n";
cout<<"║※4.重启计算机 ║\n";
cout<<"║※5.休眠计算机 ║\n";
cout<<"║※0.退出系统 ║\n";
cout<>c;
switch(c){
case 0: break;
case 1: cout<>t;
if(atoi(t.c_str())>600 || atoi(t.c_str())<0){
cout<<"Error!\n";
break;
}
char t1[3];
t.copy(t1, t.size(), 0);
system(strcat(cmd,t1));
case 2: system("c:\\windows\\system32\\shutdown.exe -p");break;
case 3: system("c:\\windows\\system32\\shutdown.exe -l");break;
case 4: system("c:\\windows\\system32\\shutdown.exe -r");break;
case 5: system("c:\\windows\\system32\\shutdown.exe -h");break;
default: cout<<"Error!\n";
}
system("pause");
exit(0);
}
2. 光标位置设定
void SetCursorPosition(const int x, const int y)//设置光标位置
{
COORD position;
position.X = x * 2;
position.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), position);
}
其中,COORD是Windows API中定义的一种结构,表示一个字符在控制台屏幕上的坐标。其定义为:
typedef struct _COORD {
SHORT X; // horizontal coordinate
SHORT Y; // vertical coordinate
} COORD;
而GetStdHandle()返回标准的输入、输出或错误的设备的句柄,也就是获得输入、输出/错误的屏幕缓冲区的句柄,值有下列几种:
值
含义
STD_INPUT_HANDLE
标准输入的句柄
STD_OUTPUT_HANDLE
标准输出的句柄
STD_ERROR_HANDLE
标准错误的句柄
SetConsoleCursorPosition是一个Windows API , 作用是设置控制台(cmd)光标位置。
3. 文本颜色设定
void SetColor(int colorID)//设置文本颜色
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colorID);
}
SetConsoleTextAttribute是一个可以在API(应用程序编程接口)中设置控制台窗口字体颜色和背景色的计算机函数,其原型为:
BOOL SetConsoleTextAttribute(HANDLE hConsoleOutput, WORD wAttributes);
其中,wAttributes是用来设置颜色的参数:
wAttributes
颜色
对应的值
FOREGROUND_BLUE
字体颜色:蓝
1
FOREGROUND_GREEN
字体颜色:绿
2
FOREGROUND_RED
字体颜色:红
4
FOREGROUND_INTENSITY
前景色高亮显示
8
BACKGROUND_BLUE
背景颜色:蓝
16
BACKGROUND_GREEN
背景颜色:绿
32
BACKGROUND_RED
背景颜色:红
64
BACKGROUND_INTENSITY
背景色高亮显示
128
也就是说SetConsoleTextAttribute函数是靠一个字节的低四来控制前景色,高四位来控制背景色;
再来看下面一段代码:
void SetBackColor()//设置文本背景色
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_BLUE |
BACKGROUND_BLUE |
BACKGROUND_GREEN |
BACKGROUND_RED );
}
这段代码中,颜色部分四个位或,也就相当于0111 0001,因此此处的值也可写作0x71,表示字体颜色为蓝色,背景色为白色,若改为0x01,则表示字体为蓝色,背景为黑色。
4. point.cpp
#include "point.h"
#include "tools.h"
#include
void Point::Print()//输出方块
{
SetCursorPosition(x, y);
std::cout << "■" ;
}
void Point::PrintCircular()//输出圆形
{
SetCursorPosition(x, y);
std::cout << "●" ;
}
void Point::Clear()//清除输出
{
SetCursorPosition(x, y);
std::cout <x = x;
this->y = y;
}
5. point.h
#ifndef POINT_H
#define POINT_H
class Point
{
public:
Point(){}
Point(const int x, const int y) : x(x), y(y) {}
void Print();
void PrintCircular();
void Clear();
void ChangePosition(const int x, const int y);
bool operator== (const Point& point) { return (point.x == this->x) && (point.y == this->y); }
int GetX(){ return this->x; }
int GetY(){ return this->y; }
private:
int x, y;
};
#endif // POINT_H
两个point文件,主要就是定义了一个Point类,用来打印点和改变点的坐标,结构很清晰。
最后,附上阅读代码的原链接:
https://github.com/silence1772/GreedySnake/blob/master/tools.cpp
https://github.com/silence1772/GreedySnake/blob/master/tools.h
https://github.com/silence1772/GreedySnake/blob/master/point.cpp
https://github.com/silence1772/GreedySnake/blob/master/point.h
作者:AnDiXL