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

Day17:C++ WITH Easyx

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

Day17:C++ WITH Easyx

 

目录

头文件:#include

一、鼠标的相关操作

1.创建窗口和关闭窗口

2.创建消息对象

 3.获取鼠标当前的坐标

 4.几种常见的消息类型

5.操作鼠标消息三步走:

        step1:创建消息对象

        step2:通过peekmessage来捕捉相关的鼠标消息        

        step3:根据捕捉到的消息,进行相应的操作 

6.实例:

二、按钮封装

1.目的:

2.小技巧:

3.在写图形化easyx界面的时候,一定要将其改为多字节。

 4.相关代码:

公共头文件:  common.h

UI筛选器下的widget.h

UI筛选器下的 widget.cpp

UI下的button.h

 UI下的button.cpp

三、实例之画线小工具

1.直接复用刚才写的common.h    button类和widget类

2.新增:

Tool中的basic_shape.h

 Tool中的bightLine.h     

 Tool中的bightLine.cpp     

待完成模块:新增一个颜色板(从button派生类,新写方法,实现点击返回相应颜色的功能)

画曲线测试:​编辑


头文件:#include

一、鼠标的相关操作

1.创建窗口和关闭窗口
initgraph(400,400);
closegraph();

2.创建消息对象

             (注:不是专门的鼠标消息对象,而是一个消息对象,可以通过peekmessage专门捕捉鼠标的消息) 

ExMessage m;

 3.获取鼠标当前的坐标
int m_x; int m_y;
m_x=m.x; m_y=m.y;

 4.几种常见的消息类型

WM_LBUTTONDDOWN、WM_LBUTTONUP、WM_RBUTTONDOWN、WM_RBUTTONUP  ->实际上都对应着某一个十六进制的整数

5.操作鼠标消息三步走:

        step1:创建消息对象

        step2:通过peekmessage来捕捉相关的鼠标消息        

peekmessage(&m,EM_MOUSE) 第二个参数表示是消息类型是鼠标操作

                当然,也可以是按键操作 EM_KEY

        step3:根据捕捉到的消息,进行相应的操作 

6.实例:

                实现左键点击画空心圆,右键点击画一个实心圆的操作。

#include
#include
using namespace std;

int main()
{
	initgraph(400, 400);

    //step one:
	ExMessage m;

	while (1)
	{
        //step two:
		
		peekmessage(&m, EM_MOUSE);		

        //step three:
		if (m.message == WM_LBUTTONDOWN)
		{
			circle(m.x, m.y, 20);
		}
		if (m.message == WM_RBUTTONDOWN)
		{
			fillcircle(m.x, m.y, 10);
		}

	}
	closegraph();
	return 0;
}

效果展示: 

二、按钮封装

1.目的:

        自己构建几个筛选器,将基本的绘图的控件构建完毕,以后直接将这些头文件拿来用即可。

2.小技巧:

        若想在另一个项目中复用这些tool&ui的.h和.cpp的文件,可以先将这些文件复制到对应项目的文件夹中,然后右键,添加“现有项”即可。

3.在写图形化easyx界面的时候,一定要将其改为多字节。

 4.相关代码:

公共头文件:  common.h

#pragma once
#include 
#include 
#include 
#include 
#include 
#pragma comment(lib,"WINMM.LIB")
using namespace std;

UI筛选器下的widget.h

        ①构造函数(宽度、高度、读取图片的路径、flag(是否显示小黑窗))

        ②show()->绘制相应的窗口

        ③Refresh()更新窗口

        ④搞一个防闪退exec,按回车退出

#pragma once
#include"common.h"
class Widget
{
public:
	enum SHOW_STYLE
	{
		NO_CON,    
		YEW_CON	   
	};
public:
	Widget(int width, int height, string url = "", int flag = 0);
	void show(int flag = 0);
	void Refresh();
	~Widget();				
	bool exec()
	{
		while (_getch() != 'r');
		return 0;
	}
protected:
	int width;
	int height;
	IMAGE* img;
	string imgURL;			
};

UI筛选器下的 widget.cpp

#include "widget.h"

Widget::Widget(int width, int height, string url, int flag)
{
	this->width = width;
	this->height = height;
	this->imgURL = url;				
	if (url.size() != 0)
	{
		this->img = new IMAGE;		
		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)
	{
		putimage(0, 0, this->img);	
	}
}

