EASYX 一个测试用的BUTTON 类

#include <graphics.h>
class ThisButton {
protected:
	int _x;
	int _y;
	int _w;
	int _h;
	int _bg_color;
	int _font_color;
	const char *_text;
	void (*_func)();
	bool _mouse_hover=false;
	bool _mouse_clicked=false;
	
public:
	ThisButton(const char *t="NEW",void(* func)()=nullptr,int x=0,int y=0,int w=100,int h=30,int bg_color=BLUE,int font_color=WHITE) {
		_x=x;
		_y=y;
		_w=w;
		_h=h;
		_bg_color=bg_color;
		_font_color=font_color;
		_text=t;
		_func=func;
		On_Draw();
	}
	~ThisButton() {
		
	}
	void On_Input(const ExMessage *msg){
		Mouse_Hover(msg);
		Mouse_Clicked(msg);
		On_Draw();
	}
	void On_Draw(){
		setfillcolor(_bg_color);
		bar(_x,_y,_x+_w,_y+_h);
		int text_h=textheight(_text);
		int text_w=textwidth(_text);
		setbkmode(TRANSPARENT);
		settextcolor(_font_color);
		if(_mouse_hover) settextcolor(RED);
		
		outtextxy((_w-text_w)/2+_x,(_h-text_h)/2+_y,_text);
		
	}
	void Mouse_Hover(const ExMessage *msg) {
		if(msg->x>_x && msg->y>_y && msg->x<_x+_w && msg->y<_y+_h){
			_mouse_hover=true;
		}else{
			_mouse_hover=false;
		} 
		
		
	}
	void Mouse_Clicked(const ExMessage *msg){
		if(_mouse_hover && msg->message==WM_LBUTTONUP){
			_func();
		}
		
	}
};
class ThisEdit {
protected:
	int _x;
	int _y;
	int _w;
	int _h;
	int _bg_color;
	int _font_color;
	
	bool _mouse_hover=false;
	bool _mouse_clicked=false;
	int EnterX;
	int EnterY;
public:
	ThisEdit(int x=0,int y=0,int w=100,int h=30,int bg_color=BLUE,int font_color=WHITE) {
		_x=x;
		_y=y;
		_w=w;
		_h=h;
		EnterX=_x+5;
		EnterY=_y+5;
		_bg_color=bg_color;
		_font_color=font_color;
		
		On_Draw();
	}
	~ThisEdit() {
		
	}
	void Mouse_Hover(const ExMessage *msg) {
		if(msg->x>_x && msg->y>_y && msg->x<_x+_w && msg->y<_y+_h){
			_mouse_hover=true;
		}else{
			_mouse_hover=false;
		} 
		
		
	}
	void On_Input(const ExMessage *msg){
		Mouse_Hover(msg);
		Mouse_Clicked(msg);

		On_Draw();
	}
	void On_Draw(){
		setcolor(BLUE);
		rectangle(_x,_y,_x+_w,_y+_h);
		setbkmode(TRANSPARENT);
		settextcolor(_font_color);
	}
	void Mouse_Clicked(const ExMessage *msg) {
		if(_mouse_hover && msg->message==WM_LBUTTONUP){
			setcolor(YELLOW);
			rectangle(_x,_y,_x+_w,_y+_h);
			outtextxy(EnterX,EnterY,"|");
			
		}	
	}
};
void Button1_Clicked(){
	
	outtextxy(0,0,"Button1 clicked.");
}

int main(){
	initgraph(400,300);
	HWND Me=GetForegroundWindow();
	SetWindowPos(Me,HWND_TOPMOST,300,300,300,300, SWP_NOSIZE | SWP_SHOWWINDOW);
	ThisButton Button1("Button",Button1_Clicked,20,20);
	ThisEdit e1(130,20);
	ExMessage msg;
	BeginBatchDraw();
	while(1){
		while(peekmessage(&msg)){
			Button1.On_Input(&msg);
			e1.On_Input(&msg);
		}
		
		
		FlushBatchDraw();
		
	}
	EndBatchDraw();
	closegraph();
	return 0;
}

好的,以下是一个easyx一个笑脸图标的示例代码: ```c++ #include <graphics.h> class Icon { private: int x, y, radius; public: Icon(int _x, int _y, int _radius) { x = _x; y = _y; radius = _radius; } void draw() { setfillcolor(YELLOW); fillellipse(x, y, radius, radius); setfillcolor(BLACK); fillellipse(x - radius / 3, y - radius / 3, radius / 6, radius / 6); fillellipse(x + radius / 3, y - radius / 3, radius / 6, radius / 6); arc(x, y + radius / 3, 220, 320, radius / 3); } }; int main() { initgraph(640, 480); Icon icon(320, 240, 100); icon.draw(); getch(); closegraph(); return 0; } ``` 在这个示例中,我们定义了一个名为`Icon`的,它有三个私有成员变量:`x`、`y`和`radius`,分别表示图标的圆心坐标和半径。中有一个构造函数,用于初始化这三个成员变量。中还有一个公共成员函数`draw()`,用于绘制图标。 在`draw()`函数中,我们首先设置填充颜色为黄色,然后用`fillellipse`函数绘制一个圆。接着,设置填充颜色为黑色,用`fillellipse`函数绘制两个小圆作为眼睛,用`arc`函数绘制一个弧线作为嘴巴。 在`main`函数中,我们先调用`initgraph`函数初始化绘图环境,然后创建一个`Icon`对象,并调用`draw`函数绘制图标。最后,调用`getch`函数等待用户按下任意键,然后调用`closegraph`函数关闭绘图环境。 运行这个程序,就可以在窗口中看到一个笑脸图标了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值