Created
October 1, 2018 17:25
-
-
Save ZJUGuoShuai/517f1d13ccbec220f15002e904033088 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <windows.h> | |
#include <stdio.h> | |
void normal(char* s) { | |
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); | |
WORD info_style = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE; | |
SetConsoleTextAttribute(hOut, info_style); | |
printf(s); | |
} | |
void success(char* s) { | |
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); | |
WORD info_style = FOREGROUND_GREEN | FOREGROUND_INTENSITY; | |
SetConsoleTextAttribute(hOut, info_style); | |
printf(s); | |
} | |
void success_bg(char* s) { | |
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); | |
WORD info_style = BACKGROUND_GREEN | BACKGROUND_INTENSITY; | |
SetConsoleTextAttribute(hOut, info_style); | |
printf(s); | |
} | |
void error(char* s) { | |
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); | |
WORD info_style = FOREGROUND_RED | FOREGROUND_INTENSITY; | |
SetConsoleTextAttribute(hOut, info_style); | |
printf(s); | |
} | |
void info(char* s) { | |
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); | |
WORD info_style = FOREGROUND_BLUE | FOREGROUND_INTENSITY; | |
SetConsoleTextAttribute(hOut, info_style); | |
printf(s); | |
} | |
void highlight(char* s) { | |
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); | |
WORD info_style = 0x0F; | |
SetConsoleTextAttribute(hOut, info_style); | |
printf(s); | |
} | |
void underline(char* s) { | |
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); | |
WORD info_style = 0x0F | COMMON_LVB_UNDERSCORE; | |
SetConsoleTextAttribute(hOut, info_style); | |
printf(s); | |
} | |
void override_prev_lines(char* s, int n) { | |
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); | |
CONSOLE_SCREEN_BUFFER_INFO csbInfo; | |
GetConsoleScreenBufferInfo(hOut, &csbInfo); | |
COORD cur_pos = csbInfo.dwCursorPosition; | |
SHORT width = csbInfo.dwMaximumWindowSize.X; | |
COORD prev_lines = {0, cur_pos.Y - n + 1}; | |
char saved_line[256] = {0}; | |
long unsigned int nSaved; | |
ReadConsoleOutputCharacter( hOut, saved_line, cur_pos.X + (n - 1) * width, prev_lines, &nSaved); | |
SetConsoleCursorPosition(hOut, prev_lines); | |
for (int i = 0; i < width * n; i++) | |
putchar(' '); | |
SetConsoleCursorPosition(hOut, prev_lines); | |
printf(s); | |
printf(saved_line); | |
} | |
int main() { | |
/* 消息样式化测试 | |
success("SUCCESS\n"); | |
error("ERROR\n"); | |
info("INFO\n"); | |
normal("NORMAL\n"); | |
highlight("HIGHLIGHT\n"); | |
underline("UNDERLINE\n"); | |
success_bg("SUCCESS BACKGROUND\n"); */ | |
printf("Input blah blah\n"); | |
printf("$ > "); | |
Sleep(1000); | |
// Insert a message | |
override_prev_lines("A MESSAGE FROM EMINEM.\n", 2); | |
getchar(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment