zoukankan      html  css  js  c++  java
  • c++运行程序 鼠标点击按钮 (c++)(windows)

    简介

    这是在黑漆漆的程序中,制造用户可点击的按钮,来决定程序下一步该作什么,的基本代码。

    详解

    头文件

    <cstdio><windows.h>

    结构体

    //这不全别复制
    struct Button
    {
    	int x,y,lenx,leny,color1,color0;
    	char * s;
    	bool nowpoint;
    };
    

    xy是按钮左上角相对于当前窗口的位置(左右是x,上下是y
    lenxleny按钮的长与宽(左右是x,上下是y
    color1color0是鼠标否或是在按钮上时按钮的颜色
    颜色详见 这个
    s是输出的一个串
    例如lenx=3,leny=3的按钮,是

    [ egin{matrix} a b c \ d e f \ g h i \ end{matrix} ]

    s"abcdefghi"
    nowpoint不用管,用户用不到。

    功能

    代码 功能
    Button a 新建一个按钮,默认为"none"
    a.Print(···) 输出a
    a.SetButton(···) 初始化并输出,返回时为串的长度是(1)否(0)符合规定
    a.Check(···) 判断鼠标是(1)否(0)点击的a按钮
    a.Delete(···) 删去a按钮,加引号的删除

    细节

    在Check的GetAsyncKeyState中
    可选VK_RBUTTON(鼠标右键)或VK_LBUTTON(鼠标左键)
    由于win10按左键会触发选择而不是按左键,所以win10推荐用右键
    Nowcolor是当前控制台的颜色(详见这个

    代码

    #include <cstdio>
    #include <windows.h> 
    
    using namespace std;
    
    const HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
    
    struct Button
    {
    	int x,y,lenx,leny,color1,color0;
    	char * s;
    	bool nowpoint;
    	Button () ;
    	void Print(bool a=1,int Nowcolor=7);
    	bool SetButton (int x,int y,int lenx,int leny,int color1,int color0,const char s[],int Nowcolor=7);
    	bool Check(int Nowcolor=7);
    	void Delete(); 
    };
    
    Button::Button () 
    {
    	x=y=0;
    	lenx=4;
    	leny=1;
    	color1=7;
    	color0=0x70;
    	nowpoint=0;
    	s=new char [4];
    	* s=* (s+2)='n';
    	* (s+1)='o';
    	* (s+3)='e';	
    }
    
    void Button::Print(bool a,int Nowcolor)
    {
    	if(a) SetConsoleTextAttribute(handle,this->color1);
    	else SetConsoleTextAttribute(handle,this->color0);
    	for(int i=0;i<this->leny;++i)
    	{
    		COORD pos = {this->x,this->y+i};
        	SetConsoleCursorPosition(handle,pos); 
    		for(int j=0;j<this->lenx;++j)
    		{
    			printf("%c",*(this->s+i*lenx+j));
    		}
    	}
    	SetConsoleTextAttribute(handle,Nowcolor);
    }
    
    bool Button::SetButton (int x,int y,int lenx,int leny,int color1,int color0,const char s[],int Nowcolor)
    {
    	this->x=x;
    	this->y=y;
    	this->lenx=lenx;
    	this->leny=leny;
    	this->color1=color1;
    	this->color0=color0;
    	delete [] this->s;
    	int lens=strlen(s);
    	this->s=new char [lens];
    	for(int i=0;i<=lens;++i)
    		* (this->s+i)=s[i];
    	this->Print(1,Nowcolor);
    	if(lens!=lenx*leny) return 0;
    	else return 1;
    }
    
    bool Button::Check(int Nowcolor)
    {
    	POINT APOINT;
        GetCursorPos(&APOINT);
        ScreenToClient(GetForegroundWindow(),&APOINT);
        APOINT.y=APOINT.y/16;
    	APOINT.x=APOINT.x/8;
    	if(APOINT.x>=this->x&&APOINT.x<=this->x+this->lenx-1&&APOINT.y>=this->y&&APOINT.y<=this->y+this->leny-1)
    	{
    		if(this->nowpoint)
    		{
    			this->Print(0,Nowcolor);
    			this->nowpoint=0;
    		}
    		if((GetAsyncKeyState(VK_RBUTTON) & 0x8000) ? 1:0) return 1;
    	}
    	else
    	{
    		if(!this->nowpoint)
    		{
    			this->Print(1,Nowcolor);
    			this->nowpoint=1;
    		}
    	}
    	return 0;
    }
    
    void Button::Delete()
    {
    	for(int i=0;i<this->leny;++i)
    	{
    		COORD pos = {this->x,this->y+i};
        	SetConsoleCursorPosition(handle,pos); 
    		for(int j=0;j<this->lenx;++j)
    		{
    			printf(" ");
    		}
    	}
    	delete [] this->s;	
    }
    
    int main ()
    {
    	Button a;
    	a.SetButton(1,10,4,1,7,0x70,"abcd");
    	while(1)
    	{
    		if(a.Check())break;
    	}
    	a.Delete();
    	return 0;	
    }
    
  • 相关阅读:
    ubuntu修改文件访问权限
    ubuntu使用root账户登录
    centos7 列出所有系统服务
    virtualbox 虚拟机硬盘扩容
    CI的意思
    更改centos 7 的默认启动为命令界面
    git Staging Deleted files
    Linux中变量$#,$@,$0,$1,$2,$*,$$,$?的含义
    List of data structures:数据结构大全
    List of algorithms:算法大全
  • 原文地址:https://www.cnblogs.com/Srand-X/p/13297693.html
Copyright © 2011-2022 走看看