扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
typedef struct _MOUSE_EVENT_RECORD { COORD dwMousePosition; // 当前鼠标位置 DWORD dwButtonState; // 鼠标按钮状态 DWORD dwControlKeyState; // 键盘控制键状态 DWORD dwEventFlags; // 事件状态 } MOUSE_EVENT_RECORD; |
#include <WINDOWS.H> #include <STDIO.H> #include <STRING.H> HANDLE hOut; HANDLE hIn; void ClearScreen(void); void DispMousePos(COORD pos); // 在最后一行显示鼠标位置 void main() { hOut = GetStdHandle(STD_OUTPUT_HANDLE); // 获取标准输出设备句柄 hIn = GetStdHandle(STD_INPUT_HANDLE); // 获取标准输入设备句柄 WORD att = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY | BACKGROUND_BLUE ; // 背景是蓝色,文本颜色是黄色 SetConsoleTextAttribute(hOut, att); ClearScreen(); // 清屏 INPUT_RECORD mouseRec; DWORD state = 0, res; COORD pos = {0, 0}; for(;;) // 循环 { ReadConsoleInput(hIn, &mouseRec, 1, &res); if (mouseRec.EventType == MOUSE_EVENT){ if (mouseRec.Event.MouseEvent.dwEventFlags == DOUBLE_CLICK) break; // 双击鼠标退出循环 pos = mouseRec.Event.MouseEvent.dwMousePosition; DispMousePos(pos); if (mouseRec.Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED) FillConsoleOutputCharacter(hOut, 'A', 1, pos, NULL); } } pos.X = 0; pos.Y = 0; SetConsoleCursorPosition(hOut, pos); // 设置光标位置 CloseHandle(hOut); // 关闭标准输出设备句柄 CloseHandle(hIn); // 关闭标准输入设备句柄 } void DispMousePos(COORD pos) // 在最后一行显示鼠标位置 { CONSOLE_SCREEN_BUFFER_INFO bInfo; GetConsoleScreenBufferInfo( hOut, &bInfo ); COORD home = {0, bInfo.dwSize.Y-1}; WORD att0 = BACKGROUND_INTENSITY ; FillConsoleOutputAttribute(hOut, att0, bInfo.dwSize.X, home, NULL); FillConsoleOutputCharacter(hOut, ' ', bInfo.dwSize.X, home, NULL); char s[20]; sprintf(s,"X = %2lu, Y = %2lu",pos.X, pos.Y); SetConsoleTextAttribute(hOut, att0); SetConsoleCursorPosition(hOut, home); WriteConsole(hOut, s, strlen(s), NULL, NULL); SetConsoleTextAttribute(hOut, bInfo.wAttributes); // 恢复原来的属性 SetConsoleCursorPosition(hOut, bInfo.dwCursorPosition); // 恢复原来的光标位置 } void ClearScreen(void) { CONSOLE_SCREEN_BUFFER_INFO bInfo; GetConsoleScreenBufferInfo( hOut, &bInfo ); COORD home = {0, 0}; unsigned long size = bInfo.dwSize.X * bInfo.dwSize.Y; FillConsoleOutputAttribute(hOut, bInfo.wAttributes, size, home, NULL); FillConsoleOutputCharacter(hOut, ' ', size, home, NULL); } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者