-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconsole.h
211 lines (181 loc) · 4.71 KB
/
console.h
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#ifndef __CONSOLE_H__
#define __CONSOLE_H__
#include <windows.h>
#include <iostream>
#include <conio.h>
enum COLOR
{
BLACK,
BLUE,
GREEN,
CYAN,
RED,
PURPLE,
GRAY,
WHITE,
LIGHTGRAY,
LIGHTBLUE,
LIGHTGREEN,
LIGHTCYAN,
LIGHTRED,
LIGHTPURPLE,
LIGHTYELLOW,
LIGHTWHITE
};
enum KEY_PRESSED //Phim dieu khien
{
KEY_UP ,
KEY_DOWN ,
KEY_LEFT ,
KEY_RIGHT ,
ENTER ,
ESCAPE ,
PAUSE ,
UNKNOWN
};
void setTextColor(WORD color) //doi mau chu
{
HANDLE hConsoleOutput;
hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO screen_buffer_info;
GetConsoleScreenBufferInfo(hConsoleOutput, &screen_buffer_info);
WORD wAttributes = screen_buffer_info.wAttributes;
color &= 0x000f;
wAttributes &= 0xfff0;
wAttributes |= color;
SetConsoleTextAttribute(hConsoleOutput, wAttributes);
}
int restart() //khoi dong lai chuong trinh, tra ve -1 khi thoat ctr vua goi,
{
char ownPth[MAX_PATH];
char absPth[MAX_PATH + 5] = ""; //duong dan co " "
// Will contain exe path
HMODULE hModule = GetModuleHandle(NULL);
if (hModule != NULL)
{
// When passing NULL to GetModuleHandle, it returns handle of exe itself
GetModuleFileName(hModule,ownPth, (sizeof(ownPth)));
strncat ( absPth, "\"", 1 );
strncat ( absPth, ownPth, MAX_PATH );
strncat ( absPth, "\"", 1 );
// std::cout << absPth;
// getch();
system( absPth ); //khoi dong lai
return -1 ;
}
else
{
std::cout << "Module handle is NULL!" << std::endl << "Restart manually!!" ;
return -1 ;
}
}
WORD key_press()
{
char key;
key = getch( );
if ( key == -32 )
{
switch( getch() )
{
case 72:
return KEY_UP;
case 80:
return KEY_DOWN;
case 75:
return KEY_LEFT;
case 77:
return KEY_RIGHT;
}
}
switch( key )
{
case 'a': case 'A':
return KEY_LEFT;
case 's': case 'S':
return KEY_DOWN;
case 'd': case 'D':
return KEY_RIGHT;
case 'w': case 'W':
return KEY_UP;
case 'p': case 'P':
return PAUSE;
case 13: case 32:
return ENTER;
case 27:
return ESCAPE;
}
return UNKNOWN; //ko co trong khai bao
}
void gotoxy(int x, int y) //chuyen con tro ve vi tri cot x, hang y
{
static HANDLE h = NULL;
if(!h)
h = GetStdHandle(STD_OUTPUT_HANDLE);
COORD c = { x, y };
SetConsoleCursorPosition(h,c);
}
bool confirm( char const action[] ) //xac nhan lua chon
{
system( "cls" );
char key;
gotoxy ( 20, 10 );
std::cout << "COMFIRM: " << (action) << " (Y/N) ";
while ( TRUE )
{
key = getch( ); //doc phim
if ( key == 'Y' || key == 'y' || key == 13 )
return TRUE;
else if ( key == 'N' || key == 'n' || key == 27 )
return FALSE;
}
}
void changeCursor( int oldPos, int newPos )
{
//Xoa con tro tuy chon o vi tri cu
gotoxy( 28, oldPos );
std::cout << " ";
gotoxy( 60, oldPos );
std::cout << " ";
//Ve lai con tro tuy chon o vi tri ms
gotoxy( 28, newPos );
std::cout << (char) 175 << (char) 175 << (char) 175;
gotoxy( 60, newPos );
std::cout << (char) 174 << (char) 174 << (char) 174;
}
//tao con tro trong Menu
//inp:
// begin : vi tri dau tien
// end : vi tri cuoi
// default : gia tri tra ve khi an ESC
//out: thu tu cua lựa chọn, từ trên xuống
int cursor ( int begin, int end, int defaut )
{
WORD KEY;
int cursor = begin;
changeCursor( begin + 2, begin ); //In ra con tro tuy chon
while ( 1 )
{
KEY = key_press( );
if ( KEY == ESCAPE )
return defaut;
else if ( KEY == ENTER )
return ( cursor - begin) / 2 + 1;
else if( KEY == KEY_DOWN ) //Di chuyen con tro xuong
{
//Thay doi toa do con tro tuy chon
if( cursor < end )
changeCursor ( cursor - 2, cursor += 2 );
else if ( cursor == end )
changeCursor ( end, cursor = begin ); //ve dau
}
else if( KEY == KEY_UP ) //Di chuyen con tro len
{
//Thay doi toa do con tro tuy chon
if( cursor > begin )
changeCursor ( cursor + 2, cursor -= 2 );
else if ( cursor== begin )
changeCursor ( begin, cursor = end ); //ve cuoi
}
}
}
#endif //__CONSOLE_H__