控制台控制
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
#include "c_WinUI_Console.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
static void HandleMouse(MOUSE_EVENT_RECORD Event) {
|
||||
char s[150];
|
||||
snprintf(s, sizeof(s)/sizeof(s[0]), "Buttons: %lu, X:%2lu Y:%2lu\n", Event.dwButtonState,
|
||||
Event.dwMousePosition.X, Event.dwMousePosition.Y);
|
||||
c_WinUI_Console_GotoXY(0, 0);
|
||||
c_WinUI_Console_WriteStr(s);
|
||||
}
|
||||
|
||||
static void HandleKey(KEY_EVENT_RECORD Event) {
|
||||
char s[100];
|
||||
snprintf(s, sizeof(s)/sizeof(s[0]), "You pressed: %c", Event.uChar);
|
||||
c_WinUI_Console_GotoXY(0, 1);
|
||||
c_WinUI_Console_WriteStr(s);
|
||||
snprintf(s, sizeof(s)/sizeof(s[0]), "Virtual Key: %3d", Event.wVirtualKeyCode);
|
||||
c_WinUI_Console_GotoXY(0, 2);
|
||||
c_WinUI_Console_WriteStr(s);
|
||||
}
|
||||
|
||||
static void SayGoodBye(void) {
|
||||
char s[100];
|
||||
c_WinUI_Console_ClearScreen();
|
||||
printf("Enter Your name:");
|
||||
gets(s);
|
||||
printf("\nGoodbye %s\n", s);
|
||||
}
|
||||
|
||||
static void InitConsoleInput(void) {
|
||||
HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
|
||||
if (hIn == INVALID_HANDLE_VALUE) return;
|
||||
|
||||
DWORD mode;
|
||||
// 1. 获取当前控制台的输入模式
|
||||
if (GetConsoleMode(hIn, &mode)) {
|
||||
|
||||
// 2. 启用鼠标输入
|
||||
mode |= ENABLE_MOUSE_INPUT;
|
||||
|
||||
// 3. 禁用快速编辑模式(非常关键!如果不禁用,鼠标事件会被控制台自身拦截用于复制文本)
|
||||
mode &= ~ENABLE_QUICK_EDIT_MODE;
|
||||
|
||||
// 4. 启用扩展属性(如果要修改 QUICK_EDIT,必须同时带上这个标志)
|
||||
mode |= ENABLE_EXTENDED_FLAGS;
|
||||
|
||||
// 5. 应用新模式
|
||||
SetConsoleMode(hIn, mode);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char** argv){
|
||||
DWORD Result;
|
||||
INPUT_RECORD Buffer;
|
||||
|
||||
InitConsoleInput();
|
||||
|
||||
HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
|
||||
|
||||
c_WinUI_Console_ClearScreen();
|
||||
c_WinUI_Console_GotoXY(0, 24);
|
||||
|
||||
c_WinUI_Console_WriteStr("Move mouse, press keys, double click to Exit...");
|
||||
|
||||
do {
|
||||
ReadConsoleInput(hIn, &Buffer, 1, &Result);
|
||||
|
||||
if (Buffer.EventType == KEY_EVENT) {
|
||||
HandleKey(Buffer.Event.KeyEvent);
|
||||
}
|
||||
|
||||
if (Buffer.EventType == MOUSE_EVENT) {
|
||||
HandleMouse(Buffer.Event.MouseEvent);
|
||||
}
|
||||
}while (!(Buffer.EventType==MOUSE_EVENT && Buffer.Event.MouseEvent.dwEventFlags==DOUBLE_CLICK));
|
||||
|
||||
SayGoodBye();
|
||||
|
||||
getchar();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
#include <c_WinUI_Console.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define ALT1 (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY)
|
||||
|
||||
int main(int argc, char** argv){
|
||||
int i;
|
||||
SetConsoleTitle("Emily Dickinson Console");
|
||||
c_WinUI_Console_ClearScreen();
|
||||
|
||||
c_WinUI_Console_WriteXY(0, 1, "Faith is a fine invention.", ALT1);
|
||||
c_WinUI_Console_WriteXY(0, 2, "For gentlemen who see;", ALT1);
|
||||
c_WinUI_Console_WriteXY(0, 3, "But microscopes are prudent", ALT1);
|
||||
c_WinUI_Console_WriteXY(0, 4, "In an emergency!", ALT1);
|
||||
|
||||
getchar();
|
||||
// system("pause");
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user