Skip to content

Instantly share code, notes, and snippets.

@CrBoy
Created April 26, 2010 18:26
Show Gist options
  • Save CrBoy/379686 to your computer and use it in GitHub Desktop.
Save CrBoy/379686 to your computer and use it in GitHub Desktop.
void gotoxy(int x, int y){
HANDLE hConsole = GetStdHandle( STD_OUTPUT_HANDLE );
COORD coord={x, y};
SetConsoleCursorPosition(hConsole, coord);
}
void setcolor(const char *fore, const char *back){
WORD type=0;
while(*fore!='\0'){
switch(*fore){
case 'r':
case 'R':
type |= FOREGROUND_RED;
break;
case 'g':
case 'G':
type |= FOREGROUND_GREEN;
break;
case 'b':
case 'B':
type |= FOREGROUND_BLUE;
break;
case '+':
type |= FOREGROUND_INTENSITY;
}
fore++;
}
while(*back!='\0'){
switch(*back){
case 'r':
case 'R':
type |= BACKGROUND_RED;
break;
case 'g':
case 'G':
type |= BACKGROUND_GREEN;
break;
case 'b':
case 'B':
type |= BACKGROUND_BLUE;
break;
case '+':
type |= BACKGROUND_INTENSITY;
}
back++;
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),type);
}//使用方法:setcolor("rgb+", "rgb+");
#ifndef __WIN32_CONSOLE_H__
#define __WIN32_CONSOLE_H__
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
void gotoxy(int x, int y); //在console mode的緩衝區中移動
void setcolor(const char* fore="rgb", const char* back=""); //設定console游標繪出的文字與背景顏色
#endif
@CrBoy
Copy link
Author

CrBoy commented Apr 26, 2010

這是我大一時候的作品,setcolor()的部份也許寫得還不夠好,若日後有機會希望再改進!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment