前回に引き続き背景色がどうしても諦められず・・・
色コードで色々試した結果、
下のような16色の背景色を出力するのに成功しました!
ソースの中のコメントに記述している【#000080】などのコード色はhtmlの色コードなのでC言語には関係ありませんが、色々コードを使って試している時にわからなくなりそうだったもので記述しました^^;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
#include <windows.h> #include <stdio.h> #include <wincon.h> //文字色の色コード #define NAVY 0x0001 //#000080←htmlの色コード #define BREEN 0x0002 //#008000 #define TEAL 0x0003 //#008080 #define MAROON 0x0004 //#800000 #define PURPLE 0x0005 //#800080 #define OLIVE 0x0006 //#808000 #define SILVER 0x0007 //#c0c0c0 #define GRAY 0x0008 //#808080 #define BLUE 0x0009 //#0000ff #define BLACK 0x0010 //#000000 #define LIME 0x0002|0x0008 //#00ff00 #define CYAN 0x0002|0x0009 //#00ffff #define RED 0x0004|0x0008 //#ff0000 #define MAGENTA 0x0004|0x0009 //#ff00ff #define YELLOW 0x0006|0x0008 //#ffff00 #define WITE 0x0006|0x0009 //#ffffff //背景色の色コード #define B_NAVY 0x0010 //#000080 #define B_BREEN 0x0020 //#008000 #define B_TEAL 0x0030 //#008080 #define B_MAROON 0x0040 //#800000 #define B_PURPLE 0x0050 //#800080 #define B_OLIVE 0x0060 //#808000 #define B_SILVER 0x0070 //#c0c0c0 #define B_GRAY 0x0080 //#808080 #define B_BLUE 0x0090 //#0000ff #define B_BLACK 0x0100 //#000000 #define B_LIME 0x0020|0x0080 //#00ff00 #define B_CYAN 0x0020|0x0090 //#00ffff #define B_RED 0x0040|0x0080 //#ff0000 #define B_MAGENTA 0x0040|0x0090 //#ff00ff #define B_YELLOW 0x0060|0x0080 //#ffff00 #define B_WITE 0x0060|0x0090 //#ffffff /******************************************************************************************************** 以下の構文を SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), 0x05|0x0020|0x0010); SET(COLOR, 文字色 | 背景色) で色の設定ができるように#defineに文字を置き換え ********************************************************************************************************/ //SetConsoleTextAttribute(GetStdHandleをSETに置き換え #define SET SetConsoleTextAttribute(GetStdHandle //STD_OUTPUT_HANDLE)をCOLORに置き換え #define COLOR STD_OUTPUT_HANDLE) |
とりあえず、使いたい時にだけincludeできればいいので、
ヘッダファイルに保存することにしました。
念のため下のソースを試してみると・・・
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
#include <stdio.h> #include "color16.h" int main(void) { SET(COLOR, 0x0f); printf("\t\t\t "); SET(COLOR, 0x0001 | 0x0020|0x0080); printf("Hello, World!"); SET(COLOR, 0x0001 | 0x0020|0x0090); printf("Hello, World!"); SET(COLOR, 0x0001 | 0x0040|0x0080); printf("Hello, World!"); SET(COLOR, 0x0001 | 0x0040|0x0090); printf("Hello, World!"); SET(COLOR, 0x0001 | 0x0060|0x0080) ; printf("Hello, World!"); SET(COLOR, 0x0001 | 0x0060|0x0090); printf("Hello, World!\n"); SET(COLOR, 0x0f); printf("\t\t\t "); SET(COLOR, 0x0002 | 0x0020 | 0x0080); printf("Hello, World!"); SET(COLOR, 0x0002 | 0x0020 | 0x0090); printf("Hello, World!"); SET(COLOR, 0x0002 | 0x0040 | 0x0080); printf("Hello, World!"); SET(COLOR, 0x0002 | 0x0040 | 0x0090); printf("Hello, World!"); SET(COLOR, 0x0002 | 0x0060 | 0x0080); printf("Hello, World!"); SET(COLOR, 0x0002 | 0x0060 | 0x0090); printf("Hello, World!\n"); SET(COLOR, 0x0f); printf("\t\t\t "); SET(COLOR, 0x0003 | 0x0020 | 0x0080); printf("Hello, World!"); SET(COLOR, 0x0003| 0x0020 | 0x0090); printf("Hello, World!"); SET(COLOR, 0x0003 | 0x0040 | 0x0080); printf("Hello, World!"); SET(COLOR, 0x0003 | 0x0040 | 0x0090); printf("Hello, World!"); SET(COLOR, 0x0003 | 0x0060 | 0x0080); printf("Hello, World!"); SET(COLOR, 0x0003 | 0x0060 | 0x0090); printf("Hello, World!\n"); SET(COLOR, 0x0f); printf("\t\t\t "); SET(COLOR, 0x0004 | 0x0020 | 0x0080); printf("Hello, World!"); SET(COLOR, 0x0004 | 0x0020 | 0x0090); printf("Hello, World!"); SET(COLOR, 0x0004 | 0x0040 | 0x0080); printf("Hello, World!"); SET(COLOR, 0x0004 | 0x0040 | 0x0090); printf("Hello, World!"); SET(COLOR, 0x0004 | 0x0060 | 0x0080); printf("Hello, World!"); SET(COLOR, 0x0004 | 0x0060 | 0x0090); printf("Hello, World!\n"); SET(COLOR, 0x0f); printf("\t\t\t "); SET(COLOR, 0x0005 | 0x0020 | 0x0080); printf("Hello, World!"); SET(COLOR, 0x0005 | 0x0020 | 0x0090); printf("Hello, World!"); SET(COLOR, 0x0005 | 0x0040 | 0x0080); printf("Hello, World!"); SET(COLOR, 0x0005 | 0x0040 | 0x0090); printf("Hello, World!"); SET(COLOR, 0x0005 | 0x0060 | 0x0080); printf("Hello, World!"); SET(COLOR, 0x0005 | 0x0060 | 0x0090); printf("Hello, World!\n"); SET(COLOR, 0x0f); printf("\t\t\t "); SET(COLOR, 0x0006 | 0x0020 | 0x0080); printf("Hello, World!"); SET(COLOR, 0x0006 | 0x0020 | 0x0090); printf("Hello, World!"); SET(COLOR, 0x0006 | 0x0040 | 0x0080); printf("Hello, World!"); SET(COLOR, 0x0006 | 0x0040 | 0x0090); printf("Hello, World!"); SET(COLOR, 0x0006 | 0x0060 | 0x0080); printf("Hello, World!"); SET(COLOR, 0x0006 | 0x0060 | 0x0090); printf("Hello, World!\n"); SET(COLOR, 0x0f); printf("\t\t\t "); SET(COLOR, 0x0007 | 0x0020 | 0x0080); printf("Hello, World!"); SET(COLOR, 0x0007 | 0x0020 | 0x0090); printf("Hello, World!"); SET(COLOR, 0x0007 | 0x0040 | 0x0080); printf("Hello, World!"); SET(COLOR, 0x0007 | 0x0040 | 0x0090); printf("Hello, World!"); SET(COLOR, 0x0007 | 0x0060 | 0x0080); printf("Hello, World!"); SET(COLOR, 0x0007 | 0x0060 | 0x0090); printf("Hello, World!\n"); SET(COLOR, 0x0f); printf("\t\t\t "); SET(COLOR, 0x0008 | 0x0020 | 0x0080); printf("Hello, World!"); SET(COLOR, 0x0008 | 0x0020 | 0x0090); printf("Hello, World!"); SET(COLOR, 0x0008 | 0x0040 | 0x0080); printf("Hello, World!"); SET(COLOR, 0x0008 | 0x0040 | 0x0090); printf("Hello, World!"); SET(COLOR, 0x0008 | 0x0060 | 0x0080); printf("Hello, World!"); SET(COLOR, 0x0008 | 0x0060 | 0x0090); printf("Hello, World!\n"); SET(COLOR, 0x0f); printf("\t\t\t "); SET(COLOR, 0x0009 | 0x0020 | 0x0080); printf("Hello, World!"); SET(COLOR, 0x0009 | 0x0020 | 0x0090); printf("Hello, World!"); SET(COLOR, 0x0009 | 0x0040 | 0x0080); printf("Hello, World!"); SET(COLOR, 0x0009 | 0x0040 | 0x0090); printf("Hello, World!"); SET(COLOR, 0x0009 | 0x0060 | 0x0080); printf("Hello, World!"); SET(COLOR, 0x0009 | 0x0060 | 0x0090); printf("Hello, World!\n\n"); SET(COLOR, 0x0f); printf("\t\t\t "); return 0; } |
無事動作確認!!