自定义控件的绘制

头文件:#include<graphics.h> 

// ButtonDemo.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include<string>

#include<graphics.h> //图形界面


class button
{
public:
	int x;
	int y;
	int width;
	int height;
	COLORREF color;
	char* pStr;
	static button* pButton;
};

//创建按钮
button* createButton(int x/*X位置*/, int y/*Y位置*/, int width/*宽*/, int height/*长*/, COLORREF color/*背景色*/, const char* text/*按钮标题*/)
{
	button* pButton = new button;
	pButton->x = x;
	pButton->y = y;
	pButton->width = width;
	pButton->height = height;
	pButton->color = color;
	pButton->pStr = (char*)malloc(strlen(text) + 1);
	strcpy(pButton->pStr, text);
	return pButton;
}

//释放按钮
void releasebutton(button* pb)
{
	button* Temp = pb;
	pb = NULL;
	delete Temp;
}

//绘制按钮
void drawButton(struct button* pb)
{
	settextstyle(30, 0, "宋体");  //设置字体
	setfillcolor(pb->color);    //设置颜色
	setlinecolor(BLACK);    //设置矩形边框颜色为黑色
	settextcolor(BLACK);    //设置字体颜色为黑色
	setbkmode(TRANSPARENT); //设置背景为透明

	fillrectangle(pb->x, pb->y, pb->x + pb->width, pb->y + pb->height); //绘制矩形
	outtextxy(pb->x + 25, pb->y + 5, pb->pStr);     //显示字体 位置
}


//判断鼠标是否在按钮内
bool mouseInButton(struct button* pb/*按钮*/, MOUSEMSG m/*鼠标*/)
{
	if ((pb->x <= m.x) && (m.x <= pb->x + pb->width))
	{
		if ((pb->y <= m.y) && (m.y <= pb->y + pb->height))
		{
			pb->color = RED;//设置背景为红色
				return true;
		}
	}
	pb->color = YELLOW; //设置字体为黄色
		return false;
}

//是否点击按钮
void clickButton(button* pb, MOUSEMSG mouse)
{
	if (mouseInButton(pb, mouse) && mouse.uMsg == WM_LBUTTONDOWN)
	{
		MessageBox(NULL, _T("左键按下"), _T("触发"), MB_SYSTEMMODAL);
	}
	else if (mouseInButton(pb, mouse) && mouse.uMsg == WM_RBUTTONDOWN)
	{
		MessageBox(NULL, _T("右键按下"), _T("触发"), MB_SYSTEMMODAL);
	}
}

int main()
{
	HWND win = initgraph(800, 600); //创建窗口

	IMAGE img;
	loadimage(&img, "E:\\img\\psc (1).png", 700, 600);      //加载图片

	button* pb = createButton(300, 200, 200, 40, YELLOW, "好好学习");
	button* pb2 = createButton(300, 250, 200, 40, YELLOW, "天天向上");
	button* pb3 = createButton(300, 300, 200, 40, YELLOW, "Mouse");
	button* pb4 = createButton(300, 350, 200, 40, YELLOW, "Windows");

	while (true)
	{
		BeginBatchDraw();
		putimage(50, 10, &img);   //显示图片
		//显示按钮
		drawButton(pb);
		drawButton(pb2);
		drawButton(pb3);
		drawButton(pb4);

		MOUSEMSG m = GetMouseMsg();
		if (mouseInButton(pb, m))
		{
			clickButton(pb, m);
		}
		else if (mouseInButton(pb2, m))
		{
			clickButton(pb2, m);
		}
		else if (mouseInButton(pb3, m))
		{
			clickButton(pb3, m);

		}
		else if (mouseInButton(pb4, m))
		{
			clickButton(pb4, m);
		}
		EndBatchDraw();

	}
	releasebutton(pb);
	releasebutton(pb2);
	releasebutton(pb3);
	releasebutton(pb4);
	closegraph();
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值