在终端中获取鼠标
东东 Lv4

介绍如何在 Windows 中通过 C 实现终端获取鼠标坐标

注: 编译后需要右击最上面的边框->属性->选项, 去掉快速编辑模式(可能需要重新启动程序?)

main.c

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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <stdbool.h>

void gotoxy(int x, int y);
void setcolor(WORD color);
void setForeGroundAndBackGroundColor(int ForeGroundColor, int BackGroundColor);
void clearscreen();
void drawpixel(unsigned char x, unsigned char y, unsigned char Color);
void drawpixel2(unsigned char x, unsigned char y, unsigned char Color, char character);
void drawcircle(int x, int y, int a, int b, int color);
void drawline(int x0, int y0, int x1, int y1, int color);
void drawfilledrectangle(unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2, unsigned char bkcol);
void drawframe(unsigned x, unsigned y, unsigned sx, unsigned sy, unsigned char col, unsigned char col2, char text_[]);
void drawwindow(unsigned x, unsigned y, unsigned sx, unsigned sy, unsigned char col, unsigned char col2, unsigned char bkcol, char text_[]);

int main()
{
gotoxy(1, 23);
setcolor(7);
clearscreen();

printf("click anywhere in console window to write - hello world -\n\n\n\n\n\n\n\n\n\n\n\n\n"
"Press Ctrl+C to Exit");

HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE hin = GetStdHandle(STD_INPUT_HANDLE);
INPUT_RECORD InputRecord;
DWORD Events;
COORD coord;
CONSOLE_CURSOR_INFO cci;
cci.dwSize = 25;
cci.bVisible = FALSE;
SetConsoleCursorInfo(hout, &cci);
SetConsoleMode(hin, ENABLE_PROCESSED_INPUT | ENABLE_MOUSE_INPUT);
bool EXITGAME = false;
int buttonX = 1, buttonY = 1;

drawpixel(buttonX, buttonY, 1);
gotoxy(buttonX + 2, buttonY);
setcolor(3);
printf("<----- a button \n");

while (!EXITGAME)
{

ReadConsoleInput(hin, &InputRecord, 1, &Events);

switch (InputRecord.EventType)
{
case KEY_EVENT: // keyboard input

switch (InputRecord.Event.KeyEvent.wVirtualKeyCode)
{
case VK_ESCAPE:
EXITGAME = TRUE;
break;

case VK_SPACE:

break;

case VK_RETURN:

break;

case VK_LEFT:
// left key move player left
printf("VK_LEFT = %d\n", InputRecord.Event.KeyEvent.wVirtualKeyCode);

break;

case VK_RIGHT:
// right key move player right
printf("VK_RIGHT = %d\n", InputRecord.Event.KeyEvent.wVirtualKeyCode);

break;

case VK_UP:
// up key move player up
printf("VK_UP = %d\n", InputRecord.Event.KeyEvent.wVirtualKeyCode);

break;

case VK_DOWN:
// up key move player down
printf("VK_DOWN = %d\n", InputRecord.Event.KeyEvent.wVirtualKeyCode);

break;

} // switch

//---------------------------------------------------------------------------------
break;

case MOUSE_EVENT: // mouse input

if (InputRecord.Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)
{
coord.X = InputRecord.Event.MouseEvent.dwMousePosition.X;
coord.Y = InputRecord.Event.MouseEvent.dwMousePosition.Y;
SetConsoleCursorPosition(hout, coord);
SetConsoleTextAttribute(hout, rand() % 7 + 9);

if ((InputRecord.Event.MouseEvent.dwMousePosition.X == buttonX) &&
(InputRecord.Event.MouseEvent.dwMousePosition.Y == buttonY))
{

clearscreen();
gotoxy(1, 1);
setcolor(7);
drawpixel(buttonX, buttonY, 1);
setcolor(3);
printf(" mybutton was pressed \n");
setcolor(7);
Sleep(500);
drawpixel(buttonX, buttonY, 1);
gotoxy(buttonX + 2, buttonY);
setcolor(3);
printf("<----- a button \n");
}

printf("Hello world at %d x %d", InputRecord.Event.MouseEvent.dwMousePosition.X,
InputRecord.Event.MouseEvent.dwMousePosition.Y);

} // mouse

break;

case WINDOW_BUFFER_SIZE_EVENT: // scrn buf. resizing
;
break;

case FOCUS_EVENT: // disregard focus events

case MENU_EVENT: // disregard menu events

break;

default:
printf("Unknown event type \n");
break;
}

FlushConsoleInputBuffer(hin);
}
gotoxy(1, 23);
setcolor(7);
clearscreen();
printf("\n");
return 0;
}

void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
return;
}

//*****************************************************************************

void setcolor(WORD color)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
return;
}

//*****************************************************************************

//
// colors:
// 0 = Black
// 1 = Blue
// 2 = Green
// 3 = Cyan
// 4 = Red
// 5 = Magenta
// 6 = Yellow
// 7 = LightGray
// 8 = DarkGray
// 9 = LightBlue
// 10 = LightGreen
// 11 = LightCyan
// 12 = LightRed
// 13 = LightMagenta
// 14 = LightYellow
// 15 = White

//

//*****************************************************************************

void setForeGroundAndBackGroundColor(int ForeGroundColor, int BackGroundColor)
{
int color = 16 * BackGroundColor + ForeGroundColor;
setcolor(color);
}

//*****************************************************************************

