C++ 设置控制台文本属性画一个DOS时代的字符窗口

简介: C++ 设置控制台文本属性画一个DOS时代的字符窗口

<windows.h>库函数:SetConsoleCursorPosition() 设置控制台光标位置;SetConsoleTextAttribute() 设置控制台文本属性,主要用它来设置颜色。用这两个函数在控制台屏幕上画2个在DOS时代可算得上比较洋气的字符窗口 ^_^


#include <iostream>
#include <windows.h> 
using namespace std;
struct winRect {
   LONG left=0; 
   LONG top=0; 
   LONG width=30;
   LONG height=3;
   SHORT foreColor=14;
   SHORT bgColor=1;
   string sTitle="";
};
void gotoXY(short x, short y)
{ //设置光标位置,坐标从左上角(0,0)起始
    COORD position = {x, y};
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hConsole, position);
}
void setColor(unsigned short foreColor=7,unsigned short bgColor=0) 
{ //设置输出文本的前景色和背景色 
  HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    foreColor%=16; bgColor%=16;
  SetConsoleTextAttribute(hConsole,foreColor|bgColor*16);
}
string rept(string s, int i) 
{ 
  string t="";
  for(int j=0;j<i;j++) t+=s;
  return t;
}
int paintWin(winRect w)
{
  int top,bottom,x=w.left,y=w.top;
  bool bT=(w.sTitle!="");
  bottom=w.top+w.height;
  setColor(w.foreColor,w.bgColor);
  string a="━",b=" ";
  w.height=w.height>2?w.height:3;
  w.height=bT?w.height+2:w.height;
  w.width=w.width>5?w.width:6;
  w.width=w.width%2==1?w.width+1:w.width;
  top=y;
  gotoXY(x,y++);
  cout<<"┏"<<rept(a,w.width/2-2)<<"┓";
  for (int i=0;i<w.height-2;i++){
    gotoXY(x,y++);
    cout<<"┃"<<rept(b,w.width-4)<<"┃";    
  }
  gotoXY(x,y++);  
  cout<<"┗"<<rept(a,w.width/2-2)<<"┛";
  bottom=y;
  gotoXY(x+2,top+1);
  if (bT) {
    gotoXY(x,w.top+2);  
    cout<<"┣"<<rept(a,w.width/2-2)<<"┫";
    gotoXY(x+2,w.top+1);
    cout<<w.sTitle.substr(0,w.width-4); 
    gotoXY(x+2,top+3);
  }
  return bottom;
}
int main(void)
{
  int ymax;
  char s[20];
  char t[]="字符窗口测试";
  SetConsoleTitle(t);
  winRect w;
  w.bgColor = 3;
  w.foreColor = 11;
  w.width = 20;
  w.height = 6; 
  ymax=paintWin(w);
  cout<<"hello, window!";
  winRect win;
  win.left = 24;
  win.top = 10;
  win.sTitle = "标题:123456789012345678901234567890"; //如标题太长,随窗口宽度截短 
  ymax=paintWin(win);
  cout<<"abcdefghijklmnopqrstuvwxyz"; //正文长度未作处理,要注意不能超过 窗口宽度-4 
  setColor();
  gotoXY(0,ymax);
  return 0;
}



执行结果如下:

20210201211915213.png


目录
相关文章
|
20天前
|
编译器 开发工具 C++
Dev-C++详细安装教程及中文设置(附带安装包链接)
Dev-C++详细安装教程及中文设置(附带安装包链接)
52 0
|
2月前
|
存储 算法 编译器
【C++ 字符数组的模板特化】面向字符串的C++模板特化:理解与实践
【C++ 字符数组的模板特化】面向字符串的C++模板特化:理解与实践
48 1
|
2月前
|
对象存储 C++
在C++语言中字符串流
在C++语言中字符串流
18 2
|
2月前
|
存储 C++ 索引
C++ string容器-字符存取讲解
C++ string容器-字符存取讲解
26 0
|
2月前
|
编译器 C++
c++关键字与三字符组
c++关键字与三字符组
18 0
|
2月前
|
C++ 容器
c++ STL 之 vector 的 capacity 和 size 属性区别
c++ STL 之 vector 的 capacity 和 size 属性区别
33 0
|
2月前
|
编译器 程序员 API
C++ 14 17 新特性:[[fallthrough]], [[nodiscard]], [[maybe_unused]], 和 [[deprecated]] 新属性的使用...
C++ 14 17 新特性:[[fallthrough]], [[nodiscard]], [[maybe_unused]], 和 [[deprecated]] 新属性的使用...
20 3
|
存储 编译器 Linux
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
|
22天前
|
C++
c++实现通讯录管理系统(控制台版)
c++实现通讯录管理系统(控制台版)
|
2月前
|
Linux API C++
【C++ 线程包裹类设计】跨平台C++线程包装类:属性设置与平台差异的全面探讨
【C++ 线程包裹类设计】跨平台C++线程包装类:属性设置与平台差异的全面探讨
56 2