zhiyin's button

· · 个人记录

一个实现按钮的程序,关键在于关掉快速编辑、屏幕和鼠标的输入

#include <iostream>
#include <windows.h>
using namespace std;

namespace basing{
    const int red=1,yellow=6,green=10,blue=3,purple=13,white=7,grey=8;
    const int bred=68,byellow=102,bgreen=170,bblue=17,bpurple=85,bwhite=119,bgrey=136;
    void color(int col){//锟斤拷锟斤拷锟斤拷锟斤拷锟缴?
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),col);
        return ;
    }
    void color_help(){//锟斤拷色锟斤拷锟斤拷 
        printf("\n");
        for (int i=0;i<256;i++){
            color(7);
            printf("%d ",i);
            color(i);
            printf("HELLO");
            color(7);
            printf("*\n");
        }
        return ;
    }
    void sleep(int t){//小写锟斤拷sleep 
        Sleep(t);
        return ;
    }
    void gotoxy(short x,short y){//锟斤拷锟矫癸拷锟轿伙拷锟?
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),{y-1,x-1});
        return ;
    }
    void settit(const char* p){//锟斤拷锟矫憋拷锟斤拷 
        SetConsoleTitle(p);
        return ;
    }
    short checkkey(char ch){//锟叫讹拷锟角凤拷锟铰硷拷锟斤拷-1为锟斤拷锟铰o拷0为没锟斤拷
        if ('a'<=ch&&ch<='z') ch+=0x1-'a';
        else if ('0'<=ch&&ch<='9') ch+=0x30-'0';
        else if (ch=='\n') ch+=0x0d-'\n';//enter 
        else if (ch=='^') ch=0x11;//Ctrl
        else if (ch=='%') ch=0x10;//Shift
        else if (ch=='&') ch=0x12;//Alt
        else if (ch=='L') return (GetKeyState(0x01)>>8||GetKeyState(0x05)>>8);//锟斤拷锟斤拷锟斤拷 
        else if (ch=='R') return ((GetKeyState(0x02)>>8)||(GetKeyState(0x06)>>8));
        return (GetKeyState(ch)>>8);
    }
    void wintop(bool b=1){//锟斤拷锟斤拷锟矫讹拷 锟斤拷锟斤拷锟斤拷0锟斤拷取锟斤拷锟矫讹拷 
        if (b==true) SetWindowPos(GetForegroundWindow(),HWND_TOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
        else{
            SetWindowPos(GetForegroundWindow(),HWND_BOTTOM,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
            SetWindowPos(GetForegroundWindow(),HWND_TOP,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
        }
        return ;
    }
    void winsize(int cx,int cy){//锟斤拷锟侥达拷锟节达拷小 
        SetWindowPos(GetForegroundWindow(),HWND_BOTTOM,0,0,cx,cy,SWP_NOZORDER|SWP_NOMOVE);
        return ;
    }
    void winpos(int x,int y){//锟斤拷锟侥达拷锟斤拷位锟斤拷
        SetWindowPos(GetForegroundWindow(),HWND_BOTTOM,x,y,0,0,SWP_NOZORDER|SWP_NOSIZE);
        return ;
    }
    void setxy(short high,bool is_not_hide=true){//锟斤拷锟矫癸拷锟竭度o拷1-100锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟截癸拷锟?
        CONSOLE_CURSOR_INFO cursor_info={high,is_not_hide}; 
        SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
        return ;
    }
} 

bool key_down(unsigned long vk) {
    return GetAsyncKeyState(vk) & 0x8000 ? true : false;
}
bool isdown(){
    bool ret=key_down(VK_LBUTTON);

    return ret;
}
bool isin(){
    int x1 = 7, x2 = 24, y1 = 32, y2 = 45;
    HWND h = GetConsoleWindow();
    POINT p;
    GetCursorPos(&p);
    RECT r;
    GetWindowRect(h,&r); 
    int x = p.x - r.left, y = p.y - r.top;
    using namespace basing;
//  gotoxy(5,5); cerr<<x<<' '<<y;
//  cerr<<"         ";
    return x <= x2 && x >= x1 && y <= y2 && y >= y1;
}
void guan(){
    HANDLE h = GetStdHandle(STD_INPUT_HANDLE);
    DWORD tmp;
    GetConsoleMode(h,&tmp);
    tmp &= ~ENABLE_QUICK_EDIT_MODE;
    tmp &= ~ENABLE_INSERT_MODE;
    tmp &= ~ENABLE_MOUSE_INPUT;
    SetConsoleMode(h,tmp);
    return ;
}
int main() {
    guan();
    HWND hWnd = GetConsoleWindow();

    basing::winpos(0, 0);
    basing::winsize(1000, 1000);

    basing::color(basing::bwhite);
    printf("##");
    basing::color(basing::white);

    //system("cls");
  //  return 0;
  using namespace basing;
 // while(1){
 //     isin();
 // }
    while(1) {
    //  gotoxy(10,10); cerr<<"down: "<<isdown();
    //  gotoxy(20,20); cerr<<"in: "<<isin();


        while(isdown()){
            if(isin()){
                while(isdown()&&isin());
                if(isin()) return 6;
                else break;
            }
            else{
                while(isdown());
                break;
            }
        }
    }

}