-콘솔의 원하는 위치에 텍스트를 출력합니다.
ex:) printText(“Hello World!!”,3,DEFAULT,RIGHT);
//오른쪽 정렬로 우측 3칸을 비우고 현재 라인에 문자열 출력
#include
#include
//텍스트 출력 정렬
#define LEFT -1
#define CENTER 0
#define RIGHT 1
//기본 좌표
#define DEFAULT -1
void printText(char* buffer,int x,int y,int align){
COORD cr;
CONSOLE_SCREEN_BUFFER_INFO info;
int width; //콘솔 너비
int height; //콘솔 높이
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE),&info);
//콘솔 너비,높이 계산
width=info.srWindow.Right-info.srWindow.Left;
height=info.srWindow.Bottom-info.srWindow.Top;
//디폴트 좌표 설정
if(x == DEFAULT || x < 0 || x > width){
x=0;
}
if(y == DEFAULT){
y=info.dwCursorPosition.Y;
}
//실제 좌표 계산
if(align == CENTER){
cr.X=(width-(strlen(buffer)+x))/2;
}else if(align == RIGHT){
cr.X=width-(strlen(buffer)+x);
}else{
cr.X=x;
}
if(cr.X < 0){
cr.X=0;
}
cr.Y=y;
//좌표 설정
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),cr);
puts(buffer);
} |