dev-c++實現類按鍵互動
阿新 • • 發佈:2021-07-21
#include<bits/stdc++.h> #include<windows.h> #include<conio.h> #include <assert.h> #include <tchar.h> #define Key_Down(VK_NONAME) ((GetAsyncKeyState(VK_NONAME)&0x8000)?1:0) //#define Sleep(x) Sleep(x/15) //#define Sleep(x) Sleep(x/2) using namespace std; //位置結構體 CONSOLE_SCREEN_BUFFER_INFO weizi; //游標結構體 CONSOLE_CURSOR_INFO kzgqx; //滑鼠結構體 CONSOLE_FONT_INFO console; //獲取控制檯 HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE); //視窗資訊 HWND hw=GetForegroundWindow(); POINT p; struct HH{ int X,Y; }post; void Window(int x,int y){//視窗大小 SMALL_RECT rc={1,1,x,y}; //重置視窗位置和大小 GetConsoleScreenBufferInfo(handle,&weizi); //獲取視窗緩衝區資訊 SetConsoleWindowInfo(handle,1,&rc); } void Wind(int x,int y){//緩衝區大小 COORD size={x,y}; //位置結構體 GetConsoleScreenBufferInfo(handle,&weizi); //獲取視窗緩衝區資訊 SetConsoleScreenBufferSize(handle,size); //重新設定緩衝區大小 } void Cursorj(bool x){//游標見 GetConsoleCursorInfo(handle,&kzgqx); //獲取控制檯游標資訊 kzgqx.bVisible=x; //隱藏開啟控制檯游標 SetConsoleCursorInfo(handle,&kzgqx); } void Cursorw(){//游標位 GetConsoleScreenBufferInfo(handle,&weizi); //獲取游標位置 post.X=weizi.dwCursorPosition.X; post.Y=weizi.dwCursorPosition.Y; } inline void Cursorg(int x,int y){//游標改 COORD size={x,y}; SetConsoleCursorPosition(handle,size); } inline void Color(int x){//字元背景顏色 SetConsoleTextAttribute(handle,x);} char a[5][250]; int len[5],pt=0; bool flag=0; void test(){ Cursorj(0);flag=1; Cursorg(24,18); Color(12);printf("登入中..."); Sleep(2000); system("cls"); Cursorg(24,12);printf("登入成功!"); Sleep(2000);Color(15); const TCHAR szOperation[]=_T("open"); const TCHAR szAddress[]=_T("https://www.cnblogs.com/zhdzh/"); int WINAPI WinMain(HINSTANCE hInst,HINSTANCE,LPSTR lpCmd); { HINSTANCE hRslt=ShellExecute(0,szOperation,szAddress,0,0,SW_SHOWNORMAL); assert(hRslt>(HINSTANCE)HINSTANCE_ERROR); exit(100); } } int main(){ Color(112);Cursorj(0); Cursorg(15,4);printf("賬號: "); Cursorg(15,8);printf("密碼: "); Color(124);Cursorg(24,14);printf("確定!"); Color(127); while(1){ Sleep(10); if(!pt)while(_kbhit())_getch(); if(flag)break; if(_kbhit()&&pt){ char ss=getche(); if(ss==13||ss==10){ if(pt==1){ if(len[1]){ pt++; Cursorg(20+len[pt],8); } else Cursorg(20,4); } else{ if(len[2])test(); else Cursorg(20,8); } continue; } else if(ss==8){ --len[pt]; printf(" "); Cursorg(20+len[pt],pt<<2); continue; } a[pt][++len[pt]]=ss; } if(Key_Down(VK_LBUTTON)){ GetCursorPos(&p); ScreenToClient(hw,&p); GetCurrentConsoleFont(handle,0,&console); int x=p.x/=console.dwFontSize.X; int y=p.y/=console.dwFontSize.Y; if((y==4||y==8)&&(x>=20&&x<=40)){ Cursorj(1);Color(12); Cursorg(24,18);printf(" "); if(y==4)pt=1; else pt=2; Cursorg(41,pt<<2);printf(" "); Cursorg(20+len[pt],pt<<2); Color(127); } else if(y==14&&(x>=24&&x<=29)){ Cursorj(0); if(len[1]&&len[2]){ test();continue; } else { if(!len[2]){ Cursorg(24,18); Color(12);printf("請輸入密碼!"); Cursorg(41,8);printf("×"); } if(!len[1]){ Cursorg(24,18); Color(12);printf("請輸入賬號!"); Cursorg(41,4);printf("×"); } } } else { pt=0; Cursorj(0); } } } exit(0); }