码迷,mamicode.com
首页 > Windows程序 > 详细

winconsio.h:一个用于在windows平台替代conio.h的自用库

时间:2015-05-17 13:28:42      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:

 1 #ifndef WIN_CONS_IO_H
 2 #define WIN_CONS_IO_H
 3 
 4 #include <windows.h>
 5 
 6 #define SetConsColor(colorIndex) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colorIndex)
 7 #define BeginFGRed SetConsColor(FOREGROUND_RED)
 8 #define BeginFGBlue SetConsColor(FOREGROUND_BLUE)
 9 #define BeginFGGreen SetConsColor(FOREGROUND_GREEN)
10 #define BeginFGAuto SetConsColor(FOREGROUND_INTENSITY)
11 #define BeginBGRed SetConsColor(BACKGROUND_RED)
12 #define BeginBGBlue SetConsColor(BACKGROUND_BLUE)
13 #define BeginBGGreen SetConsColor(BACKGROUND_GREEN)
14 #define BeginBGAuto SetConsColor(BACKGROUND_INTENSITY)
15 
16 void cls(void)
17 {
18    //Get the handle to the current output buffer...
19    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
20    //This is used to reset the carat/cursor to the top left.
21    COORD coord = {0, 0};
22    //A return value... indicating how many chars were written
23    //   not used but we need to capture this since it will be
24    //   written anyway (passing NULL causes an access violation).
25    DWORD count;
26    //This is a structure containing all of the console info
27    // it is used here to find the size of the console.
28    CONSOLE_SCREEN_BUFFER_INFO csbi;
29    //Here we will set the current color
30    if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
31    {
32       //This fills the buffer with a given character (in this case 32=space).
33       FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
34       FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
35       //This will set our cursor position for the next print statement.
36       SetConsoleCursorPosition(hStdOut, coord);
37    }
38 }
39 
40 #define gotoXY(x, y) SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), {x, y})
41 
42 void hideCur(void)
43 {
44    CONSOLE_CURSOR_INFO cciCursor;
45    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
46  
47    if(GetConsoleCursorInfo(hStdOut, &cciCursor))
48    {
49       cciCursor.bVisible = FALSE;
50       SetConsoleCursorInfo(hStdOut, &cciCursor);
51    }
52 }
53  
54 
55 void showCur(void)
56 {
57    CONSOLE_CURSOR_INFO cciCursor;
58    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
59  
60    if(GetConsoleCursorInfo(hStdOut, &cciCursor))
61    {
62       cciCursor.bVisible = TRUE;
63       SetConsoleCursorInfo(hStdOut, &cciCursor);
64    }
65 }
66 
67 
68 char getCh (void)
69 {
70   HANDLE hStdin = GetStdHandle (STD_INPUT_HANDLE);
71   INPUT_RECORD irInputRecord;
72   DWORD dwEventsRead;
73   char cChar;
74 
75   while(ReadConsoleInputA (hStdin, &irInputRecord, 1, &dwEventsRead)) /* Read key press */
76     if (irInputRecord.EventType == KEY_EVENT
77     &&irInputRecord.Event.KeyEvent.wVirtualKeyCode != VK_SHIFT
78     &&irInputRecord.Event.KeyEvent.wVirtualKeyCode != VK_MENU
79     &&irInputRecord.Event.KeyEvent.wVirtualKeyCode != VK_CONTROL)
80     {
81       cChar = irInputRecord.Event.KeyEvent.uChar.AsciiChar;
82     ReadConsoleInputA (hStdin, &irInputRecord , 1, &dwEventsRead); /* Read key release */
83     return cChar;
84     }
85   return EOF;
86 }
87 
88 typedef struct pos { int x, int y; } posT;
89 #define UpPos(pos) { pos.x - 1, pos.y }
90 #define DownPos(pos) { pos.x + 1, pos.y }
91 #define LeftPos(pos) { pos.x, pos.y - 1 }
92 #define RightPos(pos) { pos.x, pos.y + 1 }
93 
94 #endif

 

winconsio.h:一个用于在windows平台替代conio.h的自用库

标签:

原文地址:http://www.cnblogs.com/yanh2001/p/winconsioh20150517.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!