栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c++ --- 封装按钮类

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c++ --- 封装按钮类

  • 鼠标操作

  • 封装按钮               

基本的鼠标操作

#include 
#include 
using namespace std;    //鼠标反馈给程序的就是一个值,这个值在图形库的EASYXMSG结构体中

int main() 
{
	initgraph(400, 400);    //创建窗口
	ExMessage  m;			//1.定义一个鼠标消息变量 ---> 结构体中存了一些信息

	//鼠标当前坐标: m.x  m.y;
	//m.message: 鼠标类型的判断    Windows系统中,鼠标特定的操作具有特定的返回值
	//cout << WM_LBUTTonDOWN << endl;    是一个整数(都是一个固定的值)
	//WM_LBUTTonDOWN              左键按下
	//WM_LBUTTonUP            
	//WM_RBUTTonDOWN             
	//WM_RBUTTonUP                右键弹起

	while (1)                   //对自己的窗口做鼠标操作
	{
		//2.获取当前窗口的鼠标消息
		peekmessage(&m, EM_MOUSE);			//获取到哪个消息变量中 获取什么类型的消息(宏)
		//EM_MOUSE : 消息类型是鼠标操作
		//EM_KEY   : 按键操作(键盘)
		//3.根据不同按键分类处理鼠标消息       按下鼠标左键(要知道鼠标左键对应的值)
		if (m.message == WM_LBUTTONDOWN)    //根据具体操作去做相应的事情
		{
			circle(m.x, m.y, 10);           //在鼠标当前点击位置画一个圆
		}
		if (m.message == WM_RBUTTONDOWN) 
		{
			fillcircle(m.x, m.y, 10);       //画填充圆
		}
	}
	closegraph();            //关闭窗口
	return 0;
}
封装按钮

知道按钮的属性

可以自己添加筛选器,自己管理文件---在项目文件夹中不显示,可以帮助我们合理划分项目模块

封装窗口类 

公共头文件       common.h

#pragma once
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#pragma comment(lib,"winmm.lib")    //静态库
using namespace std;

  widget.h 窗口

#pragma once
#include "common.h"
class Widget     //不同的显示模式,有两种参数---用枚举类型表示显示窗口的类型
{
public:
	enum SHOW_STYLE
	{
		NO_CON,  //不显示控制台 0
		YEW_CON  //显示控制台   1
	};
public:                                            //是否显示控制台 默认0表示不显示控制台
	Widget(int width, int height, string url = "");//创建窗口  窗口的宽高,是否有图片显示
	void Show(int flag = 0);                       //显示窗口  以什么方式显示
	void Refresh();                                //刷新函数---不刷新没有显示
	bool exec();                                   //防止闪屏的函数

	~Widget();                                     //做了动态内存申请,需要析构函数
protected:
	int width;
	int height;
	IMAGE* img;    //图片
	string imgURL; //路径
};

 widget.cpp

#include "widget.h"
Widget::Widget(int width, int height, string url)
{
	this->width = width;      //构造函数给数据成员初始化
	this->height = height;
	this->imgURL = url;		  //窗口背景的图片路径
	if (url.size() != 0)      //!=0说明传入了图片(创建窗口可以不带图片) 
	{
		this->img = new IMAGE;  //loadimage要求使用多字节 通过路径加载图片
		loadimage(this->img, url.c_str(), this->width, this->height);//缩放成窗口大小
	}
}
void Widget::Show(int flag)
{
	initgraph(this->width, this->height, flag);//显示窗口    窗口宽高 参数
	if (this->imgURL.size() != 0)              //如果图片路径!=0,要把图片背景贴出来
	{
		putimage(0, 0, this->img);             //在0,0位置显示img图片
	}
}
void Widget::Refresh()
{
	if (imgURL.size() != 0)
		putimage(0, 0, img);                   //如果文件的路径!=0,用贴图的方式去刷新
	else
		cleardevice();                         //没有图片,直接做清屏效果,也是刷新
}
bool Widget::exec()
{
	while (_getch() != 'r');  //写了一个循环让窗口停在那,按回车就关闭窗口
	return 0;
}
Widget::~Widget()
{
	if (imgURL.size() != 0)    //需要先判断路径是否存在
	{
		delete img;
	}
}

textDemo.cpp 主函数部分