void Widget::Refresh()
{
	if (imgURL.size() != 0)
	{
		putimage(0, 0, img);
	}
	else
	{
		cleardevice();
	}
}

Widget::~Widget()
{
	if (imgURL.size() != 0)			
	{
		delete img;
	}
}

UI下的button.h

        ①已继承下来了width和height,一个按钮还需要x、y坐标以及上面可以显示的文字

        ②增加设置相关数据成员COLORREF和文字颜色的接口

        ③show()->绘制按钮(主要就是保证文字的居中!)

        ④MouseInButton()判断鼠标是否放在了按钮上

        ⑤clickButton()   点击鼠标后进行的反应

        ⑥新增数据成员,鼠标放置在按钮上颜色,和不在按钮上的颜色及当前色 COLOREF

#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);
	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;
	
};

 UI下的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->text = text;
	this->x = x;
	this->y = y;
}

void Button::SetBkColor(COLORREF color)
{
	this->curColor = color;
	this->outColor = color;
	this->inColor = LIGHTBLUE;//鼠标在按钮中显示淡蓝色
	this->textColor = BLACK;
}

void Button::setTextColor(COLORREF color)
{
	this->textColor = textColor;
}

void Button::show()
{
	//按钮绘制出来(①按钮边框线的颜色②按钮的填充颜色)
	setlinecolor(this->curColor);
	setfillcolor(this->curColor);
	fillrectangle(x, y, x + width, y + height);
	//文字显示:文字居中及文字样式
	setbkmode(TRANSPARENT);				        
	settextstyle(2*this->height/3, 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());
}

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; 
		return true;
	}
	this->curColor = this->outColor;
	return false;
}

bool Button::clickButton(ExMessage msg)
{
	if (MouseInButton(msg)&&msg.message == WM_LBUTTONDOWN)
	{
		return true;
	}
	return false;
}

三、实例之画线小工具

1.直接复用刚才写的common.h    button类和widget类

2.新增:

Tool中的basic_shape.h

        写一个抽象类

#pragma once
#include"common.h"
class Basic_shape
{
public:
	virtual void drawShape(ExMessage msg) = 0;
protected:
	vector shape;
};

 Tool中的bightLine.h     

        注意要重写抽象父类的方法

#pragma once

#include"common.h"
#include"basic_shape.h"

class BightLine :public Basic_shape
{
public:
	BightLine(int size = 5, COLORREF color = WHITE);
	void drawLine(ExMessage msg);
	void drawShape(ExMessage msg);
	
protected:
	int size;				
	COLORREF color;			
	tuplebegin;	
	tuple end;	
	bool isDown;			
};

 Tool中的bightLine.cpp     

        关键:如何实现画出曲线的效果!isDown作为检测变量,过渡两个消息之间的差异,注意:时刻更新begin的坐标,这样while(1)的循环下,使setlinestyle(PS_ENDCAP_ROUND, this->size);  形成曲线的效果

#include "bightLine.h"
BightLine::BightLine(int size, COLORREF color)
{
	this->size = size;
	this->color = color;
	this->isDown = false;
}

void BightLine::drawLine(ExMessage msg)
{
	if (msg.message == WM_LBUTTONDOWN)
	{
		this->isDown = true;
		begin = tuple(msg.x, msg.y);	

	}
	if (msg.message == WM_LBUTTONUP)
	{
		this->isDown = false;
	}
	if (this->isDown == true && msg.message == WM_MOUSEMOVE)
	{
		setlinestyle(PS_ENDCAP_ROUND, this->size);
		line(get<0>(begin), get<1>(begin), msg.x, msg.y);
	}
	
	begin = tuple(msg.x, msg.y);
}

void BightLine::drawShape(ExMessage msg)
{
	drawLine(msg);
}

待完成模块:新增一个颜色板(从button派生类,新写方法,实现点击返回相应颜色的功能)

#pragma once
#include"button.h"

class ColorTool :public Button
{
public:
	COLORREF getColor();
};
#include "colorTool.h"

COLORREF ColorTool::getColor()
{
    return COLORREF();
}

画曲线测试:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/997956.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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