展开全部
/*******************************/
/* 时钟源程序 */
/* 仿WINDOWS界面设计 */
/* Turboc3下调试通过 */
/* 包含两个源文件: */
/* clock.cpp win.cpp */
/*******************************/
#include"stdio.h"
#include"stdlib.h"
#include"conio.h"
#include"time.h"
#include"string.h"
#include"math.h"
#include"dos.h"
#include"bios.h"
#include"win.cpp" //调用窗体显示
#define TRUE 1
#define FALSE 0
#define PI 3.1415926
void cursor(int x,int y) //光标
{
int count=0;
while(count<=10){
delay(20);
count++;
if(count<=5)
setcolor(9);
else
setcolor(7);
line(x,y,x,y+12);
line(x+1,y,x+1,y+12);
}
}
int keyscancode() //检测按键
{
int key;
while(bioskey(1)==0) return 0;
key=bioskey(0);
key=key&0xff ? key&0xff:key;
return(key);
}
void message(int x,int y,int n) //状态栏信息
{
char *msg[6]={"Press ESC to quit clock.",
"Press T or t to set time.",
"Press D or d to set date.",
"Press ESC to cancel set.",
"Press Enter to confirm set.",
"Press BackSpace to delete a number."};
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(x,y,x+50*8,y+12);
setcolor(10);
outtextxy(x,y+2,msg[n]);
}
class CLOCK:public WIN //定义CLOCK为WIN的继承类
{
int hour,min,sec;
int year,mon,day;
int x,y,radio;
float ak,aj,ai;
int xs,ys,xm,ym,xh,yh;
public:
CLOCK(char *,int,int,int,int);
int isleapyear(int);
int isweek(int,int);
void showclock();
void runclock();
void setclock();
void inittodaydate();
void initnowtime();
void showdate();
void hidedate();
void pip();
void showtime();
void setdates();
};
//构造函数
CLOCK::CLOCK(char *n,int a,int b,int c,int d):WIN(n,a,b,c,d){
x=a+130;
y=b+155;
radio=120;
}
int CLOCK::isleapyear(int year) //判断是否闰年
{
if(year%100!=0&&year%4==0)
return TRUE;
if(year%400==0)
return TRUE;
return FALSE;
}
int CLOCK::isweek(int year,int mon) //计算year年mon月1号的星期数
{
int days=0,week=0;
int month[12]={31,28,31,30,31,30,31,31,30,31,30,31};
if(isleapyear(year)) month[1]=29;
else month[1]=28;
for(int i=1;i<year;i++)
{
days=365;
if(isleapyear(i)) days=366;
week=(days+week)%7;
}
for(i=0;i<mon-1;i++)
week=(month[i]+week)%7;
return week;
}
void CLOCK::setdates() //设置曰期
{
struct date set;
int key=0,count=0,index=0;
int textx=x+140,texty=y+42,tcnt=0;
char text[2];
int dateset[10];
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(textx,texty,textx+150,texty+12);
do{
message(80+6,380-12,index+3);
count++;
index=count/10;
if(count==29) count=0;
runclock();
key=keyscancode();
cursor(textx,texty);
if((key>='0'&&key<='9')&&tcnt!=10)
{ //限制按键,防止误操作
if(tcnt<4||(tcnt==5&&key<='1')||((tcnt==6&&dateset[tcnt-1]==1&&key<='2')||(tcnt==6&&dateset[tcnt-1]==0))||(tcnt==8&&key<='3')||(tcnt==9))
{
sprintf(text,"%c",key);
text[1]='\0';
setcolor(LIGHTBLUE);
outtextxy(textx,texty+2,text);
dateset[tcnt]=key-'0';
textx+=10;
tcnt++;
if((tcnt==4||tcnt==7)&tcnt!=10){
outtextxy(textx,texty+2,"-");
textx+=10;
tcnt++;
}
}
}
if(key==8&&tcnt>0){
if(tcnt==5||tcnt==8)
{
tcnt-=2;
textx-=20;
}
else{
tcnt--;
textx-=10; }
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(textx,texty,textx+8,texty+12);
}
}while(key!=13&&key!=27);
if(key==27) return; //如果是ESC键,则退出设置
if(key==13) //如果ENTER键,则设置曰期
{
set.da_year=dateset[0]*1000+dateset[1]*100+dateset[2]*10+dateset[3];
set.da_mon=dateset[5]*10+dateset[6];
set.da_day=dateset[8]*10+dateset[9];
setdate(&set);
}
}
void CLOCK::setclock() //设置时间
{
struct time set;
int key=0,count=0,index=0;
int textx=x+140,texty=y+6,tcnt=0;
char text[2];
int timeset[8];
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(textx,texty,textx+150,texty+12);
do{
message(80+6,380-12,index+3);
count++;
index=count/10;
if(count==29) count=0;
key=keyscancode();
cursor(textx,texty);
if((key>='0'&&key<='9')&&tcnt!=8)
{ //限制按键,防止误操作
if((tcnt==0&&key<='2')||((tcnt==1&&key<='3'&×et[tcnt-1]==2)||(tcnt==1&×et[tcnt-1]!=2))||(tcnt==3&&key<='5')||(tcnt==6&&key<='5')||tcnt==4||tcnt==7)
{
sprintf(text,"%c",key);
text[1]='\0';
setcolor(9);
outtextxy(textx,texty+2,text);
timeset[tcnt]=key-'0';
textx+=10;
tcnt++;
if((tcnt==2||tcnt==5)&&tcnt!=8) {
outtextxy(textx,texty+2,":");
textx+=10;
tcnt++;
}
}
}
if(key==8&&tcnt>0){
if(tcnt==3||tcnt==6){
tcnt-=2;
textx-=20;
}
else{
tcnt--;
textx-=10;
}
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(textx,texty,textx+8,texty+12);
}
}while(key!=13&&key!=27);
if(key==27) return;
if(key==13)
{
set.ti_hour=timeset[0]*10+timeset[1];
set.ti_min=timeset[3]*10+timeset[4];
set.ti_sec=timeset[6]*10+timeset[7];
settime(&set);
}
}
void CLOCK::showdate() //在窗体上显示曰期
{
char nowdate[15];
char days[4];
char *week[7]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
int month[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int dayx=x+135,dayy=y-88,todayweek=0,control;
struct date d;
getdate(&d);
setcolor(12);
for(int i=0;i<7;i++)
outtextxy(x+i*8*4+135,y-100,week[i]);
todayweek=isweek(d.da_year,d.da_mon); // 得到本月1号的星期数
if(todayweek+1!=7)
for(i=1;i<=todayweek+1;i++) dayx=dayx+4*8;
control=todayweek+1;
if(isleapyear(d.da_year)) month[1]=29;
else month[1]=28;
setcolor(11);
for(i=1;i<=month[d.da_mon-1];i++)
{
control++;
sprintf(days,"%3d",i);
if(i==d.da_day) setcolor(LIGHTMAGENTA);
outtextxy(dayx,dayy,days);
setcolor(11);
dayx=dayx+4*8;
if(control%7==0) { dayx=x+135; dayy=dayy+12; }
}
todayweek=(todayweek+day)%7; //得到今天的星期数
sprintf(nowdate,"%04d-%02d-%02d %s",d.da_year,d.da_mon,d.da_day,week[todayweek]);
nowdate[14]='\0';
setcolor(LIGHTBLUE);
outtextxy(x+185,y-120,nowdate);
if(d.da_year!=year||d.da_mon!=mon||d.da_day!=day) //如果曰期改变了,则先隐藏曰期,再显示
hidedate();
}
void CLOCK::hidedate() //隐藏曰期
{
inittodaydate();
setfillstyle(SOLID_FILL,LIGHTGRAY); //抹去曰期,如果曰期改变了
bar(x+135,y-90,x+135+6*8*4+3*8,y-90+12*6);
bar(x+165,y-122,x+170+16*8+10,y-112);
}
void CLOCK::showtime() //显示时间
{
char nowtime[9];
sprintf(nowtime,"%02d:%02d:%02d",hour,min,sec);
nowtime[8]='\0';
setcolor(14);
outtextxy(x-30,y+82,nowtime);
ak=(90+30*(12-hour)-min*5/60.*6)*PI/180; //时针角度
aj=(90+6*(60-min))*PI/180; //分针角度
ai=(90+6*(60-sec))*PI/180; //秒针角度
xs=x+cos(ai)*(radio-10);
ys=y-sin(ai)*(radio-10);
xm=x+cos(aj)*(radio-25);
ym=y-sin(aj)*(radio-25);
xh=x+cos(ak)*(radio-45);
yh=y-sin(ak)*(radio-45);
setcolor(RED); //画时针
setlinestyle(0,0,THICK_WIDTH);
line(x-cos(ak)*(radio-118),y+sin(ak)*(radio-118),xh,yh);
setlinestyle(0,0,NORM_WIDTH); //画时针在钟面上指示位置
circle(x+cos(ak)*radio,y-sin(ak)*radio,2);
setcolor(LIGHTGREEN);
circle(x+cos(aj)*radio,y-sin(aj)*radio,2); //画分针在钟面上的指示位置
line(x-cos(aj)*(radio-115),y+sin(aj)*(radio-115),xm,ym); //画分针
setcolor(LIGHTBLUE);
line(x-cos(ai)*(radio-110),y+sin(ai)*(radio-110),xs,ys); //画秒针
circle(x+cos(ai)*radio,y-sin(ai)*radio,2); //画秒针在钟面上的指示位置
}
void CLOCK::runclock() //运行时钟
{
int h,m,s;
float ah;
struct time tim;
gettime(&tim);
s=tim.ti_sec;
m=tim.ti_min;
h=tim.ti_hour;
ah=(90+30*(12-h)-m*5/60.*6)*PI/180;
showdate();
showtime();
delay(200);
nosound();
if(ah!=ak) {
setcolor(LIGHTGRAY);
setlinestyle(0,0,THICK_WIDTH);
line(x-cos(ak)*(radio-118),y+sin(ak)*(radio-118),xh,yh);
setlinestyle(0,0,NORM_WIDTH);
if(hour%5==0) setcolor(YELLOW);
circle(x+cos(ak)*radio,y-sin(ak)*radio,2);
setcolor(LIGHTGRAY);
hour=h;
}
if(m!=min) {
setcolor(LIGHTGRAY);
line(x-cos(aj)*(radio-115),y+sin(aj)*(radio-115),xm,ym);
if(min%5==0) setcolor(YELLOW);
circle(x+cos(aj)*radio,y-sin(aj)*radio,2);
min=m;
}
if(s!=sec) {
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(x-30,y+80,x-30+8*8,y+80+8);
setcolor(LIGHTGRAY);
if(sec%5==0) setcolor(YELLOW);
circle(x+cos(ai)*radio,y-sin(ai)*radio,2);
setcolor(LIGHTGRAY);
line(x-cos(ai)*(radio-110),y+sin(ai)*(radio-110),xs,ys);
sec=s;
if(hour>=12) //整点报时
h=hour-12;
else
h=hour;
if(min==59&&sec>=60-h)
sound(392);
if(min==00&&sec==00)
sound(784);
}
showclock();
}
void CLOCK::inittodaydate() //初始化今天的曰期
{
struct date initdate;
getdate(&initdate);
year=initdate.da_year;
mon=initdate.da_mon;
day=initdate.da_day;
}
void CLOCK::initnowtime() //初始化时间
{
struct time inittime;
gettime(&inittime);
hour=inittime.ti_hour;
min=inittime.ti_min;
sec=inittime.ti_sec;
}
void CLOCK::showclock() //显示时钟
{
float alpha;
int x0,y0,i;
char s[3];
for(i=60;i>=1;i--) //显示钟面上数字
{
alpha=(90+6*(60-i))*PI/180;
x0=x+cos(alpha)*radio;
y0=y-sin(alpha)*radio;
setcolor(14);
if(i%5==0){
circle(x0,y0,2);
sprintf(s,"%d",i/5);
outtextxy(x+cos(alpha)*(radio-10)-4,y-sin(alpha)*(radio-10)-2,s);
}
else
circle(x0,y0,1);
}
}
void about(int x,int y,int index) //关于与帮助信息
{
int i=0,t=0;
char *abouttext[]={"You can get message from",
" the status bar at the ",
"bottom.",
"When you set time,you just",
"input like this:080819.",
"The symbol ':' isn't needed.",
"Set date is the same.",
"This clock is designed by",
" Nie Shiqiu.(C99102-07)",
"I'm very interested in C/C++",
"This is my course design."};
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(x,y,x+27*8+6,y+4*12);
t=index-3;
if(index<4){
y=y+(3-index)*12;
t=0;
}
for(i=t;i<=index;i++)
{
if(i<11){
if(i==8) setcolor(RED); //名字着重显示
else setcolor(LIGHTBLUE);
outtextxy(x+2,y+2,abouttext[i]);
}
y+=12;
}
}
void main() //主程序
{
int driver=DETECT,mode;
int exitflag=FALSE,key,index=0,count=0,aboutindex=0,acnt=0;
WIN winCLK("Clock",80,80,570,385); //创建窗体
CLOCK c1("Clock",80,80,570,385);
initgraph(&driver,&mode,"");
winCLK.Form(FALSE);
c1.showclock();
c1.initnowtime();
do{
about(342,80+185+44,aboutindex);
acnt++;
aboutindex=acnt/10;
if(acnt==139) acnt=0;
message(80+6,380-12,index);
count++;
index=count/10;
if(count==29) count=0;
key=keyscancode();
c1.runclock();
if(key==27) exitflag=TRUE; //按ESC退出
if(key=='T'||key=='t') {
c1.showtime();
c1.setclock();
}
if(key=='D'||key=='d') {
c1.setdates();
}
}while(exitflag==FALSE);
winCLK.draw_closebutton(570-18,80+4,BLACK,TRUE);
delay(300);
closegraph();
}
/*******************/
/* 窗体文件 win.cpp*/
/*******************/
#include"graphics.h"
#define TRUE 1
#define FALSE 0
class WIN
{
char title[20];
protected:
int left,top,right,bottom;
public:
WIN(char *n,int a,int b,int c,int d){strcpy(title,n);left=a;top=b;right=c;bottom=d;}
void draw_minbutton(int left,int top,int color,int state);
void draw_maxbutton(int left,int top,int color,int state);
void draw_closebutton(int left,int top,int color,int state);
void Form(int);
void Button(int,int,int,int,int);
void line3d(int,int,int,int,int);
void Frame(int,int,int,int,char *);
};
void WIN::Frame(int left,int top,int right,int bottom,char *s)
{
line3d(left,top+2,right,top+2,FALSE);
line3d(left,top+3,left,bottom,FALSE);
line3d(left,bottom,right,bottom,FALSE);
line3d(right,top+2,right,bottom,FALSE);
bar(left+4,top,left+4+8*strlen(s)+4,top+8);
setcolor(10);
outtextxy(left+5,top,s);
}
void WIN::line3d(int left,int top,int right,int bottom,int state) /*画有凹凸感的线条函数*/
{
if(state==FALSE){
setcolor(0);
line(left,top,right,bottom);
setcolor(15);
if(top==bottom){
line(left,top+1,right,bottom+1);
}
if(left==right){
line(left+1,top,right+1,bottom);
} }
else
{ setcolor(15);
line(left,top,right,bottom);
setcolor(0);
if(top==bottom){
line(left,top+1,right,bottom+1);
}
if(left==right){
line(left+1,top,right+1,bottom);
} }
}
void WIN::Form(int state)
{
Button(left,top,right,bottom,state);
setfillstyle(1,1);
bar(left+2,top+2,right-2,top+20);
setcolor(YELLOW);
circle(left+12,top+12,8);
line(left+12,top+12,left+12,top+4);
line(left+12,top+12,left+16,top+12);
outtextxy(left+16+10,top+8,title);
draw_minbutton(right-16*3-4,top+4,BLACK,FALSE);
draw_maxbutton(right-16*2-3,top+4,BLACK,FALSE);
draw_closebutton(right-18,top+4,BLACK,FALSE);
Frame(left+5,top+25,left+255,top+278,"Time");
Frame(left+260,top+25,right-5,top+25+20*6,"Date");
line3d(left+280,top+25+20,right-25,top+25+20,FALSE);
Frame(left+260,top+150,right-5,top+150+30,"SetTime(T)");
Button(left+265,top+160,right-8,top+150+25,TRUE);
Frame(left+260,top+185,right-5,top+185+30,"SetDate(D)");
Button(left+265,top+195,right-8,top+185+25,TRUE);
Button(left+4,bottom-20,right-4,bottom-4,TRUE);
Frame(left+260,top+185+35,right-5,bottom-27,"About");
}
void WIN::Button(int left,int top,int right,int bottom,int state)
{
if(state==-1){
setfillstyle(1,7);
bar(left,top,right,bottom);}
setfillstyle(1,7);
bar(left,top,right,bottom);
if(state==FALSE)
{
setcolor(15);
line(left,top,right,top);
line(left,top,left,bottom);
setcolor(0);
line(left,bottom,right,bottom);
line(right,top,right,bottom);
setcolor(8);
line(left+1,bottom-1,right-1,bottom-1);
line(right-1,top+1,right-1,bottom-1);
}
if(state==TRUE)
{
setcolor(8);
line(left,top,right,top);
line(left,top,left,bottom);
setcolor(15);
line(left,bottom,right,bottom);
line(right,top,right,bottom);
}
}
void WIN::draw_minbutton(int left,int top,int color,int state)
{
if(state==FALSE)
{
Button(left,top,left+15,top+15,FALSE);
setcolor(color);
line(left+3,top+11,left+12,top+11);
line(left+3,top+12,left+12,top+12);
}
else
{
Button(left,top,left+15,top+15,TRUE);
setcolor(color);
line(left+3,top+13,left+12,top+13);
line(left+3,top+12,left+12,top+12);
}
}
void WIN::draw_maxbutton(int left,int top,int color,int state)
{
if(state==FALSE)
{
Button(left,top,left+15,top+15,FALSE);
setcolor(color);
rectangle(left+4,top+4,left+12,top+12);
rectangle(left+4,top+5,left+12,top+12);
}
else
{
Button(left,top,left+15,top+15,TRUE);
setcolor(color);
rectangle(left+4,top+4,left+12,top+14);
rectangle(left+4,top+3,left+12,top+14);
}
}
void WIN::draw_closebutton(int left,int top,int color,int state)
{
if(state==FALSE)
{
Button(left,top,left+15,top+15,FALSE);
setcolor(color);
line(left+4,top+4,left+12,top+12);
line(left+5,top+4,left+13,top+12);
line(left+4,top+12,left+12,top+4);
line(left+5,top+12,left+13,top+4);
}
else
{
Button(left,top,left+15,top+15,TRUE);
setcolor(color);
line(left+4,top+4,left+12,top+12);
line(left+5,top+4,left+13,top+12);
line(left+4,top+12,left+12,top+4);
line(left+5,top+12,left+13,top+4);
}
}
/* 时钟源程序 */
/* 仿WINDOWS界面设计 */
/* Turboc3下调试通过 */
/* 包含两个源文件: */
/* clock.cpp win.cpp */
/*******************************/
#include"stdio.h"
#include"stdlib.h"
#include"conio.h"
#include"time.h"
#include"string.h"
#include"math.h"
#include"dos.h"
#include"bios.h"
#include"win.cpp" //调用窗体显示
#define TRUE 1
#define FALSE 0
#define PI 3.1415926
void cursor(int x,int y) //光标
{
int count=0;
while(count<=10){
delay(20);
count++;
if(count<=5)
setcolor(9);
else
setcolor(7);
line(x,y,x,y+12);
line(x+1,y,x+1,y+12);
}
}
int keyscancode() //检测按键
{
int key;
while(bioskey(1)==0) return 0;
key=bioskey(0);
key=key&0xff ? key&0xff:key;
return(key);
}
void message(int x,int y,int n) //状态栏信息
{
char *msg[6]={"Press ESC to quit clock.",
"Press T or t to set time.",
"Press D or d to set date.",
"Press ESC to cancel set.",
"Press Enter to confirm set.",
"Press BackSpace to delete a number."};
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(x,y,x+50*8,y+12);
setcolor(10);
outtextxy(x,y+2,msg[n]);
}
class CLOCK:public WIN //定义CLOCK为WIN的继承类
{
int hour,min,sec;
int year,mon,day;
int x,y,radio;
float ak,aj,ai;
int xs,ys,xm,ym,xh,yh;
public:
CLOCK(char *,int,int,int,int);
int isleapyear(int);
int isweek(int,int);
void showclock();
void runclock();
void setclock();
void inittodaydate();
void initnowtime();
void showdate();
void hidedate();
void pip();
void showtime();
void setdates();
};
//构造函数
CLOCK::CLOCK(char *n,int a,int b,int c,int d):WIN(n,a,b,c,d){
x=a+130;
y=b+155;
radio=120;
}
int CLOCK::isleapyear(int year) //判断是否闰年
{
if(year%100!=0&&year%4==0)
return TRUE;
if(year%400==0)
return TRUE;
return FALSE;
}
int CLOCK::isweek(int year,int mon) //计算year年mon月1号的星期数
{
int days=0,week=0;
int month[12]={31,28,31,30,31,30,31,31,30,31,30,31};
if(isleapyear(year)) month[1]=29;
else month[1]=28;
for(int i=1;i<year;i++)
{
days=365;
if(isleapyear(i)) days=366;
week=(days+week)%7;
}
for(i=0;i<mon-1;i++)
week=(month[i]+week)%7;
return week;
}
void CLOCK::setdates() //设置曰期
{
struct date set;
int key=0,count=0,index=0;
int textx=x+140,texty=y+42,tcnt=0;
char text[2];
int dateset[10];
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(textx,texty,textx+150,texty+12);
do{
message(80+6,380-12,index+3);
count++;
index=count/10;
if(count==29) count=0;
runclock();
key=keyscancode();
cursor(textx,texty);
if((key>='0'&&key<='9')&&tcnt!=10)
{ //限制按键,防止误操作
if(tcnt<4||(tcnt==5&&key<='1')||((tcnt==6&&dateset[tcnt-1]==1&&key<='2')||(tcnt==6&&dateset[tcnt-1]==0))||(tcnt==8&&key<='3')||(tcnt==9))
{
sprintf(text,"%c",key);
text[1]='\0';
setcolor(LIGHTBLUE);
outtextxy(textx,texty+2,text);
dateset[tcnt]=key-'0';
textx+=10;
tcnt++;
if((tcnt==4||tcnt==7)&tcnt!=10){
outtextxy(textx,texty+2,"-");
textx+=10;
tcnt++;
}
}
}
if(key==8&&tcnt>0){
if(tcnt==5||tcnt==8)
{
tcnt-=2;
textx-=20;
}
else{
tcnt--;
textx-=10; }
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(textx,texty,textx+8,texty+12);
}
}while(key!=13&&key!=27);
if(key==27) return; //如果是ESC键,则退出设置
if(key==13) //如果ENTER键,则设置曰期
{
set.da_year=dateset[0]*1000+dateset[1]*100+dateset[2]*10+dateset[3];
set.da_mon=dateset[5]*10+dateset[6];
set.da_day=dateset[8]*10+dateset[9];
setdate(&set);
}
}
void CLOCK::setclock() //设置时间
{
struct time set;
int key=0,count=0,index=0;
int textx=x+140,texty=y+6,tcnt=0;
char text[2];
int timeset[8];
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(textx,texty,textx+150,texty+12);
do{
message(80+6,380-12,index+3);
count++;
index=count/10;
if(count==29) count=0;
key=keyscancode();
cursor(textx,texty);
if((key>='0'&&key<='9')&&tcnt!=8)
{ //限制按键,防止误操作
if((tcnt==0&&key<='2')||((tcnt==1&&key<='3'&×et[tcnt-1]==2)||(tcnt==1&×et[tcnt-1]!=2))||(tcnt==3&&key<='5')||(tcnt==6&&key<='5')||tcnt==4||tcnt==7)
{
sprintf(text,"%c",key);
text[1]='\0';
setcolor(9);
outtextxy(textx,texty+2,text);
timeset[tcnt]=key-'0';
textx+=10;
tcnt++;
if((tcnt==2||tcnt==5)&&tcnt!=8) {
outtextxy(textx,texty+2,":");
textx+=10;
tcnt++;
}
}
}
if(key==8&&tcnt>0){
if(tcnt==3||tcnt==6){
tcnt-=2;
textx-=20;
}
else{
tcnt--;
textx-=10;
}
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(textx,texty,textx+8,texty+12);
}
}while(key!=13&&key!=27);
if(key==27) return;
if(key==13)
{
set.ti_hour=timeset[0]*10+timeset[1];
set.ti_min=timeset[3]*10+timeset[4];
set.ti_sec=timeset[6]*10+timeset[7];
settime(&set);
}
}
void CLOCK::showdate() //在窗体上显示曰期
{
char nowdate[15];
char days[4];
char *week[7]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
int month[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int dayx=x+135,dayy=y-88,todayweek=0,control;
struct date d;
getdate(&d);
setcolor(12);
for(int i=0;i<7;i++)
outtextxy(x+i*8*4+135,y-100,week[i]);
todayweek=isweek(d.da_year,d.da_mon); // 得到本月1号的星期数
if(todayweek+1!=7)
for(i=1;i<=todayweek+1;i++) dayx=dayx+4*8;
control=todayweek+1;
if(isleapyear(d.da_year)) month[1]=29;
else month[1]=28;
setcolor(11);
for(i=1;i<=month[d.da_mon-1];i++)
{
control++;
sprintf(days,"%3d",i);
if(i==d.da_day) setcolor(LIGHTMAGENTA);
outtextxy(dayx,dayy,days);
setcolor(11);
dayx=dayx+4*8;
if(control%7==0) { dayx=x+135; dayy=dayy+12; }
}
todayweek=(todayweek+day)%7; //得到今天的星期数
sprintf(nowdate,"%04d-%02d-%02d %s",d.da_year,d.da_mon,d.da_day,week[todayweek]);
nowdate[14]='\0';
setcolor(LIGHTBLUE);
outtextxy(x+185,y-120,nowdate);
if(d.da_year!=year||d.da_mon!=mon||d.da_day!=day) //如果曰期改变了,则先隐藏曰期,再显示
hidedate();
}
void CLOCK::hidedate() //隐藏曰期
{
inittodaydate();
setfillstyle(SOLID_FILL,LIGHTGRAY); //抹去曰期,如果曰期改变了
bar(x+135,y-90,x+135+6*8*4+3*8,y-90+12*6);
bar(x+165,y-122,x+170+16*8+10,y-112);
}
void CLOCK::showtime() //显示时间
{
char nowtime[9];
sprintf(nowtime,"%02d:%02d:%02d",hour,min,sec);
nowtime[8]='\0';
setcolor(14);
outtextxy(x-30,y+82,nowtime);
ak=(90+30*(12-hour)-min*5/60.*6)*PI/180; //时针角度
aj=(90+6*(60-min))*PI/180; //分针角度
ai=(90+6*(60-sec))*PI/180; //秒针角度
xs=x+cos(ai)*(radio-10);
ys=y-sin(ai)*(radio-10);
xm=x+cos(aj)*(radio-25);
ym=y-sin(aj)*(radio-25);
xh=x+cos(ak)*(radio-45);
yh=y-sin(ak)*(radio-45);
setcolor(RED); //画时针
setlinestyle(0,0,THICK_WIDTH);
line(x-cos(ak)*(radio-118),y+sin(ak)*(radio-118),xh,yh);
setlinestyle(0,0,NORM_WIDTH); //画时针在钟面上指示位置
circle(x+cos(ak)*radio,y-sin(ak)*radio,2);
setcolor(LIGHTGREEN);
circle(x+cos(aj)*radio,y-sin(aj)*radio,2); //画分针在钟面上的指示位置
line(x-cos(aj)*(radio-115),y+sin(aj)*(radio-115),xm,ym); //画分针
setcolor(LIGHTBLUE);
line(x-cos(ai)*(radio-110),y+sin(ai)*(radio-110),xs,ys); //画秒针
circle(x+cos(ai)*radio,y-sin(ai)*radio,2); //画秒针在钟面上的指示位置
}
void CLOCK::runclock() //运行时钟
{
int h,m,s;
float ah;
struct time tim;
gettime(&tim);
s=tim.ti_sec;
m=tim.ti_min;
h=tim.ti_hour;
ah=(90+30*(12-h)-m*5/60.*6)*PI/180;
showdate();
showtime();
delay(200);
nosound();
if(ah!=ak) {
setcolor(LIGHTGRAY);
setlinestyle(0,0,THICK_WIDTH);
line(x-cos(ak)*(radio-118),y+sin(ak)*(radio-118),xh,yh);
setlinestyle(0,0,NORM_WIDTH);
if(hour%5==0) setcolor(YELLOW);
circle(x+cos(ak)*radio,y-sin(ak)*radio,2);
setcolor(LIGHTGRAY);
hour=h;
}
if(m!=min) {
setcolor(LIGHTGRAY);
line(x-cos(aj)*(radio-115),y+sin(aj)*(radio-115),xm,ym);
if(min%5==0) setcolor(YELLOW);
circle(x+cos(aj)*radio,y-sin(aj)*radio,2);
min=m;
}
if(s!=sec) {
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(x-30,y+80,x-30+8*8,y+80+8);
setcolor(LIGHTGRAY);
if(sec%5==0) setcolor(YELLOW);
circle(x+cos(ai)*radio,y-sin(ai)*radio,2);
setcolor(LIGHTGRAY);
line(x-cos(ai)*(radio-110),y+sin(ai)*(radio-110),xs,ys);
sec=s;
if(hour>=12) //整点报时
h=hour-12;
else
h=hour;
if(min==59&&sec>=60-h)
sound(392);
if(min==00&&sec==00)
sound(784);
}
showclock();
}
void CLOCK::inittodaydate() //初始化今天的曰期
{
struct date initdate;
getdate(&initdate);
year=initdate.da_year;
mon=initdate.da_mon;
day=initdate.da_day;
}
void CLOCK::initnowtime() //初始化时间
{
struct time inittime;
gettime(&inittime);
hour=inittime.ti_hour;
min=inittime.ti_min;
sec=inittime.ti_sec;
}
void CLOCK::showclock() //显示时钟
{
float alpha;
int x0,y0,i;
char s[3];
for(i=60;i>=1;i--) //显示钟面上数字
{
alpha=(90+6*(60-i))*PI/180;
x0=x+cos(alpha)*radio;
y0=y-sin(alpha)*radio;
setcolor(14);
if(i%5==0){
circle(x0,y0,2);
sprintf(s,"%d",i/5);
outtextxy(x+cos(alpha)*(radio-10)-4,y-sin(alpha)*(radio-10)-2,s);
}
else
circle(x0,y0,1);
}
}
void about(int x,int y,int index) //关于与帮助信息
{
int i=0,t=0;
char *abouttext[]={"You can get message from",
" the status bar at the ",
"bottom.",
"When you set time,you just",
"input like this:080819.",
"The symbol ':' isn't needed.",
"Set date is the same.",
"This clock is designed by",
" Nie Shiqiu.(C99102-07)",
"I'm very interested in C/C++",
"This is my course design."};
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(x,y,x+27*8+6,y+4*12);
t=index-3;
if(index<4){
y=y+(3-index)*12;
t=0;
}
for(i=t;i<=index;i++)
{
if(i<11){
if(i==8) setcolor(RED); //名字着重显示
else setcolor(LIGHTBLUE);
outtextxy(x+2,y+2,abouttext[i]);
}
y+=12;
}
}
void main() //主程序
{
int driver=DETECT,mode;
int exitflag=FALSE,key,index=0,count=0,aboutindex=0,acnt=0;
WIN winCLK("Clock",80,80,570,385); //创建窗体
CLOCK c1("Clock",80,80,570,385);
initgraph(&driver,&mode,"");
winCLK.Form(FALSE);
c1.showclock();
c1.initnowtime();
do{
about(342,80+185+44,aboutindex);
acnt++;
aboutindex=acnt/10;
if(acnt==139) acnt=0;
message(80+6,380-12,index);
count++;
index=count/10;
if(count==29) count=0;
key=keyscancode();
c1.runclock();
if(key==27) exitflag=TRUE; //按ESC退出
if(key=='T'||key=='t') {
c1.showtime();
c1.setclock();
}
if(key=='D'||key=='d') {
c1.setdates();
}
}while(exitflag==FALSE);
winCLK.draw_closebutton(570-18,80+4,BLACK,TRUE);
delay(300);
closegraph();
}
/*******************/
/* 窗体文件 win.cpp*/
/*******************/
#include"graphics.h"
#define TRUE 1
#define FALSE 0
class WIN
{
char title[20];
protected:
int left,top,right,bottom;
public:
WIN(char *n,int a,int b,int c,int d){strcpy(title,n);left=a;top=b;right=c;bottom=d;}
void draw_minbutton(int left,int top,int color,int state);
void draw_maxbutton(int left,int top,int color,int state);
void draw_closebutton(int left,int top,int color,int state);
void Form(int);
void Button(int,int,int,int,int);
void line3d(int,int,int,int,int);
void Frame(int,int,int,int,char *);
};
void WIN::Frame(int left,int top,int right,int bottom,char *s)
{
line3d(left,top+2,right,top+2,FALSE);
line3d(left,top+3,left,bottom,FALSE);
line3d(left,bottom,right,bottom,FALSE);
line3d(right,top+2,right,bottom,FALSE);
bar(left+4,top,left+4+8*strlen(s)+4,top+8);
setcolor(10);
outtextxy(left+5,top,s);
}
void WIN::line3d(int left,int top,int right,int bottom,int state) /*画有凹凸感的线条函数*/
{
if(state==FALSE){
setcolor(0);
line(left,top,right,bottom);
setcolor(15);
if(top==bottom){
line(left,top+1,right,bottom+1);
}
if(left==right){
line(left+1,top,right+1,bottom);
} }
else
{ setcolor(15);
line(left,top,right,bottom);
setcolor(0);
if(top==bottom){
line(left,top+1,right,bottom+1);
}
if(left==right){
line(left+1,top,right+1,bottom);
} }
}
void WIN::Form(int state)
{
Button(left,top,right,bottom,state);
setfillstyle(1,1);
bar(left+2,top+2,right-2,top+20);
setcolor(YELLOW);
circle(left+12,top+12,8);
line(left+12,top+12,left+12,top+4);
line(left+12,top+12,left+16,top+12);
outtextxy(left+16+10,top+8,title);
draw_minbutton(right-16*3-4,top+4,BLACK,FALSE);
draw_maxbutton(right-16*2-3,top+4,BLACK,FALSE);
draw_closebutton(right-18,top+4,BLACK,FALSE);
Frame(left+5,top+25,left+255,top+278,"Time");
Frame(left+260,top+25,right-5,top+25+20*6,"Date");
line3d(left+280,top+25+20,right-25,top+25+20,FALSE);
Frame(left+260,top+150,right-5,top+150+30,"SetTime(T)");
Button(left+265,top+160,right-8,top+150+25,TRUE);
Frame(left+260,top+185,right-5,top+185+30,"SetDate(D)");
Button(left+265,top+195,right-8,top+185+25,TRUE);
Button(left+4,bottom-20,right-4,bottom-4,TRUE);
Frame(left+260,top+185+35,right-5,bottom-27,"About");
}
void WIN::Button(int left,int top,int right,int bottom,int state)
{
if(state==-1){
setfillstyle(1,7);
bar(left,top,right,bottom);}
setfillstyle(1,7);
bar(left,top,right,bottom);
if(state==FALSE)
{
setcolor(15);
line(left,top,right,top);
line(left,top,left,bottom);
setcolor(0);
line(left,bottom,right,bottom);
line(right,top,right,bottom);
setcolor(8);
line(left+1,bottom-1,right-1,bottom-1);
line(right-1,top+1,right-1,bottom-1);
}
if(state==TRUE)
{
setcolor(8);
line(left,top,right,top);
line(left,top,left,bottom);
setcolor(15);
line(left,bottom,right,bottom);
line(right,top,right,bottom);
}
}
void WIN::draw_minbutton(int left,int top,int color,int state)
{
if(state==FALSE)
{
Button(left,top,left+15,top+15,FALSE);
setcolor(color);
line(left+3,top+11,left+12,top+11);
line(left+3,top+12,left+12,top+12);
}
else
{
Button(left,top,left+15,top+15,TRUE);
setcolor(color);
line(left+3,top+13,left+12,top+13);
line(left+3,top+12,left+12,top+12);
}
}
void WIN::draw_maxbutton(int left,int top,int color,int state)
{
if(state==FALSE)
{
Button(left,top,left+15,top+15,FALSE);
setcolor(color);
rectangle(left+4,top+4,left+12,top+12);
rectangle(left+4,top+5,left+12,top+12);
}
else
{
Button(left,top,left+15,top+15,TRUE);
setcolor(color);
rectangle(left+4,top+4,left+12,top+14);
rectangle(left+4,top+3,left+12,top+14);
}
}
void WIN::draw_closebutton(int left,int top,int color,int state)
{
if(state==FALSE)
{
Button(left,top,left+15,top+15,FALSE);
setcolor(color);
line(left+4,top+4,left+12,top+12);
line(left+5,top+4,left+13,top+12);
line(left+4,top+12,left+12,top+4);
line(left+5,top+12,left+13,top+4);
}
else
{
Button(left,top,left+15,top+15,TRUE);
setcolor(color);
line(left+4,top+4,left+12,top+12);
line(left+5,top+4,left+13,top+12);
line(left+4,top+12,left+12,top+4);
line(left+5,top+12,left+13,top+4);
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询