easyx 制作C++计时器


本文仅在博客园发布,若在其他网站发现均为盗取,请认准原文链接:
https://www.cnblogs.com/jisuanjizhishizatan/p/15521112.html

先上效果:

输入设定时间后,会出现时间。

如果时间结束,则显示time's up。

代码的实现很简单,就是每间隔1秒刷新一次时间。间隔1秒可以使用Sleep(1000)实现。
关于输出时间和输入设定时间,我们可以使用easyx的inputbox来实现。
对于时间的60进制,我们可以判断当前的seconds是否为0,如果为0,那么把minutes减去一,然后把seconds重置为59.
代码:

// Timer.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<graphics.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<windows.h>
#include<conio.h>

int main(void){
	initgraph(300,200);
	setbkcolor(WHITE);
	settextcolor(BLACK);
	cleardevice();
	settextstyle(48,0,"Consolas");
	char s[100],t[100];
	InputBox(s,sizeof(s),"输入设定时间:(分钟为单位)","设定时间");
	int minutes=0,seconds=0;
	sscanf(s,"%d",&minutes);
	while(minutes!=0 || seconds!=0){
		if(seconds==0){
			minutes--;seconds=59;
		}
		else seconds--;
		Sleep(1000);
		sprintf(t,"%02d:%02d",minutes,seconds);
		cleardevice();
		outtextxy(50,50,t);
	}
	cleardevice();
	outtextxy(50,50,"Time's up!");
	_getch();
	closegraph();
	return 0;
}

注意需要提前把项目设置为使用多字节字符集而不是Unicode,否则会出编译错误。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM