C++练手小项目之倒计时(EasyX图形化编程)

项目简介:

  1. 实现功能:开始、暂停、重置;
  2. 项目进行了模块化封装,易于修改界面;
  3. 暂停按钮需要长按1s或者点按,因为使用了Sleep()函数;
  4. 适合小白或者刚接触C++的EasyX图形化编程的小伙伴;
  5. 我会将源文件上传到百度网盘(链接在文末)和CSDN资源社区,供小伙伴自行下载;
  6. 注意事项:项目属性-》高级-》字符集-》使用多字节字符集;
  7. 关于EasyX库可自行搜索下载安装,否则部分代码会报错;
  8. 如有疑问或者下载链接失效,可评论留言,我将在第一时间为你解忧;
封装的类
//按钮类
class Button
{
public:
	Button(int x, int y, int width, int length) :
		x(x), y(y), width(width), length(length), white(WHITE) 
	{
		this->str = new char[12];
		//this->str = nullptr;
	}
	int Draw();               //画按钮
	int setColor(COLORREF);   //设置按钮颜色
	char& setText(const char*,COLORREF);//设置按钮文字和颜色

	//按钮点击事件...
	bool Clicked(ExMessage);

protected:
	int x;
	int y;
	int width;
	int length;
	char* str;
	COLORREF white;
};
//时间类
class Time
{
public:
	Time() :hour(0), minute(6), second(0),SWITCH(false),btn(nullptr) {}
	//打印时分秒函数...
	void outtext_Hour(int hour);
	void outtext_Minute(int minute);
	void outtext_Second(int second);

	//获取时分秒函数
	int& getHour();
	int& getMinute();
	int& getSecond();
	bool& getSwitch();

	int start();

protected:
	int hour;
	int minute;
	int second;
	bool SWITCH;

	Button* btn;
};
//注窗口界面类
class MyWin
{
public:
	MyWin(int width, int length) :Win_width(width), Win_length(length) {}
	int DrawWin();

protected:
	int Win_width;
	int Win_length;
};
  • 这就是几个主要的类的封装,没有实现具体的功能,具体功能的实现我只展示部分主要的函数;
倒计时函数:
int Time::start()
{
    Sleep(250);
    if (this->hour >= 0 || this->minute >= 0 || this->second >= 0)
    {
        if (this->second >= 0 || this->minute >= 0 || this->hour >= 0)
        {
            if (this->second == 0 && this->minute > 0)
            {
                this->second = 60;
                this->minute--;
            }
            if (this->second == 0 && this->minute <= 0 && this->hour > 0)
            {
                this->hour--;
                this->minute = 59;
                this->second = 60;
            }
        }
        this->second--;
        Sleep(500);
        cout << this->hour << " : " << this->minute << " : " << this->second << endl;
        //setbkmode(TRANSPARENT)

        if (this->hour <= 0 && this->minute <= 0 && this->second <= 0)
        {
            this->getHour() = 0;
            this->getMinute() = 6;
            this->getSecond() = 0;
            this->getSwitch() = false;
            this->SWITCH = false;
            this_thread::sleep_for(3s);//和Sleep()函数相同的功能,具体可查阅相关资料
        }

        Sleep(250);
    }

    return 0;
}
设置文字显示并居中
//如有疑问可参考我前面的文章,有为什么这样设置文字为按钮居中的详解
char& Button::setText(const char* str1,COLORREF color)
{   
    int str_length = strlen(str1) + 1;
    strcpy_s(this->str, str_length, str1);

    //设置文字风格部分...
    settextstyle(32, 0, "楷体");
    settextcolor(color);
    setbkmode(TRANSPARENT);

    //显示文字部分...
    int textW = textwidth(this->str);
    int textH = textheight(this->str);

    int t_X = this->x + (this->width - textW) / 2;
    int t_Y = this->y + (this->length - textH) / 2;

    outtextxy(t_X, t_Y, this->str);

    return *this->str;
}
判断按钮点击事件
bool Button::Clicked(ExMessage m)
{
    if (m.message == WM_LBUTTONDOWN &&
        m.x >= this->x && m.x <= this->x + this->width &&
        m.y >= this->y && m.y <= this->y + this->length)
    {
        return true;
    }

    return false;
}
主函数逻辑部分
//鼠标消息初始化...
	ExMessage m;
	BeginBatchDraw();

	//按键与操作消息关联...
	//tim_1.stop(btn2, m);

	while (1)
	{
		//cout << "绘图咯,绘图咯..." << endl;
		putimage(-10, 0, &img_bk);

		//鼠标消息处理部分...
		while (peekmessage(&m, EM_MOUSE));

		if (btn1->Clicked(m))
		{
			tim_1.getSwitch() = true;
			cout << "click message..." << endl;
			//flushmessage(EM_MOUSE | EM_KEY);
		}
		if (btn2->Clicked(m))
		{
			tim_1.getSwitch() = false;
			cout << "stop message..." << endl;
			//flushmessage(EM_MOUSE | EM_KEY);
		}

		if (btn3->Clicked(m))
		{
			tim_1.getHour() = 0;
			tim_1.getMinute() = 6;
			tim_1.getSecond() = 0;
			tim_1.getSwitch() = false;
			cout << "repeat time..." << endl;
			//flushmessage(EM_MOUSE | EM_KEY);
		}

		//倒计时逻辑部分...
		
		if (tim_1.getSwitch())
		{
			tim_1.start();
			//Sleep(500);
		}

		settextcolor(LIGHTRED);

		//绘制倒计时时间...
		//tim_1.outtext_Hour(tim_1.getHour());
		tim_1.outtext_Minute(tim_1.getMinute());
		tim_1.outtext_Second(tim_1.getSecond());
		
		//绘制按钮...
		btn1->setColor(YELLOW);
		btn1->Draw();
		btn1->setText("开始", RED);

		btn2->setColor(RED);
		btn2->Draw();
		btn2->setText("暂停", YELLOW);

		btn3->setColor(BLUE);
		btn3->Draw();
		btn3->setText("重置", WHITE);
		FlushBatchDraw();
	}
	EndBatchDraw();
项目运行截图

C++练手小项目之倒计时(EasyX图形化编程)_第1张图片

可将项目设置为如下以减小电脑负担

减小电脑负担的方法

  • 文件已上传至我的CSDN资源处,欢迎下载;
  • 源代码网盘链接:链接: https://pan.baidu.com/s/1KrEz33EPLavtPFVsUpdi1Q?pwd=vmjr 提取码: vmjr;(如遇链接失效可评论留言)
  • 注意:如有疑问欢迎留言讨论!!!其他的问题在我能力范围以内的也欢迎各位小伙伴私信或留言讨论!!!

你可能感兴趣的:(C++基础,c++,c语言,开发语言,小程序)