跳到正文

个人记录

用按钮拯救世界

其实我是蒟蒻约 7 分钟
原文
文章信息与版本

文章信息

发布
2019年6月16日
收录
2025年12月12日
更新
2026年10月12日
篇幅
约 7 分钟

版本

  1. v1当前
Write my name in front of the fire.\mathfrak{\color{red}{\boxed{\colorbox{red}{\color{white}{Write my name in front of the fire.}}}}}

如何用C++实现按钮功能?

点这里

。。。

1,头文件

C++
#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstdlib>
#include<windows.h>
#include<algorithm>
using namespace std;

#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
//不要问我这是什么

struct Button{//按钮类型
    int x,y,color;//按钮位置和颜色
    const char *name;//名字
    int len;//名字的长度
};

void GetPos(POINT &pt){//获得鼠标相对于屏幕的位置
//POINT是自带类型
    HWND hwnd=GetForegroundWindow();
    GetCursorPos(&pt);
    ScreenToClient(hwnd,&pt);
    pt.y=pt.y/16,pt.x=pt.x/16;//除以16,想不明白自己把它去掉试试
}

void color(int a){SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);}
//修改颜色
//想知道每个颜色的代码就for循环1到256看看
void gto(int x,int y)//将打字的光标移到x行y列
{
    COORD pos;pos.X=y*2;pos.Y=x;
    //必须反过来
    //y*2是因为汉字是2个字符
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}

Button NewButton(int x,int y,int color,const char *name){
    Button t;
    t.x=x,t.y=y,t.name=name;
    t.color=color;
    t.len=strlen(name);
    return t;//新建按钮,返回它
}

bool Preserve(Button A){
    //维护一个按钮
    //如果要使这个起作用必须在循环中不断执行它
    //详见后面的示例
    gto(A.x,A.y),color(A.color),printf("%s",A.name);
    POINT pt;
    GetPos(pt);
    if(pt.y==A.x&&(pt.x>=A.y&&pt.x<=A.y+A.len/2)){
        color(A.color+16),gto(A.x,A.y),printf("%s",A.name);
        if(KEY_DOWN(MOUSE_MOVED)) return 1;//检测到点击按钮
    }
    return 0;//没有检测到
}

从网上拿下来的。。。

你可以把ta保存为.h的形式存储在文件夹里

我把ta存在了D盘一个叫header的文件夹

然后无聊的写了一个剪刀石头布QWQ

C++
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <windows.h>
#include <algorithm>
#include <iostream>
#include <mmsystem.h>
#include <time.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)

#include<D:\header\cursor.h>//这部就是你存储的位置

using namespace std;
int main(){
	int duis,ans,p,y,s;
    Button A=NewButton(1,1,7,"准备好了吗,兄弟");
    while(1){
        if(Preserve(A)){
        	break; 
		} 
    	Sleep(100);
	} 
	system("cls"); 
	system("color 07");
	cout<<endl<<"这仅仅是一个石头剪刀布,只是为了测试按钮的功能"<<endl;
    A.name="ojbk";
    A.x=2;
    A.y=1;
    p=0;
    s=0;
    y=0;
	while(1){
        if(Preserve(A)){
        	break; 
		} 
    	Sleep(100);
	} 
	system("cls"); 
	system("color 07");
    Button B=NewButton(3,2,7,"石头");
    Button C=NewButton(3,6,7,"剪刀");
    Button D=NewButton(6,6,7,"再来一局");
    A.name="布";
    A.x=3;
    A.y=10;
    while(1){
    	cout<<"          出击吧"<<endl;
    	cout<<"      请选择您的手势"<<endl;
		srand(time(NULL));
		duis=rand()%3;
		while(1){
	        if(Preserve(A)){
	        	ans=1;
	        	break; 
			} 
	        if(Preserve(B)){
	        	ans=2;
	        	break; 
			} 
	        if(Preserve(C)){
	        	ans=0;
	        	break; 
			} 
			Sleep(100);
		}
		cout<<endl; 
		cout<<"    对手:";
		if(duis==1){
			cout<<"布";
		}
		if(duis==2){
			cout<<"石头";
		}
		if(duis==0){
			cout<<"剪刀";
		}
		cout<<"   ";
		cout<<"你:";
		if(ans==1){
			cout<<"布";
		}
		if(ans==2){
			cout<<"石头";
		}
		if(ans==0){
			cout<<"剪刀";
		}
		cout<<endl;
		cout<<"         ";
		if(duis==ans){
			cout<<"平局"<<endl; 
			p++;
		} 
		else if(ans==1&&duis==2){
			cout<<"你赢了"<<endl;
			y++;
		}
		else if(ans==2&&duis==0){
			cout<<"你赢了"<<endl;
			y++;
		}
		else if(ans==0&&duis==1){
			cout<<"你赢了"<<endl;
			y++;
		}
		else{
			cout<<"你输了"<<endl;
			s++;
		}
		cout<<endl;
		cout<<"   胜局数:"<<y<<" 平局数:"<<p<<" 败局数:"<<s<<endl;
		while(1){
			if(Preserve(D)) {
				break;
			}
			Sleep(100);
		}
		system("cls"); 
		system("color 07");
	}
    return 0;
}