UPD2023年7月28日16:15:17修复了只输入0但是无法显示的bug
#include<bits/stdc++.h>
#include<conio.h>
#include<windows.h>
#include<io.h>
using namespace std;
void GPT(POINT &pt) {
GetCursorPos(&pt);
ScreenToClient(FindWindow("ConsoleWindowClass", NULL), &pt);
}
void GT(int x,int y){//GT是 G o T oxy的缩写,写代码方便
COORD coord;
coord.X=y;//这里调换了顺序,否则就是将输出指针移到y行,x列
coord.Y=x;
HANDLE a=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(a,coord);
}
void colour(int Fx,int Gx,bool If_FHighlight,bool If_GHighlight){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),Fx|Gx|(If_FHighlight?8:0)|(If_GHighlight?128:0));
}
void clear_buffer(){//很重要的函数——清空缓冲区
while(kbhit())
if(getch()!=EOF);
for(int i=1;i<=256;++i)
if(GetAsyncKeyState(i));
}
int ConPosx,ConPosy;//字体大小,分别是长,高
RECT Rect;//存储窗口坐标
HWND hwnd=GetForegroundWindow();
void GetCon(){
HANDLE hOutput;
hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_FONT_INFO consoleCurrentFont;
GetCurrentConsoleFont(hOutput, FALSE, &consoleCurrentFont);
//printf("通过GetCurrentConsoleFont()函数获取到的控制台当前字体长度是%d, 高度是%d.\n",consoleCurrentFont.dwFontSize.X,consoleCurrentFont.dwFontSize.Y);
ConPosx=consoleCurrentFont.dwFontSize.X;
ConPosy=consoleCurrentFont.dwFontSize.Y;
}
void HideCursor(){
CONSOLE_CURSOR_INFO cursor_info={1,0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
}
class MESSAGE{//鼠标类
public:
int x,y;//储存第一的字符的控制台坐标
int length,height;//储存被判区域的长度和宽度“都是多少字符”
bool Is_Touching;//(1是0否)被鼠标触碰
POINT pt;//存储鼠标
short Left,Right,Top,Bottom;//存储字符串的可点击范围
MESSAGE(int x=0,int y=0,int length=0,int height=0){
this->x=x;
this->y=y;
this->length=length;
this->height=height;
}
void Army(short x,short y,short length,short height){
this->x=x;
this->y=y;
this->length=length;
this->height=height;
}
bool If_Touching(){
Left=y*ConPosx;
Right=Left+length*ConPosx;
Top=x*ConPosy;
Bottom=Top+height*ConPosy;
GPT(pt);
return pt.x>=Left&&pt.x<Right&&pt.y>=Top&&pt.y<Bottom;
}
short Be_Clicked(){
if(If_Touching()){
if(GetAsyncKeyState(1))//左键
return 1;
else if(GetAsyncKeyState(2))//右键
return 2;
else if(GetAsyncKeyState(4))//中键
return 3;
else//没按键
return 0;
}
return 0;
}
};
MESSAGE cur;//共用一个
class InPutWindow{//输入窗口类,目前只适合数字
public:
int x,y,length;//位置,长度
string st;//内容
InPutWindow(int x=0,int y=0,int length=0,string st=""){
this->x=x;
this->y=y;
this->length=length;
this->st=st;
}
bool Be_Clicked(){//被按下就要启动修改
cur.Army(x,y,length,1);
return cur.Be_Clicked()==1;
}
void Reput(char ch){//重写48~57
keybd_event(ch,(BYTE)0,0,0);
keybd_event(ch,(BYTE)0,2,0);
}
void InPut(){
//分为两步,一是重写之前的数字,二是接受新数字
clear_buffer();//先清空缓冲区
for(int i=0;st[i];++i)
Reput(st[i]);
st.clear();
GT(x,y);
for(int i=0;i<length;++i)
putchar(' ');
//此处不清空缓冲区,此后开启输入板块即可回显这些字符
char ch;int count=-1;
ch=getch();//用不回显就是为了满足删除键(8)、回车键(13)和长度限制,我们稍后采取人工补充输出
while(ch!=13){//13是回车键的ASCII
if(ch==8&&count>=0){
st.pop_back();
count--;
GT(x,y+count+1);
putchar(' ');
}
if(((ch>=48&&ch<=57&&st[0]!='0'))&&count<length-1){//排除首位0
st+=ch;count++;
GT(x,y+count);
putchar(ch);
}
ch=getch();
}
}
void Put(){//这个是带装饰的,只输出字符直接用cout<<mess.st
GT(x,y);
cout<<st;
GT(x-1,y-1);
for(int i=0;i<=length+1;++i)
printf("=");
GT(x+1,y-1);
for(int i=0;i<=length+1;++i)
printf("=");
GT(x,y-1);printf("|");GT(x,y+length);printf("|");
}
};
int main(){
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode;
GetConsoleMode(hStdin, &mode);
mode &= ~ENABLE_QUICK_EDIT_MODE; //移除快速编辑模式
mode &= ~ENABLE_INSERT_MODE; //移除插入模式
mode &= ~ENABLE_MOUSE_INPUT;
SetConsoleMode(hStdin, mode);
GetCon();
GetWindowRect(hwnd,&Rect);
InPutWindow mess(2,5,10,"4956");
GT(0,5);
printf("点击输入框已上线!点击输入框即可输入/修改数字,回车结束,支持删除键");
mess.Put();
while(1){
if(mess.Be_Clicked()){
mess.InPut();
mess.Put();
}
}
return 0;
}