按钮封装加图形库与类的使用和c++面向对象的初级应用

所需用到Easyx库以及类的初始化和封装的概念,图形库的简单基础函数以及应用

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include <cstring>
#include <algorithm>
#include<graphics.h>

using namespace std;

class Button 
{
private:
	//按钮的左上角
	int x;
	int y;
	//按钮的宽和高
	int w;
	int h;
	unsigned long outColor;			//不在按钮中颜色
	unsigned long inColor;			//在按钮中颜色
	char* str;						//带有的文字
	unsigned long textColor;		//文字颜色
	char* changeTxt;//更改的文字
public:
	void setNum(int r,int m,int W,int H,COLORREF color, COLORREF c, const char*s,COLORREF c2)
	{
		x = r;
		y = m;
		w = W;
		h = H;
		outColor = color;
		inColor = c;
		str = (char*)malloc(strlen(s) + 1);
		strcpy(str, s);
		textColor = c2;
	}
	int getX()
	{
		return x;
	}
	int getY()
	{
		return y;
	}
	int getW()
	{
		return w;
	}
	int getH()
	{
		return h;
	}
	COLORREF getOutColor()
	{
		return outColor;
	}
	COLORREF getInColor()
	{
		return inColor;
	}
	char* getStr()
	{
		return str;
	}
	COLORREF getTxtColor()
	{
		return textColor;
	}

	//绘制按钮
	void DrawButton()
	{
		solidrectangle(getX(), getY(), getX() + getW(), getY() + getH());
		setbkmode(TRANSPARENT);
		settextcolor(getTxtColor());
		//调整文字显示居中
		int height = textheight(getStr());
		int width = textwidth(getStr());
		int x = getX() + (getW() - width) / 2;
		int y = getY() + (getH() - height) / 2;
		outtextxy(x,y,getStr());
	}
	//判断鼠标是否在按钮区域中
	bool mouseInButton(Button pB, struct MOUSEMSG m)
	{
		
		if ( pB.getX()<= m.x && m.x <= pB.getX() + pB.getW() &&
			pB.getY() <= m.y && m.y <= pB.getY() + pB.getH())
		{
			//若在区域中,背景颜色更改
			setfillcolor(pB.getInColor());
			return 1;
		}
		setfillcolor(pB.getOutColor());
		return 0;
	}

	//若在区域中,文字更改等操作效果
	void setTxt(const char*s)
	{
		str = (char*)malloc(strlen(s) + 1);
		strcpy(str, s);
	}
};
int main()
{
	initgraph(800, 600);
	Button pButton;
	pButton.setNum(0, 0, 100, 25, RGB(204, 213, 240), RGB(236, 244, 255),
		"开始", BLACK);
	Button endButton;
	endButton.setNum(200, 0, 100, 25, RGB(204, 213, 240), RGB(236, 244, 255),
		"结束", BLACK);
	//详情
	Button deTailButton;
	deTailButton.setNum(100, 0, 100, 25, RGB(204, 213, 240), RGB(236, 244, 255),
		"详情", BLACK);

	BeginBatchDraw();
	while (1)
	{
		MOUSEMSG m = GetMouseMsg();
		if (pButton.mouseInButton(pButton, m) && m.uMsg == WM_LBUTTONDOWN)
		{
			pButton.setTxt("保存中");
		}
		pButton.DrawButton();

	
		if (deTailButton.mouseInButton(deTailButton, m) && m.uMsg == WM_LBUTTONDOWN)
		{
			deTailButton.setTxt("详情");
		}
		deTailButton.DrawButton();

		if (endButton.mouseInButton(endButton, m) && m.uMsg == WM_LBUTTONDOWN)
		{
			endButton.setTxt("保存中");
			break;
		}
		endButton.DrawButton();
		FlushBatchDraw();
	}

	EndBatchDraw();
	closegraph();
	return 0;
}

对以后项目开发具有铺垫作用 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值