生活随笔
收集整理的這篇文章主要介紹了
c++同一屏幕输出多种颜色字体
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
輸出彩色的幾行字
我們先看一下效果圖:
同一屏幕出現了多種不同的顏色
代碼如下:
#include <stdio.h>
#include <Windows.h>
#include <conio.h>void color(WORD c
);
void color(WORD c
)
{SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE
), c
);
}
int main()
{color(0x0c);printf("hello world!\n");color(0x09);printf("hello world!\n");return 0;
}
原理其實就是用到了SetConsoleTextAttribute函數改變文字和背景顏色
SetConsoleTextAttribute函數改變文字和背景顏色
輸出一個絢麗的企鵝
效果圖如下:
代碼如下:
#include <stdio.h>
#include <Windows.h>
#include <conio.h>void color(WORD c
);
void kongge(int sum
);
void chars(int sum
);
void print_start();void color(WORD c
)
{SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE
), c
);
}
void kongge(int sum
)
{while (sum
--)printf(" ");
}
void chars(int sum
)
{while (sum
--)printf("#");
}void print_start()
{kongge(17);chars(6);printf("\n");kongge(15);chars(11);printf("\n");kongge(14);chars(13);printf("\n");kongge(14);chars(2);color(0x04);chars(3);color(0x07);chars(2);color(0x04);chars(3);color(0x07);chars(3);printf("\n");kongge(14);chars(2);color(0x04);chars(1);color(0x07);chars(1);color(0x04);chars(1);color(0x07);chars(2);color(0x04);chars(1);color(0x07);chars(1);color(0x04);chars(1);color(0x07);chars(3);printf("\n");kongge(14);color(0x07);chars(4);color(0x06);chars(4);color(0x07);chars(5);printf("\n");kongge(14);color(0x07);chars(2);color(0x06);chars(8);color(0x07);chars(3);printf("\n");kongge(13);chars(3);color(0x08);chars(2);color(0x06);chars(4);color(0x08);chars(2);color(0x07);chars(4);printf("\n");kongge(12);color(0x07);chars(2);color(0x02);chars(11);color(0x07);chars(4);printf("\n");kongge(11);chars(2);color(0x02);chars(13);color(0x07);chars(4);printf("\n");kongge(10);chars(3);color(0x02);chars(13);color(0x07);chars(5);printf("\n");kongge(9);chars(3);color(0x02);chars(15);color(0x07);chars(5);printf("\n");kongge(8);chars(3);color(0x02);chars(16);color(0x07);chars(5);printf("\n");kongge(7);chars(4);color(0x02);chars(16);color(0x07);chars(5);printf("\n");kongge(7);chars(1);color(0x06);chars(2);color(0x07);chars(1);color(0x02);chars(14);color(0x02);chars(2);color(0x07);chars(4);color(0x06);chars(1);printf("\n");kongge(7);color(0x06);chars(4);color(0x07);chars(1);color(0x02);chars(13);color(0x06);chars(3);color(0x07);chars(2);color(0x06);chars(2);printf("\n");kongge(6);color(0x06);chars(5);color(0x07);chars(2);color(0x02);chars(10);color(0x07);chars(1);color(0x06);chars(9);printf("\n");kongge(2);color(0x06);chars(9);color(0x7);chars(1);color(0x02);chars(9);color(0x07);chars(2);color(0x06);chars(11);printf("\n");kongge(5);color(0x06);chars(8);color(0x07);chars(1);color(0x02);chars(6);color(0x07);chars(3);color(0x06);chars(10);printf("\n");kongge(3);color(0x06);chars(11);color(0x07);chars(9);color(0x06);chars(6);printf("\n");kongge(9);color(0x06);chars(4);color(0x07);chars(11);color(0x06);chars(3);printf("\n");color(0x06);
}
int main()
{print_start();return 0;
}
上面的代碼借鑒于: https://blog.csdn.net/akbarmain/article/details/104230443
總結
以上是生活随笔為你收集整理的c++同一屏幕输出多种颜色字体的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。