[C语言] Printf打印字符到指定位置,且可以指定颜色显示

[复制链接]
 楼主| 一路向北lm 发表于 2020-6-2 20:41 | 显示全部楼层 |阅读模式
  1. #include<windows.h>
  2. #include<stdio.h>

  3. //设置文字颜色枚举结构
  4. typedef enum
  5. {
  6.         BLACK = 0,
  7.         BLUE,
  8.         GREEN,
  9.         GYAN,
  10.         RED,
  11.         MAGENTA,
  12.     BROWN,
  13.     LIGHTGRAY,
  14.     DARKGRAY,
  15.     LIGHTBLUE,
  16.     LIGHTGREEN,
  17.     LIGHTCYAN,
  18.     LIGHTRED,
  19.     LIGHTMAGENTA,
  20.     YELLOW,
  21.     WHITE
  22. }Text_Color;

 楼主| 一路向北lm 发表于 2020-6-2 20:42 | 显示全部楼层
// 对于Windows 平台
void gotoxy(int x,int y)
{
        COORD pos = {x,y};
        HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
        SetConsoleCursorPosition(hOut,pos);
}

//设置文字颜色
// 0 - BLACK 黑
// 1 - BLUE 兰
// 2 - GREEN 绿
// 3 - CYAN 青
// 4 - RED 红
// 5 - MAGENTA 洋红
// 6 - BROWN 棕
// 7 - LIGHTGRAY 淡灰
// 8 - DARKGRAY 深灰
// 9 - LIGHTBLUE 淡兰
// 10 - LIGHTGREEN 淡绿
// 11 - LIGHTCYAN 淡青
// 12 - LIGHTRED 淡红
// 13 - LIGHTMAGENTA 淡洋红
// 14 - YELLOW 黄
// 15 - WHITE 白
void SetColor(int ForgC)
{
     WORD wColor;
     //We will need this handle to get the current background attribute
     HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
     CONSOLE_SCREEN_BUFFER_INFO csbi;

    //We use csbi for the wAttributes word.
        if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
        {
        //Mask out all but the background attribute, and add in the forgournd color
        wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
        SetConsoleTextAttribute(hStdOut, wColor);
    }
}
// 打点函数
void ponit(int x,int y,char color)
{
   gotoxy(x,y);
   SetColor(color);
   printf(".");
}
//清屏函数
void clear_display()
{
  system("cls");
}

您需要登录后才可以回帖 登录 | 注册

本版积分规则

293

主题

3837

帖子

81

粉丝
快速回复 在线客服 返回列表 返回顶部