前回に引き続き背景色がどうしても諦められず・・・
色コードで色々試した結果、
下のような16色の背景色を出力するのに成功しました!
この色は0x0001 などのコードを入力して出力された色から逆にhtmlの色コードのcolor名に当てはめたものです。実際にこの色コードが存在するものかどうかは?です。
ソースの中のコメントに記述している【#000080】などのコード色はhtmlの色コードなのでC言語には関係ありませんが、色々コードを使って試している時にわからなくなりそうだったもので記述しました^^;
#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できればいいので、
ヘッダファイルに保存することにしました。
念のため下のソースを試してみると・・・
#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;
}
無事動作確認!!






コメント