本文代码均在turboC2.0的环境下编译实现
用指针实现时钟界面:
//模拟指针时钟
//时钟面板 60个刻度,各刻度之间角度为6度
秒针:每秒走6度,60秒旋转 一圈
分针:每十秒走1度,1分钟走6度,60分钟旋转一圈
时针:每2分钟(20*3*2=120秒)走1度,每12分钟走1个刻度(6度)一小时走5个刻度,12小时走60个刻度
#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
#include<bios.h>
#include<ctype.h>
#include<process.h>
#include<conio.h>
#define INTERRUPT 0x1c
int alpha,beta,gama,min;
struct time t;
union REGS i,o;
void(interrupt far * old_handler)();
void hand(int alpha,int pointer,int clr);
void hand(int alpha,int pointer,int clr)
{ int x,y;
int ro;
for(ro=-10;ro<=pointer;ro++)
{ x=ro*cos(alpha*3.14/180)+320;
y=ro*sin(alpha*3.14/180)+240;
putpixel(x,y,getpixel(x,y));
}
//重画指针
setfillstyle(SOLID_FILL,YELLOW);
circle(320,240,2);
floodfill(320,240,YELLOW);
}
//时钟中断服务程序
void interrupt far handler()
{ static int time1=0,timer=0;
int x,y,ro;
time1++;
if(time1>=18)
{timer++;
t.ti_sec++;
hand(alpha,100,11); //调用 hand 程序画秒针
//秒针新走一圈,sipha清零,每秒走6度
alpha=alpha%360+6;
hand(alpha,100,11);
if(t.ti_sec%10==0)
{ t.ti_sec=0;
hand(beta,85,4);
//调用hand程序画分针
//新走一圈后,beta清零每10秒走1度,一分钟走一个刻度
beta=beta%360+1;
hand(beta,85,4);
min++;
//如果时间走过2分钟,则调用 hand程序画时针
if(min>=2*6)
{ hand(gama,65,7);
//时针先走一圈后,gama清零,每2分钟走1度
gama=gama%360+1;
hand(gama,65,7);
min=0;
}
}
//调整中断误差
if(timer==5)
{ timer=0;
time1=-1;
}
else
time1=0;
}
old_handler();
}
main()
{ int ro,j,x,y,driver,mode;
void interrupt far
handler();
i.h.ah=0x0f;
//调用 15号中断功能
int86(0x10,&i,&o);
//调用 16号中断
gettime(&t); //获取当前时间
old_handler=getvect(INTERRUPT); //获取原中断向量地址
setvect(INTERRUPT,handler);
//将新的服务程序地址置于中断入口地址中
//屏显方式初始化
driver=DETECT;
mode=VGAHI;
initgraph(&driver,&mode,"c:\\tc\\bgi");
setbkcolor(BLUE);
cleardevice();
setcolor(YELLOW);
setfillstyle(SOLID_FILL,YELLOW);
alpha=6*t.ti_sec-90;
//将当前秒读数变换为秒针的角度并置于 alpha中
beta=t.ti_min*6+(t.ti_sec/10)-90;
//将当前秒针读数变换为分针的角度并置于 beta中
gama=30*(t.ti_hour-12*(t.ti_hour/12))-90+t.ti_min/2;
//当前小时读数变换为时针的角度并置于 gama
//hua画时针分针秒针
r0=65,85,100
hand(gama,65,7);
hand(beta,85,4);
hand(alpha,100,11);
//画时钟
circle(320,240,109);
for(j=0;j<=360;j++)
{ if(j%6==0&&j%30!=0)
{
x=100*cos(j*3.14/180)+320;
y=100*sin(j*3.14/180)+240;
setcolor(GREEN);
setfillstyle(SOLID_FILL,GREEN);
circle(x,y,1);
floodfill(x,y,GREEN);
}