티스토리 뷰

콘솔 환경에서 텍스트 컬러를 바꾸는 코드는 아래와 같습니다.


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
// color상수 지정 
#define BLACK 
#define BLUE 
#define GREEN 
#define CYAN 
#define RED 
#define MAGENTA 
#define BROWN 
#define LIGHTGRAY 
#define DARKGRAY 
#define LIGHTBLUE 
#define LIGHTGREEN 10 
#define LIGHTCYAN 11 
#define LIGHTRED 12 
#define LIGHTMAGENTA 13 
#define YELLOW 14 
#define WHITE 15 
 
#include<stdio.h> 
#include<windows.h> 
 
void textcolor(int foreground, int background); 
int main() 
textcolor(LIGHTRED, BLACK); 
printf("Hello World\n"); 
 
textcolor(YELLOW, BLUE); 
printf("Hello World\n"); 
return 0
 
void textcolor(int foreground, int background) 
int color=foreground+background*16
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color); 
}
cs

살펴보시면 textcolor 함수는 두개의 전달인자를 받는것을 알 수 있습니다. 첫번째는 글자색, 두번째는 배경색입니다.

결과화면


위 함수를 통해서 텍스트 컬러를 변경하게 되면, 위와같이 '계속하려면 아무키나 누르십시오'라는 메세지까지 색깔이 바뀌어서 나오기때문에 이게 싫으신 분들은 종료전에 원래 색깔로 바꿔 주시면 되겠습니다. 원래색깔은 글자색이 LIGHTGRAY, 배경색이 BLACK


1
    textcolor(LIGHTGRAY, BLACK);
cs


댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
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
글 보관함