void clearscreen()
{
COORD coordScreen = {0, 0};
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD dwConSize;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

GetConsoleScreenBufferInfo(hConsole, &csbi);
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
GetConsoleScreenBufferInfo(hConsole, &csbi);
FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
SetConsoleCursorPosition(hConsole, coordScreen);
return;
}

//*****************************************************************************

void drawpixel(unsigned char x, unsigned char y, unsigned char Color)
{
setcolor(Color);
gotoxy(x, y);
printf("Û");
}
//*****************************************************************************

void drawpixel2(unsigned char x, unsigned char y, unsigned char Color, char character)
{
setcolor(Color);
gotoxy(x, y);
printf("%c", character);
}

//*****************************************************************************

void drawcircle(int x, int y, int a, int b, int color)
{
int wx, wy;
int thresh;
int asq = a * a;
int bsq = b * b;
int xa, ya;

drawpixel(x, y + b, color);
drawpixel(x, y - b, color);

wx = 0;
wy = b;
xa = 0;
ya = asq * 2 * b;
thresh = asq / 4 - asq * b;

for (;;)
{
thresh += xa + bsq;

if (thresh >= 0)
{
ya -= asq * 2;
thresh -= ya;
wy--;
}

xa += bsq * 2;
wx++;

if (xa >= ya)
break;

drawpixel(x + wx, y - wy, color);
drawpixel(x - wx, y - wy, color);
drawpixel(x + wx, y + wy, color);
drawpixel(x - wx, y + wy, color);
}

drawpixel(x + a, y, color);
drawpixel(x - a, y, color);

wx = a;
wy = 0;
xa = bsq * 2 * a;

ya = 0;
thresh = bsq / 4 - bsq * a;

for (;;)
{
thresh += ya + asq;

if (thresh >= 0)
{
xa -= bsq * 2;
thresh = thresh - xa;
wx--;
}

ya += asq * 2;
wy++;

if (ya > xa)
break;

drawpixel(x + wx, y - wy, color);
drawpixel(x - wx, y - wy, color);
drawpixel(x + wx, y + wy, color);
drawpixel(x - wx, y + wy, color);
}
}

//*****************************************************************************

void drawline(int x0, int y0, int x1, int y1, int color)
{
int pix = color;
int dy = y1 - y0;
int dx = x1 - x0;
int stepx, stepy;

if (dy < 0)
{
dy = -dy;
stepy = -1;
}
else
{
stepy = 1;
}
if (dx < 0)
{
dx = -dx;
stepx = -1;
}
else
{
stepx = 1;
}
dy <<= 1; // dy is now 2*dy
dx <<= 1; // dx is now 2*dx

drawpixel(x0, y0, pix);
if (dx > dy)
{
int fraction = dy - (dx >> 1); // same as 2*dy - dx
while (x0 != x1)
{
if (fraction >= 0)
{
y0 += stepy;
fraction -= dx; // same as fraction -= 2*dx
}
x0 += stepx;
fraction += dy; // same as fraction -= 2*dy
drawpixel(x0, y0, pix);
}
}
else
{
int fraction = dx - (dy >> 1);
while (y0 != y1)
{
if (fraction >= 0)
{
x0 += stepx;
fraction -= dy;
}
y0 += stepy;
fraction += dx;
drawpixel(x0, y0, pix);
}
}
}

//*****************************************************************************

void drawframe(unsigned x, unsigned y, unsigned sx, unsigned sy, unsigned char col, unsigned char col2, char text_[])
{
unsigned i, j, m;
{

m = (sx - x); // differential
j = m / 8; // adjust
j = j - 1; // more adjustment
gotoxy(x, y);
printf("É"); // Top left corner of drawframe
gotoxy(sx, y);
printf("»"); // Top right corner of drawframe
gotoxy(x, sy);
printf("È"); // Bottom left corner of drawframe
gotoxy(sx, sy);
printf("¼"); // Bottom right corner of drawframe

for (i = x + 1; i < sx; i++)
{
gotoxy(i, y);
printf("Í"); // Top horizontol line
gotoxy(i, sy);
printf("Í"); // Bottom Horizontal line
}

for (i = y + 1; i < sy; i++)
{
gotoxy(x, i);
printf("º"); // Left Vertical line
gotoxy(sx, i);
printf("º"); // Right Vertical Line
}

gotoxy(x + j, y);
printf(text_); // put Title
gotoxy(1, 24);
}
}

//*****************************************************************************

void drawfilledrectangle(unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2, unsigned char bkcol)
{
int x, y;
setcolor(bkcol); // Set to color bkcol

for (y = y1; y < y2; y++)
{ // Fill Y Region Loop
for (x = x1; x < x2; x++)
{ // Fill X region Loop
gotoxy(x, y);
printf(" "); // Draw Solid space
}
}
}

//*****************************************************************************

void drawwindow(unsigned x, unsigned y, unsigned sx, unsigned sy,
unsigned char col, unsigned char col2, unsigned char bkcol, char text_[])
{
drawfilledrectangle(x, y, sx, sy, bkcol);
drawframe(x, y, sx, sy, col, col2, text_);
}

//*****************************************************************************

void drawcolorpalette()
{
for (int i = 0; i < 16; i++)
{
for (int j = 0; j < 16; j++)
{
setForeGroundAndBackGroundColor(i, j);
gotoxy(i * 4, j);
printf("%d", (i * j) + 1);
}
}
}

源文件来自于

 评论