C++控制台鼠标化操作
C++控制臺鼠標化教程二
一個學習c++的人,難免會做億些小游戲來消遣時間,在這里,我講解一下如何讓你的控制臺游戲完全鼠標化.
首先,先給大家看一下執行鼠標操作的必備頭文件
#include<windows.h> #define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0) CONSOLE_FONT_INFO consoleCurrentFont;然后進入正題
首先我們需要解除控制臺的快速編譯功能,并且隱藏控制臺光標(就是程序運行時那個一閃一閃的東西),下面是對應代碼
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); CONSOLE_CURSOR_INFO cursor_info={1,0};SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);到這里,你的開始準備工作還沒有做完!
你需要進行一下鼠標矯正
struct {LONG x=-1,y=-1,lest_x=-1,lest_y=-1;double real_x,real_y;POINT pt= {0,0}; void check_mouse() { pt.y=pt.y/real_y,pt.x=(pt.x/real_x)/**2*/;if(pt.y<0)pt.y=0;if(pt.x<0)pt.x=0;}void mouse(){SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),15);GetCursorPos(&pt); //獲取鼠標當前位置 ScreenToClient(FindWindow("ConsoleWindowClass", NULL), &pt); check_mouse();lest_y=y,lest_x=x;x=pt.x,y=pt.y;SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),7);} void get_real_mouse(){basic.cls();printf("鼠標校正,請用鼠標點擊右下角的\"Hi\"");int x=rand()%10+35,y=rand()%10+75;basic.gt(x,y);printf("Hi");while(1){GetCursorPos(&pt); //獲取鼠標當前位置ScreenToClient(FindWindow("ConsoleWindowClass", NULL), &pt); if(KEY_DOWN(VK_LBUTTON)&&MessageBox(0,"您確定您點擊了\"Hi\"嗎?\n如果您沒有點擊此點可能會導致程序錯亂.","鼠標校正",4)==IDYES){real_x=pt.x/y,real_y=pt.y/x;break;}}basic.wait(500); basic.cls();} }mouse;里面的"?void get_real_mouse()?"就是上述的鼠標定位.
然而你把它拷貝到編譯器中,編譯器會提示你?缺少一些東西
是的,的確缺少.所以我補一下那些缺少的部分
struct { void color(int all){SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),all);}void wait(int all){Sleep(all);}void cls(){system("cls");}void b(short hz,double time){Beep(hz,time);}void gt(int x,int y) {COORD coord;coord.X=y;//Here, the order is reversed, otherwise, the output pointer is moved to Y row and X columncoord.Y=x;HANDLE a=GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleCursorPosition(a,coord);} }basic;好了準備工作完畢!
接下來進入到最主要的環節
如何使用?
我給大家寫了一個如何使用它例子
int choose(void) {string whats[10]={},title="",about_things="null";short how_long=0,wait_=1;for(int i=1;i<=9;i++){if(whats[i]!="")how_long++;else break;}int choose; while(1) {choose=-1; basic.gt(1,0);mouse.mouse();if(mouse.y==1)choose=1;printf("%s",title.c_str()); if(choose==1){basic.color(16+7);printf("(?)\n");} else {basic.color(7);printf(" \n");} short x=1;for(int i=1;i<=how_long;i++){x++;if(mouse.y==x)choose=x;if(choose==i+1)basic.color(16*15);else basic.color(7);printf("%s\n",whats[i].c_str()); }basic.color(0);printf("\n---"); if(wait_==1){basic.wait(500);wait_=0;}if(KEY_DOWN(VK_LBUTTON)&&choose>1)break;else if(KEY_DOWN(VK_LBUTTON)&&choose==1){string about="關于"+title;MessageBox(0,about_things.c_str(),about.c_str(),MB_OK);} }for(int i=1;i<=9;i++)whats[i]="";title="",about_things="null";basic.color(7);basic.cls();return choose-1; }我講解一下這個代碼的用途?string whats[10]={},title="",about_things="null";
這個是按鍵內容(string whats[10]),選擇界面的標題(string tittle),和關于標題的介紹(string about_things)
其余的就是基礎代碼了大家可以自行理解?其實我懶得打字
這個大體的運行效果就是顯示一排按鍵,然后當你點擊其中任意一個按鍵后會返回一個對應按鍵的值
最后
我把這些代碼連接在一起,方便大家白嫖學習
#include<bits/stdc++.h> #include<ctime> #include<windows.h> #include<stdio.h> #include<conio.h> #include<iostream> #include<cstdio> #include<time.h> #define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0) CONSOLE_FONT_INFO consoleCurrentFont; using namespace std; string user_name; struct {void color(int all){SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),all);}void wait(int all){Sleep(all);}void cls(){system("cls");}void b(short hz,double time){Beep(hz,time);}void gt(int x,int y){COORD coord;coord.X=y;//Here, the order is reversed, otherwise, the output pointer is moved to Y row and X columncoord.Y=x;HANDLE a=GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleCursorPosition(a,coord);} }basic; struct {LONG x=-1,y=-1,lest_x=-1,lest_y=-1;double real_x,real_y;HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);POINT pt= {0,0};void check_mouse(){//GetCurrentConsoleFont(hOutput, FALSE, &consoleCurrentFont); //獲取字體信息//pt.y=((pt.y/=consoleCurrentFont.dwFontSize.Y)/1.2)+1;//pt.x=(pt.x/=consoleCurrentFont.dwFontSize.X)/1.25;pt.y=pt.y/real_y,pt.x=(pt.x/real_x)/**2*/;if(pt.y<0)pt.y=0;if(pt.x<0)pt.x=0;}void mouse(){SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),15);GetCursorPos(&pt); //獲取鼠標當前位置ScreenToClient(FindWindow("ConsoleWindowClass", NULL), &pt);check_mouse();lest_y=y,lest_x=x;x=pt.x,y=pt.y;SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),7);}void get_real_mouse(){basic.cls();printf("鼠標校正,請用鼠標點擊右下角的\"Hi\"");int x=rand()%10+35,y=rand()%10+75;basic.gt(x,y);printf("Hi");while(1){GetCursorPos(&pt); //獲取鼠標當前位置ScreenToClient(FindWindow("ConsoleWindowClass", NULL), &pt);if(KEY_DOWN(VK_LBUTTON)&&MessageBox(0,"您確定您點擊了\"Hi\"嗎?\n如果您沒有點擊此點可能會導致程序錯亂.","鼠標校正",4)==IDYES){real_x=pt.x/y,real_y=pt.y/x;break;}}basic.wait(500); basic.cls();} }mouse; struct {//移動庫int num_get(){int num=0,ans=1,fu=0;int life[10]={};short now_key,key,lk;for(int i=1;i<=5;i++){now_key=(int)getch()-48;key=now_key+48;if(key==13)break;else if(now_key==-40){if(lk!=49){i=0,ans=0,num=0,fu=0;for(int q=0;q<=9;q++)life[q]=0;printf("(重新輸入)");}}else if(key==45){ fu=1,i--;printf("(此數已經設定為負數)");}else if(now_key>9||now_key<0)i--;else printf("%i",now_key);lk=now_key;life[i]=now_key,ans++;}int d=1;//for(int i=1;i<=ans;i++)d=d*10;for(int i=ans;i>=1;i--){num=num+life[i]*d;d=d*10;}if(fu==1)num=num*-1;return num/10;}string whats[10]={},title="",about_things="null";int ai_choose(void){basic.cls();short how_long=0,wait_=1;for(int i=1;i<=9;i++){if(whats[i]!="")how_long++;else break;}int choose;while(1){choose=-1;basic.gt(1,0);mouse.mouse();if(mouse.y==1)choose=1;printf("%s",title.c_str());if(choose==1){basic.color(16+7);printf("(?)\n");}else {basic.color(7);printf(" \n");}short x=1;for(int i=1;i<=how_long;i++){x++;if(mouse.y==x)choose=x;if(choose==i+1)basic.color(16*15);else basic.color(7);printf("%s\n",whats[i].c_str());}if(wait_==1){basic.wait(500);wait_=0;}if(KEY_DOWN(VK_LBUTTON)&&choose>1)break;else if(KEY_DOWN(VK_LBUTTON)&&choose==1){string about="關于"+title;if(about!="null")MessageBox(0,about_things.c_str(),about.c_str(),MB_OK);}}for(int i=1;i<=9;i++)whats[i]="";title="",about_things="null";basic.cls();return choose-1;} }choose; int main() {system("mode con cols=150 lines=40");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);CONSOLE_CURSOR_INFO cursor_info={1,0};SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);mouse.get_real_mouse();while(1){choose.about_things="開始界面選擇",choose.title="有什么好看的?",choose.whats[1]="開始游戲", choose.whats[2]="打開Pearl Harbor War官網", choose.whats[3]="關閉游戲";short a=choose.ai_choose();cout<<a;} }小結: 此鼠標操作有一個缺點:當你完成鼠標定位后不能修改控制臺字體大小,一旦修改鼠標定位將失靈
大家拿走前請標明出處,感謝大家的配合!
文章來源:關于c++控制臺鼠標化操作 - bailiwen/heveral(白.)的博客 - 洛谷博客
------
注:洛谷用戶 bailiwen ,CDSN中的heveral ,NOI中的heveral(白.)均屬于同一人
?
?
總結
以上是生活随笔為你收集整理的C++控制台鼠标化操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 有哪些适合女生练字的字帖?
- 下一篇: Teams Bot开发系列:Middle