#include "common.h"
#include "widget.h"
#include "button.h"
#include "bightLine.h"
int main() 
{

	Widget w(800, 800); //构建一个800*800的窗口不带图片 Widget w(800, 800,.jpg);带图片

    w.Show();


    Button *pp=new Button("播放音乐",300,300,200,50);    //封装的好处,做任何按钮都一样
    Button *ppause=new Button("暂停音乐",300,355,200,50);
    //pp->SetBkColor(RGB(204,213,240)); 用缺省的方式(给一个默认颜色)
    //pp->SetTextClolur(BLACK);   */
    Button *pre=new Button("继续播放",300,410,200,50);  
    Button *pclose=new Button("关闭音乐",300,465,200,50);  
	ExMessage m;                        //定义鼠标消息
    BeginBatchDraw();                   //开始双缓冲---防止闪屏
    mciSnedString("open 1.mp3",0,0,0);  //打开音乐文件

	while (true) 
	{
		w.Refresh();                //刷新
		peekmessage(&m, EM_MOUSE);  //获取鼠标消息

        pp->Show();
        ppause->Show();
        pre->Show();
        pclose->Show();

        if(pp->ClickButton(m))       //如果点击...按钮,对应事件处理
        {
            mciSendString("play 1.mp3 repeat",0,0,0);    //重复播放
        }
        if(ppause->ClickButton(m))
        {
            mciSendString("pause 1.mp3 repeat",0,0,0);
        }
        if(pre->ClickButton(m))
        {
            mciSendString("resume 1.mp3 repeat",0,0,0);    //继续
        }
        if(pclose->ClickButton(m))
        {
            mciSendString("close 1.mp3 repeat",0,0,0);
        }    
        
        FlushBatchDraw();    //清屏
	}
    EndBatchDraw();          //结束双缓冲
    delete pp;
    delete ppause;
    delete ppre;
    delete pclose;
	return w.exec();         //函数遇到return结束
}
封装按钮

button.h 按钮就是一个矩形

#pragma once
#include "common.h"
#include "widget.h"
class Button :public Widget             //继承窗口类(有宽度和高度,无起始坐标)
{
public:
//缺省处理
	Button(string text="", int x = 0, int y = 0, int width = 100, int height = 25);
	void SetBkColor(COLORREF color);   //提供接口去修改---设置按钮背景颜色
	void SetTextColor(COLORREF color); //设置文本颜色
	void Show();                       //按钮显示  访问---就近原则

	bool MouseInButton(ExMessage msg);  
	bool ClickButton(ExMessage msg);    

protected:
	//按钮的坐标
	int x;
	int y;
	//按钮文字
	string text;
	//按钮颜色
	COLORREF curColor;		//当前按钮颜色(和鼠标不在按钮中的颜色一样)
	COLORREF inColor;		//鼠标在按钮中
	COLORREF outColor;		//鼠标不在按钮中的颜色
	COLORREF textColor;     //文字颜色
	
};

 button.cpp 

#include "button.h"
//调用父类构造函数
Button::Button(string text, int x, int y, int width, int height):Widget(width,height)
{
	this->curColor = LIGHTGREEN;   //缺省处理 给一个默认颜色
	this->outColor = LIGHTGREEN;
	this->inColor = LIGHTBLUE;
	this->textColor = BLACK;
	this->text = text;    //初始化文字
	this->x = x;
	this->y = y;
}
void Button::SetBkColor(COLORREF color)
{
	this->curColor = color;
	this->outColor = color;
	this->inColor = LIGHTBLUE;		//鼠标在按钮中显示淡蓝色(图形库中没有自带按钮)
}
void Button::SetTextColor(COLORREF color)
{
	this->textColor = textColor;
}
void Button::Show()
{
	//把按钮绘制出来
	setlinecolor(this->curColor);    //线的颜色和当前按钮颜色一样
	setfillcolor(this->curColor);    //设置填充颜色
    //画填充矩形(左上右下的坐标)
	fillrectangle(this->x, this->y, this->x + this->width, this->y + this->height);
	//文字显示
	//文字居中
	setbkmode(TRANSPARENT);		      //去掉文字的背景(设置背景为透明方式)
	settextcolor(this->textColor);    //设置文字颜色
//字体大小一般为按钮高度的2/3
	settextstyle(this->height*2/3, 0, "楷体"); //文字样式---粗细/0是自适应/字体
	int x = this->x+(this->width - textwidth(text.c_str()))/2;
	int y = this->y + (this->height - textheight(text.c_str())) / 2;
	outtextxy(x, y, text.c_str());  //在x,y坐标的位置显示文字  文字一定要转换为.c_str()!
}
bool Button::MouseInButton(ExMessage msg)
{
	if (msg.x >= this->x && msg.y >= this->y && //鼠标在按钮中的条件
		msg.x <= this->x + this->width &&
		msg.y <= this->y + this->height) 
	{
		this->curColor = this->inColor;   //把当前颜色改为鼠标在里面的颜色inColour
      //cout<curColour<curColor = this->outColor;      //把当前颜色改为鼠标不在里面的颜色outColour
      //cout<curColour<
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/675792.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号