小时钟代码简易

合集下载

单片机电子闹钟程序亲自编写-可用

单片机电子闹钟程序亲自编写-可用

单片机电子闹钟程序(亲自编写-可用)————————————————————————————————作者:————————————————————————————————日期:学校电子钟,有闹钟功能,按键可调时间,可调打铃时间,打铃时间长短显示,每个模块有功能注释。

其中正常时间显示和闹钟时间显示可用一个开关来调整。

芯片选择STC89C52程序:#include<reg51.h>#include<intrins.h>#define uchar unsigned char#define uint unsigned int//定义显示段码uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00};uchar codebbtime[]={0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; uchar clock[]={0,0,0,0};uchar clock1[]={12,30,0};uchar weikong[6];uchar bbduration=4;uchar lingtime=9;//学校打铃时间组uchar shangwu1[]={8,30};uchar shangwu2[]={10,0};uchar shangwu3[]={10,20};uchar shangwu4[]={11,50};uchar xiawu1[]={13,30};uchar xiawu2[]={15,00};uchar xiawu3[]={15,15};uchar xiawu4[]={16,45};//按键定义sbit mode=P1^7;sbit sec_clr=P1^0;sbit min_set_add=P1^3;sbit min_set_sub=P1^4;sbit hour_set_add=P1^1;sbit hour_set_sub=P1^2;sbit bb_set_add=P1^5;sbit bb_set_sub=P1^6;sbit speaker=P2^6;//延时函数void delay(unsigned int t){while(t--);//时钟进位函数void clockjinwei(){clock[0]++;if(clock[0]==20){clock[1]++;clock[0]=0;if(clock[1]==60){clock[2]++;clock[1]=0;if(clock[2]==60){clock[3]++;clock[2]=0;if(clock[3]==24)clock[3]=0;}}}}//定时器0中断服务函数void timer0(void) interrupt 1 using 1 {TMOD=0x01;TH0=0x3c;TL0=0xb0;clockjinwei();}//时钟分位显示函数void fenwei(){weikong[0]=clock[3]/10;weikong[1]=clock[3]%10;weikong[2]=clock[2]/10;weikong[3]=clock[2]%10;weikong[4]=clock[1]/10;weikong[5]=clock[1]%10;}//闹钟分位显示函数void naofen(){weikong[0]=clock1[0]/10;weikong[1]=clock1[0]%10;weikong[2]=clock1[1]/10;weikong[3]=clock1[1]%10;weikong[4]=clock1[2]/10;weikong[5]=clock1[2]%10; }//闹钟定时显示函数void naozhongdisplay(){uchar z,s;uchar x=0x01;naofen();for(z=0;z<6;z++){P2=0;P0=table[weikong[z]];P2=x;x=_crol_(x,1);for(s=0;s<255;s++);}}//时钟显示函数void display(){uchar i,j;uchar x=0x01;fenwei();for(i=0;i<6;i++){P2=0;P0=table[weikong[i]];P2=x;x=_crol_(x,1);for(j=0;j<255;j++);}}//总显示函数void zhongxian(){if(mode==1)delay(100);if(mode==1)display();if(mode==0)delay(100);if(mode==0)naozhongdisplay();}//按键处理程序void key_set(){zhongxian();P1=0xff;if(min_set_add==0){delay(100);if(min_set_add==0){if(mode==1){clock[2]++;if(clock[2]==60){clock[2]=0;}while(min_set_add==0)zhongxian();}}if(mode==0){clock1[1]++;if(clock1[1]==60){clock1[1]=0;}while(min_set_add==0)zhongxian();}}//if(min_set_sub==0){delay(100);if(min_set_sub==0){if(mode==1){clock[2]--;if(clock[2]==0)clock[2]=59;}while(min_set_sub==0)zhongxian();if(mode==0){clock1[1]--;if(clock1[1]==0)clock1[1]=59;}while(min_set_sub==0)zhongxian();}}//if(hour_set_add==0){delay(100);if(hour_set_add==0){if(mode==1){clock[3]++;if(clock[3]==24){clock[3]=0;}while(hour_set_add==0)zhongxian();}if(mode==0){clock1[0]++;if(clock1[0]==24){clock1[0]=0;}while(hour_set_add==0)zhongxian();}}}//if(hour_set_sub==0){delay(100);if(hour_set_sub==0){if(mode==1){clock[3]--;if(clock[3]==0)clock[3]=23;}while(hour_set_sub==0)zhongxian();if(mode==0){clock1[0]--;if(clock1[0]==0)clock1[0]=23;}while(hour_set_sub==0)zhongxian();}}//if(sec_clr==0){delay(100);if(sec_clr==0){clock[1]=0;}while(sec_clr==0)zhongxian();}}//闹钟响铃函数void bb(){if(clock[1]<=bbduration){speaker=1;delay(100);speaker=0;}else speaker=0;}//打铃函数void daling(){if(clock[1]<=lingtime){speaker=1;delay(100);speaker=0;}else speaker=0;}//时间比较函数void bijiao(){if(clock[3]==shangwu1[0]){if(clock[2]==shangwu1[1])daling();}if(clock[3]==shangwu2[0]){if(clock[2]==shangwu2[1])daling();}if(clock[3]==shangwu3[0]){if(clock[2]==shangwu3[1])daling();}if(clock[3]==shangwu4[0]){if(clock[2]==shangwu4[1])daling();}if(clock[3]==xiawu1[0]){if(clock[2]==xiawu1[1])daling();}if(clock[3]==xiawu2[0]){if(clock[2]==xiawu2[1])daling();}if(clock[3]==xiawu3[0]){if(clock[2]==xiawu3[1])daling();}if(clock[3]==xiawu4[0]){if(clock[2]==xiawu4[1])daling();}}//闹钟比较void naobijiao(){if(clock[3]==clock1[0]){if(clock[2]==clock1[1]||clock[2]==clock1[1]+1||clock[2]==clock1[1]+2) bb();}}//响铃时长显示函数void bbtimeshow(){P3=bbtime[bbduration];if(bbduration>15)bbduration=0;}//响铃按键处理函数void bbtime_set(){bbtimeshow();if(bb_set_add==0){delay(100);if(bb_set_add==0)bbduration++;while(bb_set_add==0)bbtimeshow();}if(bb_set_sub==0){delay(100);if(bb_set_sub==0)bbduration--;while(bb_set_sub==0)bbtimeshow();}}//主程序void main(){EA=1;ET0=1;TR0=1;while(1){key_set();bijiao();bbtime_set();naobijiao();}}电路图:分四部分显示:如果在学习这个程序过程中有什么问题,可以发邮件到询问。

单片机数字时钟24小时制 时间可调

单片机数字时钟24小时制 时间可调
void Interrupt() interrupt 0
{
b=b+1;
if(b==1){ET0=0;TR0=0;e=0;}
if(b==2){e=1;b=0;d=0;TH0=0x3c;TL0=0xb0;TR0=1;ET0=1;}
}
//*************************外部中断程序************************
if(d==1){ if(up==0){delay_ms(2);}
if(up==0){if(hour1<2){hour1=hour1+1;disp_time();}
else{hour1=0;disp_time();}
}
while(!up){disp_time();}
}
if(d==2){if(up==0){delay_ms(2);}
if(up==0){if(hour1<2){if(hour0<9){hour0=hour0+1;disp_time();}
else{hour0=0;disp_time();}}
if(hour1==2){if(hour0<4){hour0=hour0+1;disp_time();}
else{hour0=0;disp_time();}}
if(up==0){if(sec1==6){sec0=0;}
if(sec1<6){if(sec0<9){sec0=sec0+1;disp_time();}
else {sec0=0;disp_time();}}
}
while(!up){disp_time();}
}
}
//*************************键扫描程序**************************

数字钟代码

数字钟代码

数字钟设计一、实验目的:1.熟悉和掌握基本电子计数器及数码显示的工作原理2.能独立的完成基本电子计数器的设计,并由此设计出带一定功能的电子钟二、实验要求设计一个能进行时、分、秒计时的十二小时制或二十四小时制的数字钟,并具有定时与闹钟功能,能在设定的时间发出闹铃音,能非常方便地对小时、分钟和秒进行手动调节以校准时间,每逢整点,产生报时音报时。

三、实验平台:MAX+PLUSII10.1四、实验过程一)、总体思路:这是一个电子钟,其功能如下:可以实现正常的时钟功能,可以调整时间,整点会报时(用显示灯表示),同时还可以设置闹钟,可以显示所设的闹钟值,闹钟有分响铃(用批示灯表示)。

在需要校正时间时,不需要暂停时间,可以像在手机上操作一样,用三个调整键,分别对小时调整(CHANGEH),对分钟的第一位调整(CHANGETM),对分钟的第二位调整(CHANGEM)。

为了更好的操作,加一个UPDOWN键,为1时,则是每次加1,为0时,则是每次减1。

对闹钟进行设置时,不影响正常的计时模块,所以不分影响准确的时间。

对每一部分的操作则是通过总的控制模块来实现的。

整个数字钟的系统框图如下图所示:该数字钟的硬件系统示意图如下图所示:二)、模块及代码和波形图:1、正常计时模块:★、六进制计数器COUNT6的VHDL代码:LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL;USE IEEE.STD_LOGIC_ARITH.ALL;USE IEEE.STD_LOGIC_UNSIGNED.ALL;ENTITY COUNT6 ISPORT( CLK,UPDOWN,CLR,P:IN STD_LOGIC;Q:BUFFER STD_LOGIC_VECTOR(2 DOWNTO 0);COUT:OUT STD_LOGIC);END COUNT6;ARCHITECTURE RTL OF COUNT6 ISBEGINPROCESS(CLK,CLR,P)BEGINIF(CLR='0')THENQ<="000";COUT<='0';ELSIF(CLK'EVENT)AND(CLK='1')THEN IF(P='0')THENIF(Q="101")THENQ<="000";COUT<='1';ELSE Q<=Q+1;COUT<='0';END IF;ELSEIF(UPDOWN='1')THENIF(Q="101")THENQ<="000";COUT<='0';ELSE Q<=Q+1;COUT<='0';END IF;ELSEIF(Q="000")THENQ<="101";COUT<='0';ELSE Q<=Q-1;COUT<='0';END IF;END IF;END IF;END IF;END PROCESS;END RTL;COUNT6波形仿真图:★、十进制计数器COUNT10的VHDL代码:LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL;USE IEEE.STD_LOGIC_ARITH.ALL;USE IEEE.STD_LOGIC_UNSIGNED.ALL;ENTITY COUNT10 ISPORT( CLK,UPDOWN,CLR,P:IN STD_LOGIC;Q:BUFFER STD_LOGIC_VECTOR(3 DOWNTO 0);COUT:OUT STD_LOGIC);END COUNT10;ARCHITECTURE RTL OF COUNT10 IS BEGINPROCESS(CLK,CLR,P)BEGINIF(CLR='0')THENQ<="0000";COUT<='0';ELSIF(CLK'EVENT)AND(CLK='1')THEN IF(P='0')THENIF(Q="1001")THENQ<="0000";COUT<='1';ELSE Q<=Q+1;COUT<='0';END IF;ELSEIF(UPDOWN='1')THENIF(Q="1001")THENQ<="0000";COUT<='1';ELSE Q<=Q+1;COUT<='0';END IF;ELSEIF(Q="0000")THENQ<="1001";COUT<='1';ELSE Q<=Q-1;COUT<='0';END IF;END IF;END IF;END IF;END PROCESS;END RTL;COUNT10的波开仿真图:★、12小时进制的计数器COUNTM代码:LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL;USE IEEE.STD_LOGIC_ARITH.ALL;USE IEEE.STD_LOGIC_UNSIGNED.ALL; ENTITY COUNTM ISPORT( CLK,UPDOWN,CLR,P:IN STD_LOGIC;Q:BUFFER STD_LOGIC_VECTOR(3 DOWNTO 0)); END COUNTM;ARCHITECTURE RTL OF COUNTM ISBEGINPROCESS(CLK,CLR,P)BEGINIF(CLR='0')THENQ<="0000";ELSIF(CLK'EVENT)AND(CLK='1')THENIF(P='0')THENIF(Q="1011")THENQ<="0000";ELSE Q<=Q+1;END IF;ELSEIF(UPDOWN='1')THENIF(Q="1011")THENQ<="0000";ELSE Q<=Q+1;END IF;ELSEIF(Q="0000")THENQ<="1011";ELSE Q<=Q-1;END IF;END IF;END IF;END IF;END PROCESS;END RTL;COUNTM波形图:2、闹钟设置模块:★、六进制闹钟设置模块RING6代码:LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL;USE IEEE.STD_LOGIC_ARITH.ALL;USE IEEE.STD_LOGIC_UNSIGNED.ALL;ENTITY RING6 ISPORT( CLK,UPDOWN,CLR,P:IN STD_LOGIC;Q:BUFFER STD_LOGIC_VECTOR(2 DOWNTO 0);COUT:OUT STD_LOGIC);END RING6;ARCHITECTURE RTL OF RING6 IS BEGINPROCESS(CLK,CLR,P)BEGINIF(CLR='0')THENQ<="000";COUT<='0';ELSIF(CLK'EVENT)AND(CLK='1')THEN IF(P='0')THENCOUT<='0';Q<=Q;ELSEIF(UPDOWN='1')THENIF(Q="101")THENQ<="000";COUT<='0';ELSE Q<=Q+1;COUT<='0';END IF;ELSEIF(Q="000")THENQ<="101";COUT<='0';ELSE Q<=Q-1;COUT<='0';END IF;END IF;END IF;END IF;END PROCESS;END RTL;RING6波形图:★、十进制闹钟设置模块RING10代码:LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL;USE IEEE.STD_LOGIC_ARITH.ALL;USE IEEE.STD_LOGIC_UNSIGNED.ALL; ENTITY RING10 ISPORT( CLK,UPDOWN,CLR,P:IN STD_LOGIC;Q:BUFFER STD_LOGIC_VECTOR(3 DOWNTO 0);COUT:INOUT STD_LOGIC);END RING10;ARCHITECTURE RTL OF RING10 ISBEGINPROCESS(CLK,CLR,P)BEGINIF(CLR='0')THENQ<="0000";COUT<='0';ELSIF(CLK'EVENT)AND(CLK='1')THENIF(P='0')THENCOUT<='0';Q<=Q;ELSEIF(UPDOWN='1')THENIF(Q="1001")THENQ<="0000";COUT<='1';ELSE Q<=Q+1;COUT<='0';END IF;ELSE IF(UPDOWN='0')THENIF(Q="0000")THENQ<="1001";COUT<='1';ELSE Q<=Q-1;COUT<='0';END IF;END IF;END IF;END IF;END IF;END PROCESS;END RTL;RING10波形仿真图:★、闹钟模块12小时进制RING12Y代码LIBRARY IEEE;USE IEEE.STD_LOGIC_1164.ALL;USE IEEE.STD_LOGIC_ARITH.ALL; USE IEEE.STD_LOGIC_UNSIGNED.ALL;ENTITY RING12Y ISPORT( CLK,UPDOWN,CLR,P:IN STD_LOGIC;Q:BUFFER STD_LOGIC_VECTOR(3 DOWNTO 0)); END RING12Y;ARCHITECTURE RTL OF RING12Y ISBEGINPROCESS(CLK,CLR,P)BEGINIF(CLR='0')THENQ<="0000";ELSIF(CLK'EVENT)AND(CLK='1')THENIF(P='0')THENQ<=Q;ELSEIF(UPDOWN='1')THENIF(Q="1011")THENQ<="0000";ELSE Q<=Q+1;END IF;ELSEIF(Q="0000")THENQ<="1011";ELSE Q<=Q-1;END IF;END IF;END IF;END IF;END PROCESS;END RTL;RING12Y波形图:3、整个闹钟模块的测试:电路图:当RS为1时,设置闹钟,可对闹钟进行调整,当UPDOWN为1 时,每一个CLK使相应的计时模块加1 ,当UPDOWN为0时,每一个CLK使相应的模块减1 ;当RS为0时,则定了闹钟。

51单片机时钟代码(带秒表闹钟功能).

51单片机时钟代码(带秒表闹钟功能).
write_com(0x80+0x00+7);
}
}
}
if(s6==0)
{
delay(5);
if(s6==0)
{
while(!s6);
di();
if(s4num==1)
{
miao--;
if(miao<0)
miao=59;
write_alarm(10,miao);
write_com(0x80+0x00+13);
ep=0;
}
voidwrite_data(uchardat) //写入字符显示数据到LCD
{
while(lcd_bz()); //等待LCD空闲
rs=1;
rw=0;
ep=0;
P0=dat;
_nop_();
_nop_();
_nop_();
_nop_();
ep=1;
_nop_();
_nop_();
_nop_();
}
if(s1num==3)
{
hour++;
if(hour==24)
hour=0;
write_time(4,hour);
write_com(0x80+0x40+7);delay(5);
}
}
}
if(s3==0)
{
delay(5);
if(s3==0)
{
while(!s3);
di();
if(s1num==1)
批注本地保存成功开通会员云端永久保存去开通
#include <reg51.h>
#include <intrins.h>

数字时钟代码

数字时钟代码

数字时钟代码1. 介绍数字时钟是一种常见的显示时间的装置,它通过数字显示屏显示当前的小时和分钟。

本文档将介绍如何编写一个简单的数字时钟代码。

2. 代码实现以下是一个基本的数字时钟代码实现示例:import timewhile True:current_time = time.localtime()hour = str(current_time.tm_hour).zfill(2)minute = str(current_time.tm_min).zfill(2)second = str(current_time.tm_sec).zfill(2)clock_display = f"{hour}:{minute}:{second}"print(clock_display, end="\r")time.sleep(1)代码说明:- `time.localtime()` 函数返回当前时间的结构化对象,包括小时、分钟和秒等信息。

- `str(current_time.tm_hour).zfill(2)` 将小时转换为字符串,并使用 `zfill()` 方法填充到两位数。

- `str(current_time.tm_min).zfill(2)` 和`str(current_time.tm_sec).zfill(2)` 同理处理分钟和秒。

- 使用 f-string 格式化字符串 `clock_display`,将小时、分钟和秒显示为 `` 的形式。

- `print(clock_display, end="\r")` 使用 `\r` 实现覆盖打印,使得时钟在同一行连续显示。

- `time.sleep(1)` 让程序每隔一秒更新一次时间。

请注意,上述代码需要在支持 Python 的环境中运行。

3. 结束语通过以上的代码实现,我们可以编写一个简单的数字时钟。

时钟代码及注释

时钟代码及注释

HOUR EQU 36HMIN EQU 37HSEC EQU 38HCHOUR1 EQU 39H;定义3个闹钟的时分CMIN1 EQU 40HCHOUR2 EQU 41HCMIN2 EQU 42HCHOUR3 EQU 43HCMIN3 EQU 44HORG 0000H;设定各个中断入口LJMP MIANORG 0003HLJMP W AIZD0ORG 000BHLJMP DINGSHIORG 0030HMIAN:MOV TMOD,#02H;使用定时器模式2 MOV TH0,#06H;设定初值为250usMOV TL0,#06HSETB TR0SETB EA;打开各个中断SETB ET0SETB EX0SETB PT0;定时器0优先级最高MOV HOUR,#0MOV MIN,#0MOV SEC,#0MOV P0,#0FFHMOV CHOUR1,#6;闹钟1设置默认数值MOV CMIN1,#0MOV CHOUR2,#8;闹钟2设置默认数值MOV CMIN2,#0MOV CHOUR3,#14;闹钟3设置默认数值MOV CMIN3,#0D0: ACALL XIANSHISECACALL XIANSHIMINACALL XIANSHIHOURACALL JIANGEACALL CLOCKZAJMP D0;要循环扫描才能动态显示CLOCKZ:;闹钟的调用函数MOV A,HOUR;闹钟一点判断CJNE A,CHOUR1,CN2;判断小时到了没有小时MOV A,MINCJNE A,CMIN1,CN2;判断分钟到时没SETB P3.4;开闹钟ACALL DELAYCLR P3.4CN2: MOV A,HOUR;闹钟2点判断CJNE A,CHOUR2,CN3;判断小时到了没有小时MOV A,MINCJNE A,CMIN2,CN3;判断分钟到时没SETB P3.4;开闹钟ACALL DELAYCLR P3.4CN3: MOV A,HOUR;闹钟2点判断CJNE A,CHOUR3,CNO;判断小时到了没有小时MOV A,MINCJNE A,CMIN3,CNO;判断分钟到时没SETB P3.4;开闹钟ACALL DELAYCLR P3.4CNO: RETSHUJU:INC 31HMOV R0,31HCJNE R0,#40,OUT0;时间达到10ms则执行下一行程序,否则返回OUT0 MOV 31H,#0MOV R0,#0INC 32HMOV R1,32HCJNE R1,#100,OUT0;时间达到1s则执行下一行程序,否则返回OUT0MOV 32H,#0MOV R1,#0INC SECMOV R2,SECCJNE R2,#60,OUT0;时间达到60s则执行下一行程序,否则返回OUT0MOV R2,#0MOV SEC,#0INC MINMOV R3,MINCJNE R3,#60,OUT0;时间达到60min则执行下一行程序,否则返回OUT0 MOV R3,#0MOV MIN,#0INC HOURCJNE R4,#24,OUT0;时间达到24h则执行下一行程序,否则返回OUT0 MOV R4,#0MOV HOUR,#0OUT0:RETJIANGE: MOV DPTR,#TABLE ;-的显示MOV A,#10;11为table编码的序地址MOVC A,@A+DPTRMOV P1,AMOV P2,#24HACALL DELAYMOV P1,#0FFHRETXIANSHISEC:MOV DPTR,#TABLEMOV A,SECMOV B,#10DIV ABMOVC A,@A+DPTRMOV P1,A;这是为位选,就是要显示的数字选?MOV P2,#02H;这是段段选,是控制哪一位,显示第一位ACALL DELAYMOV P1,#0FFHMOV A,BMOVC A,@A+DPTRMOV P1,AMOV P2,#01HACALL DELAYMOV P1,#0FFHRETXIANSHIMIN:MOV DPTR,#TABLEMOV A,MINMOV B,#10DIV ABMOVC A,@A+DPTRMOV P1,AMOV P2,#10HACALL DELAYMOV P1,#0FFHMOV A,BMOVC A,@A+DPTRMOV P1,AACALL DELAYMOV P1,#0FFHRETXIANSHIHOUR:MOV DPTR,#TABLEMOV A,HOURMOV B,#10DIV ABMOVC A,@A+DPTRMOV P1,AMOV P2,#80HACALL DELAYMOV P1,#0FFHMOV A,BMOVC A,@A+DPTRMOV P1,AMOV P2,#40HACALL DELAYMOV P1,#0FFHRETXIANCLOCK1:;闹钟1显示函数MOV DPTR,#TABLEMOV A,CHOUR1;显示闹钟小时MOV B,#10DIV ABMOVC A,@A+DPTR;显示十位MOV P1,AMOV P2,#80HACALL DELAYMOV P1,#0FFHMOV A,BMOVC A,@A+DPTR;显示个位MOV P1,AMOV P2,#40HACALL DELAYMOV P1,#0FFHMOV DPTR,#TABLE ;间隔-的显示MOV A,#10;11为table编码的序地址MOVC A,@A+DPTRMOV P1,AMOV P2,#24HACALL DELAYMOV P1,#0FFHMOV DPTR,#TABLE;显示闹钟1的分钟MOV A,CMIN1MOV B,#10DIV ABMOVC A,@A+DPTR;显示十位MOV P1,AMOV P2,#10HACALL DELAYMOV P1,#0FFHMOV A,BMOVC A,@A+DPTR;显示个位MOV P1,AMOV P2,#08HACALL DELAYMOV P1,#0FFHMOV DPTR,#TABLE ;间隔C的显示MOV A,#11;11为table编码C的地址MOVC A,@A+DPTRMOV P1,AMOV P2,#02HACALL DELAYMOV P1,#0FFHMOV DPTR,#TABLE ;间隔1的显示MOV A,#1MOVC A,@A+DPTRMOV P1,AMOV P2,#01HACALL DELAYMOV P1,#0FFHRETXIANCLOCK2:;闹钟2显示函数MOV DPTR,#TABLEMOV A,CHOUR2;显示闹钟小时MOV B,#10DIV ABMOVC A,@A+DPTR;显示十位MOV P1,AMOV P2,#80HACALL DELAYMOV P1,#0FFHMOV A,BMOVC A,@A+DPTR;显示个位MOV P1,AMOV P2,#40HACALL DELAYMOV P1,#0FFHMOV DPTR,#TABLE ;间隔-的显示MOV A,#10;11为table编码的序地址MOVC A,@A+DPTRMOV P1,AMOV P2,#24HACALL DELAYMOV P1,#0FFHMOV DPTR,#TABLE;显示闹钟3的分钟MOV A,CMIN2MOV B,#10DIV ABMOVC A,@A+DPTR;显示十位MOV P1,AMOV P2,#10HACALL DELAYMOV P1,#0FFHMOV A,BMOVC A,@A+DPTR;显示个位MOV P1,AMOV P2,#08HACALL DELAYMOV P1,#0FFHMOV DPTR,#TABLE ;间隔C的显示MOV A,#11;11为table编码C的地址MOVC A,@A+DPTRMOV P1,AMOV P2,#02HACALL DELAYMOV P1,#0FFHMOV DPTR,#TABLE ;间隔2的显示MOV A,#2MOVC A,@A+DPTRMOV P1,AMOV P2,#01HACALL DELAYMOV P1,#0FFHRETXIANCLOCK3:;闹钟1显示函数MOV DPTR,#TABLEMOV A,CHOUR3;显示闹钟小时MOV B,#10DIV ABMOVC A,@A+DPTR;显示十位MOV P1,AMOV P2,#80HACALL DELAYMOV P1,#0FFHMOV A,BMOVC A,@A+DPTR;显示个位MOV P1,AMOV P2,#40HACALL DELAYMOV P1,#0FFHMOV DPTR,#TABLE ;间隔-的显示MOV A,#10;11为table编码的序地址MOVC A,@A+DPTRMOV P1,AMOV P2,#24HACALL DELAYMOV P1,#0FFHMOV DPTR,#TABLE;显示闹钟3的分钟MOV A,CMIN3MOV B,#10DIV ABMOVC A,@A+DPTR;显示十位MOV P1,AMOV P2,#10HACALL DELAYMOV P1,#0FFHMOV A,BMOVC A,@A+DPTR;显示个位MOV P1,AMOV P2,#08HACALL DELAYMOV P1,#0FFHMOV DPTR,#TABLE ;间隔C的显示MOV A,#11;11为table编码C的地址MOVC A,@A+DPTRMOV P1,AMOV P2,#02HACALL DELAYMOV P1,#0FFHMOV DPTR,#TABLE ;间隔3的显示MOV A,#3MOVC A,@A+DPTRMOV P1,AMOV P2,#01HACALL DELAYMOV P1,#0FFHRETDINGSHI:MOV TH0,#06H;初值为250usMOV TL0,#06HACALL SHUJURETISETKEY:JNB P3.7,$ACALL DELAYSETHOUR:ACALL XIANSHIHOURJB P3.1,HOURJIANJNB P3.1,$INC HOURMOV A,HOURCJNE A,#24,HOURJIANMOV HOUR,#0HOURJIAN:JB P3.0,OUTHJNB P3.0,$ACALL DELAYDEC HOURMOV A,HOURCJNE A,#-1,OUTHMOV HOUR,#23OUTH: JB P3.7,SETHOURJNB P3.7,$ACALL DELAYSETMIN: ACALL XIANSHIMINJB P3.1,MINJIANJNB P3.1,$INC MINMOV A,MINCJNE A,#60,MINJIANMOV MIN,#0MINJIAN:JB P3.0,OUTH0JNB P3.0,$ACALL DELAYDEC MINMOV A,MINCJNE A,#-1,OUTH0MOV MIN,#59OUTH0: JB P3.7,SETMINJNB P3.7,$;ACALL DELAYRET;闹钟子设置程序SETCLOCK:C1SHI: ACALL XIANCLOCK1;调用闹钟一显示函数JB P3.1,C1JIAN;如果被按下则顺序执行,不按下则跳转判断减法按键JNB P3.1,$;松手检测ACALL DELAYINC CHOUR1;按下则闹钟1的时自加1MOV A,CHOUR1CJNE A,#24,C1JIANMOV CHOUR1,#0C1JIAN: JB P3.0,DCCC1;按下则执行自减1JNB P3.0,$DEC CHOUR1;自减1ACALL DELAYMOV A,CHOUR1CJNE A,#-1,DCCC1MOV CHOUR1,#23DCCC1: JB P3.7,C1SHI;切换键按下则执行下面的分钟设置,不按下则跳转回时闹钟时的设置JNB P3.7,$ACALL DELAYC1FEN: ACALL XIANCLOCK1JB P3.1,JIANFEN1;如果被按下则顺序执行,不按下则跳转判断减法按键JNB P3.1,$;松手检测ACALL DELAYINC CMIN1;按下则闹钟1的时自加1MOV A,CMIN1CJNE A,#60,JIANFEN1;判断是否为60MOV CMIN1,#0JIANFEN1:JB P3.0,SETC2;不按下则转到闹钟2设置JNB P3.0,$ACALL DELAYDEC CMIN1MOV A,CMIN1CJNE A,#-1,SETC2MOV CMIN1,#59SETC2: JB P3.7,C1FEN;切换键按下则执行闹钟2JNB P3.7,$ACALL DELAY;闹钟2C2SHI: ACALL XIANCLOCK2;调用闹钟一显示函数JB P3.1,C1JIAN2;如果被按下则顺序执行,不按下则跳转判断减法按键JNB P3.1,$;松手检测ACALL DELAYINC CHOUR2;按下则闹钟2的时自加1MOV A,CHOUR2CJNE A,#24,C1JIAN2MOV CHOUR2,#0C1JIAN2:JB P3.0,DCCC2;按下则执行自减1JNB P3.0,$DEC CHOUR2;自减1ACALL DELAYMOV A,CHOUR2CJNE A,#-1,DCCC2MOV CHOUR2,#23DCCC2: JB P3.7,C2SHI;切换键按下则执行下面的分钟设置,不按下则跳转回时闹钟时的设置JNB P3.7,$ACALL DELAYC2FEN: ACALL XIANCLOCK2JB P3.1,JIANFEN2;如果被按下则顺序执行,不按下则跳转判断减法按键JNB P3.1,$;松手检测ACALL DELAYINC CMIN2;按下则闹钟2的时自加1MOV A,CMIN2CJNE A,#60,JIANFEN2MOV CMIN2,#0JIANFEN2:JB P3.0,SETC22;不按下则转到闹钟2设置JNB P3.0,$ACALL DELAYDEC CMIN2MOV A,CMIN2CJNE A,#-1,SETC22MOV CMIN1,#59SETC22: JB P3.7,C2FEN;切换键按下则向下执行闹钟三。

Java数字时钟(简单的桌面应用)

Java数字时钟(简单的桌面应用)

import java.util.*;/*****该程序是一个简单的数字时钟,每变化一秒,颜色随机变色,可以系统托盘,最大的特点是可以和桌面形成一体,也就是容纳这个数字时钟的窗体可以看成是透明的***********/import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.awt.image.BufferedImage;public class app509 extends JFrame{static int i=-1,geli=-1;/*这一部分是系统托盘图标的文件路径,可以自己设置,只要路径正确即可,可以是gif,jpg,png格式*/static Image image=Toolkit.getDefaultToolkit().getImage("D:1\\3.png");static SystemTray systemTray=SystemTray.getSystemTray();static PopupMenu pop01=new PopupMenu();static MenuItem MI01=new MenuItem("打开主程序");static MenuItem MI02=new MenuItem("退出程序");static MenuItem MI03=new MenuItem("隐藏");static TrayIcon trayIcon=new TrayIcon(image,"这是程序图标",pop01);static JLabel beijingtu=new JLabel();static JLabel xingqi=new JLabel(" ",JLabel.CENTER);static JLabel jlabel02=new JLabel("年",JLabel.CENTER);static JLabel jlabel03=new JLabel("月",JLabel.CENTER);static JLabel jlabel04=new JLabel("日",JLabel.CENTER);static JLabel jlabel05=new JLabel("分",JLabel.CENTER);static JLabel jlabel10=new JLabel("时",JLabel.CENTER);static JLabel jlabel12=new JLabel("分",JLabel.CENTER);static JLabel jlabel13=new JLabel("秒",JLabel.CENTER);static JLabel jlabel06=new JLabel(" ",JLabel.CENTER);static JLabel jlabel07=new JLabel(" ",JLabel.CENTER);static JLabel jlabel08=new JLabel(" ",JLabel.CENTER);static JLabel jlabel09=new JLabel(" ",JLabel.CENTER);static JLabel jlabel11=new JLabel(" ",JLabel.CENTER);static JLabel jlabel=new JLabel(" ",JLabel.CENTER);static JLabel jbData[]={jlabel13,jlabel02,jlabel03,jlabel04,jlabel10,jlabel12};static JLabel jbData02[]={jlabel,jlabel07,jlabel08,jlabel09,jlabel11,jlabel06};static int mill=0;static int minute=0;static int hour=0;static int day=0;static int month=0;static int year=0;static int week;static int zuobiaoX,zuobiaoY;static JFrame JF01=new JFrame();static JDialog JF=new JDialog(JF01," ");static Robot robot;static BufferedImage image1;static Rectangle rec;static class mouseListener extends MouseAdapter{public void mouseClicked(MouseEvent a){if(a.getSource()==trayIcon){if(a.getClickCount()==2){i++;if(i%2==1){geli++;if(geli%2==1){image1=robot.createScreenCapture(rec);beijingtu.setIcon(new ImageIcon(image1));JF.setBounds(0,0,120,560);JF.setVisible(true);}}else{JF.setBounds(0,0,400,1);}}}}public void mouseEntered(MouseEvent a){if(a.getSource()==JF){image1=robot.createScreenCapture(rec);beijingtu.setIcon(new ImageIcon(image1));JF.setBounds(0,0,120,560);JF.setVisible(true);}}public void mouseExited(MouseEvent a){if(a.getSource()==JF){JF.setBounds(0,0,400,1);}}}public static void main(String args[]) throws Exception{trayIcon.addMouseListener(new mouseListener());rec=new Rectangle(0,0,(int)Toolkit.getDefaultToolkit().getScreenSize().getWidth(),(int)Toolkit.getDefaultToolkit().getScreenSize().getHeight());try{robot=new Robot(); }catch(Exception b){}image1=robot.createScreenCapture(rec);beijingtu.setIcon(new ImageIcon(image1));MI01.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent a){Image1=robot.createScreenCapture(rec);beijingtu.setIcon(new ImageIcon(image1));JF.setBounds(0,0,120,560);JF.setVisible(true);}});MI03.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent a){JF.setBounds(0,0,400,1);}});MI02.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent a){System.exit(0);}});try{pop01.add(MI01);pop01.add(MI03);pop01.add(MI02);systemTray.add(trayIcon);trayIcon.setImageAutoSize(true);trayIcon.addMouseListener(new mouseListener());}catch(Exception a){} JF.setResizable(false) ;JF.addMouseListener(new mouseListener());JF.setUndecorated(true);beijingtu.setBounds(0,0,(int)Toolkit.getDefaultToolkit().getScreenSize().getWidth(),(int)Toolkit.getDefaultToolkit().getScreenSize().getHeight());JF.setLayout(null);JF.setBounds(0,0,120,560);JF.setVisible(true);jlabel02.setBounds(91,94,24,25);jlabel06.setBounds(15,94,64,28);jlabel03.setBounds(91,175,24,25);jlabel07.setBounds(2,125,86,75);jlabel04.setBounds(91,261,24,25);jlabel08.setBounds(2,210,86,75);jlabel10.setBounds(91,346,24,25);jlabel09.setBounds(2,296,86,75);jlabel11.setBounds(2,382,86,75);jlabel12.setBounds(91,433,24,25);jlabel13.setBounds(91,520,24,25);jlabel.setBounds(2,468,86,75);xingqi.setBounds(2,30,118,62);JF.add(xingqi);xingqi.setHorizontalTextPosition(JLabel.CENTER);xingqi.setFont(new Font("微软雅黑",Font.BOLD,20));for(int i=0;i<jbData.length;i++){JF.add(jbData[i]);JF.add(jbData02[i]);}for(int i=0;i<jbData.length;i++){jbData[i].setFont(new Font("微软雅黑",Font.BOLD,15));jbData02[i].setFont(new Font("微软雅黑",Font.BOLD,30));}jlabel06.setFont(new Font("微软雅黑",Font.BOLD,15));for(int i=0;i<jbData.length;i++){jbData[i].setForeground(Color.blue);jbData02[i].setForeground(Color.red);}for(int i=0;i<jbData.length;i++){jbData[i].setHorizontalTextPosition(JLabel.CENTER);jbData02[i].setHorizontalTextPosition(JLabel.CENTER);}jlabel02.setHorizontalTextPosition(JLabel.RIGHT);JF.add(beijingtu);xiancheng xiancheng01=new xiancheng();xiancheng01.start();}}class xiancheng extends Thread{static GregorianCalendar date=new GregorianCalendar();app509 app=new app509();public void run(){for(int i=0;i<60;){try{sleep(1000);}catch(Exception a){}app.year=(date=new GregorianCalendar()).get(date.YEAR);app.jlabel06.setText(Integer.toString(app.year));app.month=((date=new GregorianCalendar()).get(date.MONTH)+1);app.jlabel07.setText(Integer.toString(app.month));app.day=(date=new GregorianCalendar()).get(date.DAY_OF_MONTH);app.jlabel08.setText(Integer.toString(app.day));app.week=(date=new GregorianCalendar()).get(date.DAY_OF_WEEK);app.hour=(date=new GregorianCalendar()).get(date.HOUR_OF_DAY);app.jlabel09.setText(Integer.toString(app.hour));app.minute=(date=new GregorianCalendar()).get(date.MINUTE);app.jlabel11.setText(Integer.toString(app.minute));l=(date=new GregorianCalendar()).get(date.SECOND);app.jlabel.setText(Integer.toString(l));if(app.jlabel.getText()!=" "){app.xingqi.setForeground(new Color((int)(255*Math.random()),(int)(255*Math.random()),(int)(255*Math.random())));for(int j=0;j<app.jbData.length;j++){app.jbData[j].setForeground(new Color((int)(255*Math.random()),(int)(255*Math.random()),(int)(255*Math.random())));app.jbData02[j].setForeground(new Color((int)(255*Math.random()),(int)(255*Math.random()),(int)(255*Math.random())));}} switch(app.week){case 1 : app.xingqi.setText("星期日");break;case 2 : app.xingqi.setText("星期一");break;case 3 : app.xingqi.setText("星期二");break;case 4 : app.xingqi.setText("星期三");break;case 5 : app.xingqi.setText("星期四");break;case 6 : app.xingqi.setText("星期五");break;case 7 : app.xingqi.setText("星期六");break;}System.gc();}}}/****复制以上代码进行编译即可*****/程序效果图:。

简单时钟的电脑编程代码

简单时钟的电脑编程代码

以下是一个简单的使用Python和turtle库来创建一个模拟时钟的代码。

这个时钟将显示当前的时间,并且每秒更新一次。

python复制代码import turtleimport timewin = turtle.Screen()win.setup(width=600, height=600)win.bgcolor("black")# 创建一个新的Turtle对象clock_turtle = turtle.Turtle()clock_turtle.hideturtle()# 循环每秒更新一次时间while True:# 获取当前时间current_time = time.strftime("%H:%M:%S")# 清空屏幕win.clear()# 画时钟的边框clock_turtle.penup()clock_turtle.goto(-200, -200)clock_turtle.pendown()clock_turtle.circle(400)# 画时钟的小时数clock_turtle.penup()clock_turtle.goto(-100, -150)clock_turtle.pendown()clock_turtle.setheading(0)clock_turtle.forward(200)clock_turtle.setheading(90)clock_turtle.forward(100)clock_turtle.setheading(180)clock_turtle.forward(200)clock_turtle.setheading(270)clock_turtle.forward(100)# 画时钟的分钟数clock_turtle.penup()clock_turtle.goto(-50, -150)clock_turtle.pendown()clock_turtle.setheading(0)clock_turtle.forward(150)clock_turtle.setheading(90)clock_turtle.forward(100)clock_turtle.setheading(180)clock_turtle.forward(150)clock_turtle.setheading(270)clock_turtle.forward(100)# 画时钟的秒钟数clock_turtle.penup()clock_turtle.goto(0, -150)clock_turtle.pendown()clock_turtle.setheading(0)clock_turtle.forward(120)clock_turtle.setheading(90)clock_turtle.forward(100)clock_turtle.setheading(180)clock_turtle.forward(120)clock_turtle.setheading(270)clock_turtle.forward(100)# 显示时间文本clock_turtle.penup()clock_turtle.goto(-150, -80)clock_turtle.pendown()clock_turtle.color("white")clock_turtle.write(current_time, font=("Arial", 24, "normal")) # 显示时间文本,字体大小为24,正常样式显示文本。

51单片机秒表程序设计

51单片机秒表程序设计

51单片机秒表程序设计1. 简介秒表是一种用于测量时间间隔的计时器,常见于体育比赛、实验室实验等场合。

本文将介绍如何使用51单片机设计一个简单的秒表程序。

2. 硬件准备•51单片机开发板•LCD液晶显示屏•按键开关•连接线3. 程序流程3.1 初始化设置1.设置LCD液晶显示屏为8位数据总线模式。

2.初始化LCD液晶显示屏。

3.设置按键开关为输入模式。

3.2 主程序循环1.显示初始界面,包括“00:00:00”表示计时器初始值。

2.等待用户按下开始/暂停按钮。

3.如果用户按下开始按钮,则开始计时,进入计时状态。

4.如果用户按下暂停按钮,则暂停计时,进入暂停状态。

5.在计时状态下,每隔1毫秒更新计时器的数值,并在LCD液晶显示屏上显示出来。

6.在暂停状态下,不更新计时器的数值,并保持显示当前数值。

3.3 计时器控制1.定义一个变量time用于存储当前的计时器数值,单位为毫秒。

2.定义一个变量running用于标记计时器的状态,0表示暂停,1表示运行。

3.定义一个变量start_time用于存储计时器开始的时间点。

4.定义一个变量pause_time用于存储计时器暂停的时间点。

5.在计时状态下,每隔1毫秒更新time的值为当前时间与start_time的差值,并将其转换为小时、分钟、秒的表示形式。

6.在暂停状态下,保持time的值不变。

3.4 按键检测1.检测按键开关是否被按下。

2.如果按键被按下,判断是开始/暂停按钮还是复位按钮。

3.如果是开始/暂停按钮,并且当前处于计时状态,则将计时状态设置为暂停状态,并记录暂停时间点为pause_time;如果当前处于暂停状态,则将计时状态设置为运行状态,并记录开始时间点为当前时间减去暂停时间的差值。

4.如果是复位按钮,则将计时器数值重置为0,并将计时状态设置为暂停。

4. 程序代码示例#include <reg51.h>// 定义LCD控制端口和数据端口sbit LCD_RS = P1^0;sbit LCD_RW = P1^1;sbit LCD_EN = P1^2;sbit LCD_D4 = P1^3;sbit LCD_D5 = P1^4;sbit LCD_D6 = P1^5;sbit LCD_D7 = P1^6;// 定义按键开关端口sbit START_PAUSE_BTN = P2^0;sbit RESET_BTN = P2^1;// 定义全局变量unsigned int time = 0; // 计时器数值,单位为毫秒bit running = 0; // 计时器状态,0表示暂停,1表示运行unsigned long start_time = 0; // 开始时间点unsigned long pause_time = 0; // 暂停时间点// 函数声明void delay(unsigned int ms);void lcd_init();void lcd_command(unsigned char cmd);void lcd_data(unsigned char dat);void lcd_string(unsigned char *str);void lcd_clear();void lcd_gotoxy(unsigned char x, unsigned char y);// 主函数void main() {// 初始化设置lcd_init();while (1) {// 显示初始界面lcd_clear();lcd_gotoxy(0, 0);lcd_string("00:00:00");// 等待用户按下开始/暂停按钮while (!START_PAUSE_BTN && !RESET_BTN);// 判断按钮类型并处理计时器状态if (START_PAUSE_BTN) {if (running) { // 当前处于计时状态,按下按钮将进入暂停状态 running = 0;pause_time = time;} else { // 当前处于暂停状态,按下按钮将进入计时状态running = 1;start_time = get_current_time() - pause_time;}} else if (RESET_BTN) { // 复位按钮按下,重置计时器time = 0;running = 0;}}}// 毫秒级延时函数void delay(unsigned int ms) {unsigned int i, j;for (i = ms; i > 0; i--) {for (j = 110; j > 0; j--);}}// LCD初始化函数void lcd_init() {lcd_command(0x38); // 设置8位数据总线模式lcd_command(0x0C); // 显示开,光标关闭lcd_command(0x06); // 光标右移,不移动显示器lcd_command(0x01); // 清屏}// 向LCD发送指令函数void lcd_command(unsigned char cmd) {LCD_RS = 0;LCD_RW = 0;LCD_EN = 1;LCD_D4 = cmd >> 4 & 1;LCD_D5 = cmd >> 5 & 1;LCD_D6 = cmd >> 6 & 1;LCD_D7 = cmd >> 7 & 1;delay(1);LCD_EN = 0;LCD_D4 = cmd >> 0 & 1;LCD_D5 = cmd >> 1 & 1;LCD_D6 = cmd >> 2 & 1;LCD_D7 = cmd >> 3 & 1;delay(1);LCD_EN = 0;}// 向LCD发送数据函数void lcd_data(unsigned char dat) { LCD_RS = 1;LCD_RW = 0;LCD_EN = 1;LCD_D4 = dat >> 4 & 1;LCD_D5 = dat >> 5 & 1;LCD_D6 = dat >> 6 & 1;LCD_D7 = dat >> 7 & 1;delay(1);LCD_EN = 0;LCD_D4 = dat >> 0 & 1;LCD_D5 = dat >> 1 & 1;LCD_D6 = dat >> 2 & 1;LCD_D7 = dat >> 3 & 1;delay(1);LCD_EN = 0;}// 向LCD发送字符串函数void lcd_string(unsigned char *str) {while (*str) {lcd_data(*str++);delay(5);}}// 清屏函数void lcd_clear() {lcd_command(0x01);}// 设置光标位置函数void lcd_gotoxy(unsigned char x, unsigned char y) {unsigned char addr;if (y == 0)addr = x | (0x80 + y);else if (y == 1)addr = x | (0xC0 + y);lcd_command(addr);}5. 总结本文介绍了使用51单片机设计一个简单的秒表程序。

html5时钟实现代码

html5时钟实现代码

html5时钟实现代码复制代码代码如下:<!DOCTYPE HTML><html><head><meta charset="UTF-8"><style type="text/css">canvas{position:absolute;top:0px;left:0px;}</style><title>时钟</title></head><body><canvas id="canvas" width="200" height="200"></canvas><canvas id="p_canvas" width="200" height="200"></canvas><script type="text/javascript">//获取绘图对象var canvas = document.getElementById('canvas');var context = canvas.getContext('2d');var p_canvas = document.getElementById('p_canvas');var p_context = p_canvas.getContext('2d');var height=200,width=200;//画⼤圆context.beginPath();context.strokeStyle="#009999";context.arc(width/2,height/2,width/2-1,0,Math.PI*2,true);context.stroke();context.closePath();//画中间点context.beginPath();context.fillStyle="#000";context.arc(width/2,height/2,3,0,Math.PI*2,true);context.fill();context.closePath();//画⼩刻度var angle = 0,radius = width/2 - 4;for(var i=0;i<60;i++){context.beginPath();context.strokeStyle="#000";//确认刻度的起始点var x = width/2 + radius*Math.cos(angle),y = height/2 + radius*Math.sin(angle); context.moveTo(x,y);//这⾥是⽤来将刻度的另⼀点指向中⼼点,并给予正确的⾓度//PI是180度,正确的⾓度就是 angle+180度,正好相反⽅向var temp_angle = Math.PI +angle;context.lineTo(x +3*Math.cos(temp_angle),y+3*Math.sin(temp_angle)); context.stroke();context.closePath();angle+=6/180*Math.PI;}//⼤刻度angle = 0,radius = width/2 - 4;context.textBaseline = 'middle';context.textAlign = 'center';context.lineWidth = 2;for(var i=0;i<12;i++){var num = i+3>12? i+3-12:i+3 ;context.beginPath();context.strokeStyle="#FFD700";var x = width/2 + radius*Math.cos(angle),y = height/2 + radius*Math.sin(angle); context.moveTo(x,y);var temp_angle = Math.PI +angle;context.lineTo(x +8*Math.cos(temp_angle),y+8*Math.sin(temp_angle));context.stroke();context.closePath();//⼤刻度⽂字context.fillText(num,x+16*Math.cos(temp_angle),y+16*Math.sin(temp_angle));angle+=30/180*Math.PI;}function Pointer(){var p_type = [['#000',70,1],['#ccc',60,2],['red',50,3]];function drwePointer(type,angle){type = p_type[type];angle = angle*Math.PI*2 - 90/180*Math.PI;var length= type[1];p_context.beginPath();p_context.lineWidth = type[2];p_context.strokeStyle = type[0];p_context.moveTo(width/2,height/2);p_context.lineTo(width/2 + length*Math.cos(angle),height/2 + length*Math.sin(angle)); p_context.stroke();p_context.closePath();}setInterval(function (){p_context.clearRect(0,0,height,width);var time = new Date();var h = time.getHours();var m = time.getMinutes();var s = time.getSeconds();h = h>12?h-12:h;h = h+m/60;h=h/12;m=m/60;s=s/60;drwePointer(0,s);drwePointer(1,m);drwePointer(2,h);},500);}var p = new Pointer();</script></body></html>。

基于JavaScript的电子时钟效果实现

基于JavaScript的电子时钟效果实现

基于JavaScript的电子时钟效果实现一、引言在网页设计中,实现电子时钟效果是一种常见的需求,通过展示一个动态的时钟可以增加网页的互动性和吸引力。

在本文中,我们将使用JavaScript来实现一个基于HTML和CSS的电子时钟效果。

二、技术原理实现一个电子时钟的关键在于获取当前时间并动态展示在页面上。

在JavaScript中,我们可以使用Date对象来获取当前时间,并通过定时器setInterval来不断更新页面上的时间显示。

三、实现步骤1. 编写HTML代码我们需要先创建一个HTML页面,用来展示我们的电子时钟。

下面是一个简单的HTML 结构:```html<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>电子时钟</title><link rel="stylesheet" href="style.css"></head><body><div class="clock"><div class="hour-hand"></div><div class="minute-hand"></div><div class="second-hand"></div></div><script src="script.js"></script></body></html>```在上面的HTML代码中,我们创建了一个div元素,用来容纳我们的时钟。

简单的51单片机时钟程序

简单的51单片机时钟程序

简单的51单片机时钟程序,可以通过按键来设置时间,按键可以自己更改。

#include<reg52.h>#define uint unsigned int#define uchar unsigned char#define tt 46080 //设置时间间隔,对应11.0592MHZ的晶振uchar code table[]="Happy every day!";uchar code table1[]="00:00:00";uchar num,hh,mm,ss,t,s1num=0;sbit en=P3^4;sbit rs=P3^5;sbit rw=P3^6;sbit s1=P3^0;sbit s2=P3^1;sbit s3=P3^2;//按键所用的端口sbit s4=P3^3;void delay(uint z){uint x,y;for(x=z;x>0;x--)for(y=110;y>0;y--); //大约是1ms,因为单片机的时钟周期为11.0592mhz。

}void write_com(uchar com){rs=0; //指令P0=com; //写指令函数delay(1);en=1;delay(1);en=0;}void write_data(uchar dat){rs=1; //数据P0=dat; //写指令函数delay(1);en=1;delay(1);en=0;}void init(){en=0; //初始时使能为0rw=0;write_com(0x38); //显示屏模式设置为1602方案write_com(0x0c);write_com(0x06); //显示开关/光标设置write_com(0x01); //清屏write_com(0x80); //指针置零for(num=0;num<16;num++)write_data(table[num]);write_com(0xc3);for(num=0;num<8;num++)write_data(table1[num]);}void dingshi(){TMOD=0x01; //确定定时器工作模式(定时模式)TH0=(65536-tt)/256; //赋初值为tt微秒TL0=(65536-tt)%256; //不赋值时默认其值是0EA=1; //开总中断ET0=1; //开定时器0中断// IE=0x82; //总线写法TR0=1; //启动定时器0 总线TCON=0x10;}void shuanxin(uchar add,uchar date){uchar shi,ge;write_com(0xc3+add); //指针指向shi=date/10;ge=date%10;write_data(0x30+shi);write_data(0x30+ge); //指针自动后移,故不必再写指针位置}/***************借助蜂鸣器接地起作用***************/ void keyscan(){if(s1==0){delay(5);if(s1==0){s1num++;while(!s1);if(s1num==1){TR0=0; //时钟停止运行write_com(0xca); //指针指向sswrite_com(0x0f); //光标闪烁}if(s1num==2){write_com(0xc7); //指针指向mmwrite_com(0x0f);}if(s1num==3){write_com(0xc4); //指针指向hhwrite_com(0x0f);}if(s1num==4){s1num=0;TR0=1; //时钟运行write_com(0x0c); //取消闪烁}}}/***************调节时间****************/if(s1num!=0) //目的是使s1按下的前提才起作用{if(s2==0){delay(5);if(s2==0){while(!s2); //松手检测,松手后方可向下执行if(s1num==1){ss++;if(ss==60)ss=0;shuanxin(6,ss);write_com(0xca);}if(s1num==2){mm++;if(mm==60)mm=0;shuanxin(3,mm);write_com(0xc7);}{hh++;if(hh==24)hh=0;shuanxin(0,hh);write_com(0xc4);}}}}if(s1num!=0) //s1按下的前提才起作用{if(s3==0){delay(5);if(s3==0){while(!s3);if(s1num==1){ss--;ss=59;shuanxin(6,ss);write_com(0xca);}if(s1num==2){mm--;if(mm==-1)mm=59;shuanxin(3,mm);write_com(0xc7);}if(s1num==3){hh--;if(hh==-1)hh=23;shuanxin(0,hh);write_com(0xc4);}}}}if(s1num!=0) //s1按下的前提才起作用{if(s4==0){delay(5);if(s4==0){while(!s4);if(s1num==1){ss=0;shuanxin(6,ss);write_com(0xca);}if(s1num==2){mm=0;shuanxin(3,mm);write_com(0xc7);}if(s1num==3){hh=0;shuanxin(0,hh);write_com(0xc4);}}}}}void main(){init();dingshi();while(1){keyscan();if(t==20){P1=P1-1;t=0;ss++;if(ss==60){ss=0;mm++;if(mm==60){mm=0;hh++;if(hh==24){hh=0;}shuanxin(0,hh);}shuanxin(3,mm);}shuanxin(6,ss);}}}void time0() interrupt 1{TH0=(65536-tt)/256; //不赋值时默认其值是0 TL0=(65536-tt)%256;t++;}。

Python+Pyqt实现简单GUI电子时钟

Python+Pyqt实现简单GUI电子时钟

Python+Pyqt实现简单GUI电⼦时钟本⽂实例为⼤家分享了Python+Pyqt实现简单GUI电⼦时钟的具体代码,供⼤家参考,具体内容如下突发奇想想⽤GUI做⼀个简单的电⼦时钟界⾯,利⽤pyqt模块也很⽅便,代码如下:from PyQt5.QtGui import *from PyQt5.QtCore import *from PyQt5.QtWidgets import QWidget,QApplication,QLCDNumber,QVBoxLayout,QMessageBox,QPushButtonimport sysimport timeclass MyTime(QWidget):def __init__(self):super().__init__()self.initUI()self.init_timer()#UI界⾯搭建def update_time(self):self.lcd.display(time.strftime('%X', time.localtime()))def init_timer(self):self.timer=QTimer()self.timer.setInterval(1000)#设置定时器 1S触发⼀次self.timer.start()#启动定时器self.timer.timeout.connect(self.update_time)def initUI(self):self.resize(400,200)self.setWindowTitle("创意时钟")###名称self.setWindowIcon(QIcon('xiaomayun.jpg'))#图标#初始化调⾊板self.pl=QPalette()self.pl.setColor(QPalette.Background,Qt.darkYellow)self.setAutoFillBackground(True)self.setPalette(self.pl)#设置顶层布局self.lcd=QLCDNumber() #初始化lcdself.lcd.setDigitCount(10)#设置数字个数self.lcd.setMode(QLCDNumber.Dec)#数字⼗进制self.lcd.setSegmentStyle(QLCDNumber.Flat)#平⾯模式self.lcd.display(time.strftime('%X',time.localtime()))##初始化盒⼦布局self.box_layout=QVBoxLayout()self.box_layout.addWidget(self.lcd)#添加LCD组件self.box_layout.setAlignment(Qt.AlignCenter)#设置组件在布局中间self.setLayout(self.box_layout)#设置窗体布局self.btn = QPushButton('Button', self)##创建按钮测试⽤self.btn.setToolTip('This is a <b>QPushButton</b> widget')self.btn.resize(self.btn.sizeHint())self.btn.move(50, 50)self.btn.clicked.connect(self.on_click)self.box_layout.addWidget(self.btn)# btn.move(50, 50)self.qbtn = QPushButton('Quit', self)self.qbtn.clicked.connect(QCoreApplication.instance().quit)self.qbtn.resize(self.qbtn.sizeHint())self.qbtn.move(300, 150)self.show()"""创建⿏标点击事件"""def on_click(self):print("PyQt5 button click")def closeEvent(self, event):reply = QMessageBox.question(self, 'Message',"Are you sure to quit?", QMessageBox.Yes |QMessageBox.No, QMessageBox.No)# 第⼀个字符串的内容被显⽰在标题栏上。

完整51单片机多功能电子钟程序代码

完整51单片机多功能电子钟程序代码

完整51单片机多功能电子钟程序代码#include "regx52.h"#define uint8 unsigned charcode uint8 LED[] = {0xc0, 0xf9, 0xa4, 0xb0,0x99, 0x92, 0x82, 0xf8,0x80, 0x90, 0x88, 0x83,0xc6, 0xa1, 0x86, 0x8e}; unsigned char riqi[8]; static uint8 keyReleas;uint8 disptmp;uint8 pos;uint8 buffer[8];code uint8 BIT[8] = {0xfe, 0xfd, 0xfb, 0xf7,0xef, 0xdf, 0xbf, 0x7f}; uint8 flag2ms;uint8 t2msCnt;uint8 halfSecCnt;uint8 IsTimerRun;uint8 mode;void GlobalvarInit();void T0Init();void TimeRun();unsigned char KeyScan();void Command();void Command();void T0IntSvr() interrupt 1 using 1{TL0 = (65536-2000)%256;TH0 = (65536-2000)/256;switch(mode){case 0:disptmp=LED[buffer[pos]]; switch(pos){case 2:case 4:case 6:disptmp&=~(1<<7); break;}break;case 1:disptmp=LED[riqi[pos]]; switch(pos){case 2:case 4:disptmp&=~(1<<7); break;}break;}P2 = 0xff;P0 = disptmp;P2 = BIT[pos];pos++;if(pos>7)pos = 0;flag2ms = 1;}void main(){GlobalvarInit();T0Init();EA = 1;while(1){TimeRun();Command();}}void TimeRun(){if(IsTimerRun == 0) return; if(flag2ms != 1) return; flag2ms = 0;t2msCnt++;if(t2msCnt < 250) return; t2msCnt = 0; halfSecCnt++;if(halfSecCnt < 2) return; halfSecCnt = 0;buffer[0]++;if(buffer[0] < 10) return; buffer[0] = 0;buffer[1]++;if(buffer[1] < 6) return; buffer[1] = 0;buffer[2]++;if(buffer[2] < 10) return;buffer[2] = 0;buffer[3]++;if(buffer[3] < 6) return;buffer[3] = 0;buffer[4]++;switch(buffer[5]){case 0:case 1:if(buffer[4] < 10) return; buffer[4] = 0;break; case 2: if(buffer[4] < 4) return; buffer[4] = 0;break; }buffer[5]++;if(buffer[5] < 3) return;buffer[5] = 0;riqi[0]++;switch(riqi[1]){case 0:if( riqi[0] < 10) return; riqi[0] = 1;break; case 1: case 2:if( riqi[0] < 10) return; riqi[0] = 0;break; case 3:if( riqi[0] < 2) return; riqi[0] = 0;break;}riqi[1]++;if(riqi[1]<4) return;riqi[1]=0;riqi[2]++;switch(riqi[3]){case 0:if( riqi[2] < 10) return; riqi[2] = 1;break;case 1:if( riqi[2] < 3) return; riqi[2] = 0;break; }riqi[3]++;if(riqi[3]<1) return;riqi[3]=0;riqi[4]++;if(riqi[4]<10) return;riqi[4]=0;riqi[5]++;if(riqi[5]<10) return;riqi[5]=0;riqi[6]++;if(riqi[6]<10) return;riqi[6]=0;riqi[7]++;if(riqi[7]<10) return;riqi[7]=0;}void T0Init(){TMOD = TMOD & 0xf0 | 0x01;// 2msTL0 = (65536-2000)%256;TH0 = (65536-2000)/256;ET0 = 1;TR0 = 1;}// 全局变量初始化void GlobalvarInit(){uint8 i;for (i = 0; i < 8; i++){buffer[i] = 0;}pos = 1;flag2ms = 0;t2msCnt = 0; IsTimerRun = 1;mode = 0;riqi[0]= 0;riqi[1]= 2;riqi[2]=6;riqi[3]=0;riqi[4]=3;riqi[5]=1;riqi[6]=0;riqi[7]=2;}unsigned char KeyScan() { unsigned char key;P3 = 0x0f;if ((P3 & 0x0f) == 0x0f) {return 0;}P3 = 0x7f;key = P3;key = key & 0x0f;if(key != 0x0f){return (key | 0x70);}P3 = 0xbf;key = P3;key = key & 0x0f;if(key != 0x0f){return (key | 0xb0);}P3 = 0xdf;key = P3;key = key & 0x0f;if(key != 0x0f){return (key | 0xd0);}P3 = 0xef;key = P3;key = key & 0x0f;if(key != 0x0f){return (key | 0xe0);}return 0;}static uint8 keyRelease = 1; void Key_bd(){buffer[7] = 0x0b; buffer[6] = 0x0d; }void Key_be() {switch(mode) {case 0:mode = 1; break;case 1:mode = 0; break;}}void Key_0x77() {if(buffer[4]>3) {buffer[5]++;if(buffer[5]>1) buffer[5]=0;}else{buffer[5]++;if(buffer[5]>2) buffer[5]=0;}}void Key_0x7b(){switch(buffer[5]){case 0:case 1:buffer[4]++;if(buffer[4]>9)buffer[4]=0; break;case 2:buffer[4]++;if(buffer[4]>3)buffer[4]=0;break; }}void Key_0xee(){switch(riqi[1]){case 0: riqi[0]++;if(riqi[0]>9) riqi[0]=1;break;case 1:case 2:riqi[0]++;if(riqi[0]>9) riqi[0]=0;break;case 3:riqi[0]++;if(riqi[0]>1)riqi[0]=0;break;}}void Key_0xeb() {switch(riqi[3]){case 0:riqi[2]++;if(riqi[2]>9) riqi[2]=1; break;case 1:riqi[2]++;if(riqi[2]>2) riqi[2]=0; break;}}void Key_0xed() {if(riqi[0]<=1){riqi[1]++;if(riqi[1]>3)riqi[1]=0;}if(riqi[0]>1){riqi[1]++;if(riqi[1]>2)riqi[1]=0;}}void Key_0xe7(){if(riqi[2]>2)riqi[3]=0;else{riqi[3]++;if(riqi[3]>1)riqi[3]=0;}}void Command(){uint8 key;key = KeyScan();if (key == 0){keyRelease = 1;return;}if(keyRelease == 0)return;keyRelease = 0;switch(key){case 0xbe: Key_be(); break; // Key_8 case 0xbd: Key_bd(); break; // Key_9 case 0xbb: IsTimerRun = 1; break;// Key_A case 0xb7: IsTimerRun = 0; break;// Key_Bcase 0x77: Key_0x77();break;// Key_Fcase 0x7b: Key_0x7b();break;// Key_Ecase 0x7d: buffer[3]++;if(buffer[3]>5)buffer[3]=0;;break;// Key_D case 0x7e: buffer[2]++;if(buffer[2]>9)buffer[2]=0;;break;// Key_C case 0xde: riqi[4]++;if(riqi[4]>9)riqi[4]=0; break;// Key_4 case 0xdd: riqi[5]++;if(riqi[5]++>9)riqi[5]=0; break;// Key_5 case 0xdb: riqi[6]++;if(riqi[6]>9)riqi[6]=0; break;// Key_6case 0xd7: riqi[7]++;if(riqi[7]>9)riqi[7]=0; break;// Key_7case 0xee: Key_0xee(); break;// Key_0case 0xed: Key_0xed();break;// Key_1case 0xeb: Key_0xeb(); break;// Key_2case 0xe7: Key_0xe7();break;// Key_3}}。

css3一个简易的LED数字时钟实现方法

css3一个简易的LED数字时钟实现方法

css3⼀个简易的LED数字时钟实现⽅法这个应该是已经有很多⼈做过的东西,我应该只是算⼿痒,想写⼀下,所以,花了点时间折腾了这个,顺便把 Dark Mode 的处理也加上了。

⾸先可以很明确的⼀点,这个真没技术含量存在,只是需要点耐⼼。

LED 数字包含了左右各两条线,中间三条线,⼀共 7 条线。

所以,为了能够更容易辨识,在写 demo 的时候,我直接这样写了。

<div class="digital digital_0"><span class="c1"></span><span class="c2"></span><span class="c3"></span><span class="l1"></span><span class="l2"></span><span class="r1"></span><span class="r2"></span></div>⾄于 digital_0 这个的作⽤,其实很简单,就是控制从 0 ~ 9 这⼗个数字的变化⽽存在的。

.digital_1 .c1,.digital_1 .c2,.digital_1 .c3,.digital_1 .l1,.digital_1 .l2,.digital_2 .l1,.digital_2 .r2,.digital_3 .l1,.digital_3 .l2,.digital_4 .c1,.digital_4 .c3,.digital_4 .l2,.digital_5 .l2,.digital_5 .r1,.digital_6 .r1,.digital_7 .c2,.digital_7 .c3,.digital_7 .l1,.digital_7 .l2,.digital_9 .l2,.digital_0 .c2 {animation: changeDigital 200ms 0ms 1 ease-in forwards;}这⾥使⽤了⼀个 animation 动画,并且时间是 200ms,主要是为了让某些部分消失的时候,有⼀个过渡效果。

(完整word版)桌面时钟代码

(完整word版)桌面时钟代码

<HTML〉<HEAD〉〈TITLE〉动态数字时钟</TITLE〉〈SCRIPT language=”javascript">function digitalClock(){var today = new Date();digit_clock.innerText = today.toLocaleString();setTimeout(”digitalClock()", 1000 );}〈/SCRIPT〉〈LINK rel="styl esheet” type=”text/css" href="js。

css"〉〈/HEAD><BODY>〈H1 align="center" id=”digit_clock">〈/H1>〈SCRIPT language=”javascript"〉digitalClock();</SCRIPT><SCRIPT LANGUAGE="JavaScript”>var S = '。

..。

.';S = S.split('');//构成秒针的圆点var M = '*****'; M = M.split('');//构成分针的圆点var H = '****’;H = H.split('’);//构成时针的圆点var Ybase = 8; //构成秒、分和时针的圆点之间的间隔距离(Y轴)var Xbase = 8; //构成秒、分和时针的圆点之间的间隔距离(X轴)var dots = 12;//钟盘上的小时刻度数,通常为12,即1,2,3,.。

,12for (var i = 0; i 〈 dots; i++){//将小时刻度值(即1,2,3,。

桌面时钟制作原代码

桌面时钟制作原代码

// AlarmSetDlg.cpp : implementation file//#include "stdafx.h"#include "deskclock.h"#include "AlarmSetDlg.h"#include "BeepSetDlg.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif///////////////////////////////////////////////////////////////////////////// // AlarmSetDlg dialogAlarmSetDlg::AlarmSetDlg(CWnd* pParent /*=NULL*/): CDialog(AlarmSetDlg::IDD, pParent){//{{AFX_DATA_INIT(AlarmSetDlg)m_bSound = TRUE;m_bMusic = 0;m_strAlarmMusic = _T("");m_pickerTime = 0;//}}AFX_DATA_INITSYSTEMTIME time;GetLocalTime(&time);m_pickerTime = time;m_hour = time.wHour;m_minute = time.wMinute;m_second = time.wSecond;}void AlarmSetDlg::DoDataExchange(CDataExchange* pDX){CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(AlarmSetDlg)DDX_Text(pDX, IDC_HO, m_hour);DDX_Text(pDX, IDC_MI, m_minute);DDX_Text(pDX, IDC_SE, m_second);DDX_Check(pDX, IDC_BMUSIC, m_bSound);DDX_Radio(pDX, IDC_MUSIC, m_bMusic);DDX_Text(pDX, IDC_PATHMUSIC, m_strAlarmMusic);DDX_DateTimeCtrl(pDX, IDC_DATETIMEPICKER2, m_pickerTime);//}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(AlarmSetDlg, CDialog)//{{AFX_MSG_MAP(AlarmSetDlg)ON_BN_CLICKED(IDC_MUSIC, OnMusic)ON_BN_CLICKED(IDC_BEEP, OnBeep)ON_BN_CLICKED(IDC_BMUSIC, OnBmusic)ON_BN_CLICKED(IDC_SMUSIC, OnSelectMusic)ON_BN_CLICKED(IDC_SBEEP, OnSelectBeep)//}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// AlarmSetDlg message handlersBOOL AlarmSetDlg::OnInitDialog(){CDialog::OnInitDialog();// TODO: Add extra initialization hereSetEnable();return TRUE; // return TRUE unless you set the focus to a control// EXCEPTION: OCX Property Pages should return FALSE }void AlarmSetDlg::SetEnable(){if(m_bSound){GetDlgItem(IDC_MUSIC)->EnableWindow(true);GetDlgItem(IDC_BEEP)->EnableWindow(true);if(!m_bMusic){GetDlgItem(IDC_PATHMUSIC)->EnableWindow(true);GetDlgItem(IDC_SMUSIC)->EnableWindow(true);GetDlgItem(IDC_SBEEP)->EnableWindow(false);}else{GetDlgItem(IDC_PATHMUSIC)->EnableWindow(false);GetDlgItem(IDC_SMUSIC)->EnableWindow(false);GetDlgItem(IDC_SBEEP)->EnableWindow(true);}}else{GetDlgItem(IDC_PATHMUSIC)->EnableWindow(false);GetDlgItem(IDC_SMUSIC)->EnableWindow(false);GetDlgItem(IDC_SBEEP)->EnableWindow(false);GetDlgItem(IDC_MUSIC)->EnableWindow(false);GetDlgItem(IDC_BEEP)->EnableWindow(false);}}void AlarmSetDlg::OnMusic(){// TODO: Add your control notification handler code hereUpdateData(true);SetEnable();}void AlarmSetDlg::OnBeep(){// TODO: Add your control notification handler code hereUpdateData(true);SetEnable();}void AlarmSetDlg::OnBmusic(){// TODO: Add your control notification handler code hereUpdateData(true);SetEnable();}void AlarmSetDlg::OnSelectMusic(){// TODO: Add your control notification handler code hereCFileDialog dlg(TRUE,"wave",NULL,NULL,"wave file(*.wav)|*.WAV",NULL);if (dlg.DoModal()==IDOK){m_strAlarmMusic = dlg.GetPathName();UpdateData(false);}}void AlarmSetDlg::OnSelectBeep(){// TODO: Add your control notification handler code hereBeepSetDlg dlg;dlg.m_typeBeep = m_typeBeep;if(dlg.DoModal() == IDOK){m_typeBeep = dlg.m_typeBeep;}}// BackSetDlg.cpp : implementation file//#include "stdafx.h"#include "deskclock.h"#include "BackSetDlg.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif///////////////////////////////////////////////////////////////////////////// // BackSetDlg dialogBackSetDlg::BackSetDlg(CWnd* pParent /*=NULL*/): CDialog(BackSetDlg::IDD, pParent){//{{AFX_DATA_INIT(BackSetDlg)m_bPicture = 0;m_strBk = _T("");m_bBkStre = -1;//}}AFX_DATA_INIT}void BackSetDlg::DoDataExchange(CDataExchange* pDX){CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(BackSetDlg)DDX_Radio(pDX, IDC_COLOR, m_bPicture);DDX_Text(pDX, IDC_EDITPICTURE, m_strBk);DDX_Radio(pDX, IDC_CENTER, m_bBkStre);//}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(BackSetDlg, CDialog)//{{AFX_MSG_MAP(BackSetDlg)ON_BN_CLICKED(IDC_COLOR, OnColor)ON_BN_CLICKED(IDC_RADIO2, OnRadio2)ON_WM_PAINT()ON_BN_CLICKED(IDC_BKCOLOR, OnBkcolor)ON_BN_CLICKED(IDC_BKPICTURE, OnBkpicture)//}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// BackSetDlg message handlersBOOL BackSetDlg::OnInitDialog(){CDialog::OnInitDialog();// TODO: Add extra initialization hereSetEnable();return TRUE; // return TRUE unless you set the focus to a control// EXCEPTION: OCX Property Pages should return FALSE }void BackSetDlg::SetEnable(){if(!m_bPicture){GetDlgItem(IDC_BKCOLOR)->EnableWindow(true);GetDlgItem(IDC_BKPICTURE)->EnableWindow(false);GetDlgItem(IDC_EDITPICTURE)->EnableWindow(false);GetDlgItem(IDC_STRE)->EnableWindow(false);GetDlgItem(IDC_CENTER)->EnableWindow(false);}else{GetDlgItem(IDC_BKCOLOR)->EnableWindow(false);GetDlgItem(IDC_BKPICTURE)->EnableWindow(true);GetDlgItem(IDC_EDITPICTURE)->EnableWindow(true);GetDlgItem(IDC_STRE)->EnableWindow(true);GetDlgItem(IDC_CENTER)->EnableWindow(true);}}void BackSetDlg::OnColor(){// TODO: Add your control notification handler code hereUpdateData(true);SetEnable();}void BackSetDlg::OnRadio2(){// TODO: Add your control notification handler code hereUpdateData(true);SetEnable();}void BackSetDlg::OnPaint(){CPaintDC dc(this); // device context for paintingCPen penBorderH(PS_SOLID,1,RGB(200,200,200));CPen *ppenOld = dc.SelectObject(&penBorderH);CBrush brPointH(m_colorBk);CBrush* pbrOld = dc.SelectObject(&brPointH);dc.Rectangle(70,105,130,135);}void BackSetDlg::OnBkcolor(){// TODO: Add your control notification handler code hereCColorDialog dlg;dlg.m_cc.Flags|=CC_RGBINIT ;dlg.m_cc.rgbResult=m_colorBk;if(IDOK==dlg.DoModal()){m_colorBk = dlg.m_cc.rgbResult;Invalidate();}}void BackSetDlg::OnBkpicture(){// TODO: Add your control notification handler code hereCFileDialog dlg(TRUE,"bmp",NULL,NULL,"bmp file(*.bmp)|*.BMP|",NULL);if (dlg.DoModal()==IDOK){m_strBk = dlg.GetPathName();UpdateData(false);}}// BeepSetDlg.cpp : implementation file//#include "stdafx.h"#include "deskclock.h"#include "BeepSetDlg.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// BeepSetDlg dialogBeepSetDlg::BeepSetDlg(CWnd* pParent /*=NULL*/): CDialog(BeepSetDlg::IDD, pParent){//{{AFX_DATA_INIT(BeepSetDlg)m_typeBeep = -1;//}}AFX_DATA_INITm_times = 5;}void BeepSetDlg::DoDataExchange(CDataExchange* pDX){CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(BeepSetDlg)DDX_Radio(pDX, IDC_XIAOJIUWO, m_typeBeep);//}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(BeepSetDlg, CDialog)//{{AFX_MSG_MAP(BeepSetDlg)ON_BN_CLICKED(IDC_XIAOJIUWO, OnXiaoJiuWo)ON_BN_CLICKED(IDC_UP, OnUp)ON_BN_CLICKED(IDC_DOWN, OnDown)ON_BN_CLICKED(IDC_C1, OnC1)ON_BN_CLICKED(IDC_D1, OnC2)ON_BN_CLICKED(IDC_E1, OnC3)ON_BN_CLICKED(IDC_F1, OnC4)ON_BN_CLICKED(IDC_G1, OnC5)//}}AFX_MSG_MAPEND_MESSAGE_MAP()///////////////////////////////////////////////////////////////////////////// // BeepSetDlg message handlersvoid BeepSetDlg::OnXiaoJiuWo(){// TODO: Add your control notification handler code hereUpdateData(true);ClockBeep();}void BeepSetDlg::OnUp(){// TODO: Add your control notification handler code hereUpdateData(true);ClockBeep();}void BeepSetDlg::OnDown(){// TODO: Add your control notification handler code here UpdateData(true);ClockBeep();}void BeepSetDlg::OnC1(){// TODO: Add your control notification handler code here UpdateData(true);ClockBeep();}void BeepSetDlg::OnC2(){// TODO: Add your control notification handler code here UpdateData(true);ClockBeep();}void BeepSetDlg::OnC3(){// TODO: Add your control notification handler code here UpdateData(true);ClockBeep();}void BeepSetDlg::OnC4(){// TODO: Add your control notification handler code here UpdateData(true);ClockBeep();}void BeepSetDlg::OnC5(){// TODO: Add your control notification handler code here UpdateData(true);ClockBeep();}void BeepSetDlg::ClockBeep(){yf a[1000]={{z3,125},{z3,50},{z4,100},{z3,50},{z2,50}, //5 {z2,125},{z2,50},{z5,100},{z2,50},{z1,50}, //10{z1,125},{z1,50},{z6,100},{z5,100}, //14{z3,125},{z4,25},{z3,25},{z2,100},{z2,100}, //19{z3,125},{z3,50},{z4,100},{z5,50},{z3,50}, //24{z5,125},{z5,50},{g2,100},{z7,50},{g1,50}, //29{g1,200},{g1,50},{z7,50},{z6,50},{z7,50}, //34{g1,125},{z7,50},{g1,200}, //37{g1,50},{z7,50},{g1,50},{z7,50},{g1,100},{z3,50},{z5,50}, //44{z5,250},{z6,50},{z7,50}, //47{g1,50},{z7,50},{g1,50},{z7,50},{g1,100},{g2,100},{g3,100}, //54{g3,300},{z6,50},{z7,50}, //57{g1,50},{z7,50},{g1,50},{z7,50},{g1,100},{g3,100}, //63{z7,50},{z6,50},{z7,50},{z6,50},{z7,50},{z5,50},{z5,50},{g1,50}, //71 {g1,200},{g3,75},{g4,25},{g4,50},{g3,50}, //76{g2,200},{yaya,50},{z5,50},{g1,50},{g2,50}, //81{g3,50},{g1,50},{g1,50},{z5,50},{z5,100},{z5,50},{g3,50}, //88{g2,50},{g3,50},{g2,50},{g1,50},{z5,50},{z5,50},{z6,50},{z7,50}, //96 {g1,50},{z6,50},{z6,50},{z3,50},{z3,100},{yaya,50},{z7,50}, //103{g1,50},{z7,50},{g1,50},{g2,50},{z6,50},{z5,50},{z5,100}, //110{z6,100},{z5,50},{z6,50},{z7,100},{g3,50},{g4,50}, //116{g3,50},{g4,50},{g3,50},{g2,50},{g2,50},{g1,100},{z5,50}, //123{z5,50},{z4,50},{z4,50},{g1,50},{g1,50},{g3,50},{g3,50},{g1,50}, //131 {g2,200},{yaya,50},{z5,50},{g1,50},{g2,50}, //136{g3,50},{g1,50},{g1,50},{z5,50},{z5,100},{z5,50},{g3,50}, //143{g2,50},{g3,50},{g2,50},{g1,50},{z5,50},{z5,50},{z6,50},{z7,50}, //151 {g1,50},{z6,50},{z6,50},{z3,50},{z3,100},{z3,50},{z7,50}, //158{g1,50},{z7,50},{g1,50},{g2,50},{z6,50},{z5,50},{z5,100}, //165{z6,50},{z7,50},{g1,50},{g2,100},{g3,50},{g4,50}, //171{g3,50},{g4,50},{g2,50},{g1,50},{g2,50},{g1,50},{g1,100}, //178{g2,50},{g1,50},{g2,50},{z6,50},{g1,100},{g2,50},{g1,50}, //185{g1,400}, //186{g2,50},{g1,50},{g2,50},{z6,50},{z7,100},{z7,50},{g1,50}, //193{g1,400}, //194};int i = 0;switch(m_typeBeep){case 0:yf *atop;atop=a;while(m_times--){Beep(atop->s,atop->t*5);atop++;}break;case 1:for(i = 5 ; i < 5 + m_times; i++){Beep(i * 70,50);Sleep(200);}break;case 2:for(i = 5 + m_times ; i > 5; i--){Beep(i * 70,50);Sleep(200);}break;case 3:for(i = 0 ; i < m_times; i++){Beep(262,50);Sleep(200);}break;case 4:for(i = 0 ; i < m_times; i++){Beep(294,50);Sleep(200);}break;case 5:for(i = 0 ; i < m_times; i++){Beep(330,50);Sleep(200);}break;case 6:for(i = 0 ; i < m_times; i++){Beep(349,50);Sleep(200);}break;case 7:for(i = 0 ; i < m_times; i++){Beep(392,50);Sleep(200);}break;}}// ClockDate.cpp: implementation of the CClockDate class.////////////////////////////////////////////////////////////////////////#include "stdafx.h"#include "deskclock.h"#include "ClockDate.h"#ifdef _DEBUG#undef THIS_FILEstatic char THIS_FILE[]=__FILE__;#define new DEBUG_NEW#endif////////////////////////////////////////////////////////////////////// // Construction/Destruction//////////////////////////////////////////////////////////////////////CClockDate::CClockDate(){m_dColor = RGB(0,255,0);m_wColor = RGB(0,255,0);m_position = 0;m_inType = 1;m_outType = 1;m_bUp = false;}CClockDate::~CClockDate(){}void CClockDate::DrawDate(CDC *pDc, CRect rectClient, CTime oTime) {SetPosition(rectClient);UINT nYear,nMonth, nDay,nDayOfWeek;nYear = oTime.GetYear();nMonth = oTime.GetMonth();nDay = oTime.GetDay();CString strDate,strY ear,strMonth,strDay,strDayOfWeek;strYear.Format("%d",nYear);strMonth.Format("%d",nMonth);if (nMonth <10){strMonth.Format("0%d",nMonth);}strDay.Format("%d",nDay);if (nDay <10){strDay.Format("0%d",nDay);}strDate.Format("%s年%s月%s日",strYear,strMonth,strDay);strDayOfWeek = weekDay( oTime);int nBKMode = pDc->SetBkMode(TRANSPARENT);pDc->SetTextColor(m_dColor);pDc->TextOut(m_DX,m_DY,strDate);pDc->SetTextColor(m_wColor);pDc->TextOut(m_WX,m_WY,strDayOfWeek);pDc->SetBkMode(nBKMode);}void CClockDate::SetPosition(CRect rectClient){CPoint ptMiddle;ptMiddle.x = rectClient.Width()/2;ptMiddle.y = rectClient.Height()/2 - 15;int nRidius = min(ptMiddle.x,ptMiddle.y);if(!m_bUp){m_DX = ptMiddle.x + nRidius * 0.8 - 110;m_DY = ptMiddle.y + 20 ;m_WX = ptMiddle.x - nRidius * 0.8 + 35;m_WY = ptMiddle.y + 20 ;return;}if(m_position == 0){ // 内部switch(m_inType){case 0: // 上m_DX = ptMiddle.x - 46;m_DY = ptMiddle.y - nRidius * 0.7 + 50;m_WX = ptMiddle.x - 16;m_WY = ptMiddle.y - nRidius * 0.7 + 30;break;case 1: // 下m_DX = ptMiddle.x - 46;m_DY = ptMiddle.y + nRidius * 0.7 - 50;m_WX = ptMiddle.x - 16;m_WY = ptMiddle.y + nRidius * 0.7 - 30;break;case 2: // 左m_DX = ptMiddle.x - nRidius * 0.8 + 10;m_DY = ptMiddle.y + 50;m_WX = ptMiddle.x - nRidius * 0.8 + 45;m_WY = ptMiddle.y + 30 ;break;case 3: // 右m_DX = ptMiddle.x + nRidius * 0.8 - 110;m_DY = ptMiddle.y + 50 ;m_WX = ptMiddle.x + nRidius * 0.8 - 75;m_WY = ptMiddle.y + 30 ;break;}}else if(m_position == 1){ // 外部switch(m_outType){case 0: // 左上角m_DX = 10;m_DY = 60;m_WX = 40;m_WY = 40;break;case 1: // 右上角m_DX = rectClient.right - 112;m_DY = 60;m_WX = rectClient.right - 82;m_WY = 40;break;case 2: // 左下角m_DX = 10;m_DY = rectClient.bottom - 80 ;m_WX = 40;m_WY = rectClient.bottom - 60 ;break;case 3: // 右下角m_DX = rectClient.right - 112;m_DY = rectClient.bottom - 80 ;m_WX = rectClient.right - 82;m_WY = rectClient.bottom - 60 ;break;}}}CString CClockDate::weekDay(CTime oTime) {CString str;int nDayOfWeek = oTime.GetDayOfWeek();switch(nDayOfWeek ){case 1:str = "星期日";break;case 2:str = "星期一";break;case 3:str = "星期二";break;case 4:str = "星期三";break;case 5:str = "星期四";break;case 6:str = "星期五";break;case 7:str = "星期六";break;}return str;}// ClockHand.cpp: implementation of the CClockHand class.////////////////////////////////////////////////////////////////////////#include "stdafx.h"#include "deskClock.h"#include "ClockHand.h"#include "ClockScale.h"#ifdef _DEBUG#undef THIS_FILEstatic char THIS_FILE[]=__FILE__;#define new DEBUG_NEW#endif////////////////////////////////////////////////////////////////////// // Construction/Destruction//////////////////////////////////////////////////////////////////////CClockHand::CClockHand(){m_HbordColor = RGB(255,255,255);m_HColor = RGB(128,128,0);m_MbordColor = RGB(255,255,255);m_MColor = RGB(0,128,128);m_SbordColor = RGB(255,128,128);}CClockHand::~CClockHand(){}void CClockHand::DrawHand(CDC *pDC, int nValue,HANDTYPE typeHand,CPoint &ptMiddle,CTime oTime){m_ptMiddle.x = ptMiddle.x;m_ptMiddle.y = ptMiddle.y;m_nRidius = min(m_ptMiddle.x,m_ptMiddle.y);m_nPointWidth = (int)m_nRidius/20;CPoint ptHand[4];//得到指针的位置GetHandPoints(nValue,typeHand,ptHand, oTime);CBrush brHandH(m_HColor);CPen penHandH(PS_SOLID,1,m_HbordColor);CBrush brHandM(m_MColor);CPen penHandM(PS_SOLID,1,m_MbordColor);CPen penrgb(PS_SOLID,1,m_SbordColor);switch(typeHand){case HOUR_HAND://设置画刷、画笔pDC->SelectObject(&brHandH);pDC->SelectObject(&penHandH);//绘制一个四边形pDC->Polygon(ptHand,4);break;case MINUTE_HAND://设置画刷、画笔pDC->SelectObject(&brHandM);pDC->SelectObject(&penHandM);//绘制一个四边形pDC->Polygon(ptHand,4);break;case SECOND_HAND:pDC->SelectObject(&penrgb);pDC->MoveTo(ptHand[0]);pDC->LineTo(ptHand[1]);break;}}void CClockHand::GetHandPoints(int nValue, HANDTYPE typeHand, CPoint *pptHand,CTime oTime){UINT nMinute = oTime.GetMinute();CClockScale Scale;Scale.m_ptMiddle.x = m_ptMiddle.x;Scale.m_ptMiddle.y = m_ptMiddle.y;int nLength = 0;//根据指针的类型区分switch(typeHand){case HOUR_HAND://时针长为钟面半径的一半nLength = MulDiv(m_nRidius, 50, 100);//因为绘制时针按照分针进行,故需要一些变换nValue *= 5;nValue += (nMinute/12);break;case MINUTE_HAND:nLength = MulDiv(m_nRidius, 70, 100);break;case SECOND_HAND:nLength = MulDiv(m_nRidius, 80, 100);break;default:ASSERT(false);}//得到时针和分针外形的四个点if (typeHand == HOUR_HAND || typeHand == MINUTE_HAND){pptHand[0] = puterFacePoint(nValue+30,m_nPointWidth*2);pptHand[1] = puterFacePoint(nValue+15,m_nPointWidth);pptHand[2] = puterFacePoint(nValue,nLength);pptHand[3] = puterFacePoint(nValue-15,m_nPointWidth);}//得到秒针的两个端点elsepptHand[0] = m_ptMiddle;pptHand[1] = puterFacePoint(nValue,nLength);}}void CClockHand::SetHandColor(COLORREF hBColor, COLORREF hColor, COLORREF mBColor, COLORREF mColor, COLORREF sColor){m_HbordColor = hBColor;m_HColor = hColor;m_MbordColor = mBColor;m_MColor = mColor;m_SbordColor = sColor;}void CClockHand::GetHandColor(COLORREF &hBColor, COLORREF &hColor, COLORREF &mBColor, COLORREF &mColor, COLORREF &sColor){hBColor = m_HbordColor;hColor = m_HColor;mBColor = m_MbordColor;mColor = m_MColor;sColor = m_SbordColor;}// ClockNum.cpp: implementation of the CClockNum class.////////////////////////////////////////////////////////////////////////#include "stdafx.h"#include "deskClock.h"#include "ClockNum.h"#ifdef _DEBUG#undef THIS_FILEstatic char THIS_FILE[]=__FILE__;#define new DEBUG_NEW//////////////////////////////////////////////////////////////////////// Construction/Destruction//////////////////////////////////////////////////////////////////////CClockNum::CClockNum(){m_position = 0;m_inType = 1;m_outType = 5;m_color = RGB(0,255,0);m_bgColor = false;}CClockNum::~CClockNum(){}void CClockNum::DrawTime(CDC *pDc, CRect rectClient,CTime oTime) {SetPosition(rectClient);UINT nHour,nMinute,nSecond;nHour = oTime.GetHour();nMinute = oTime.GetMinute();nSecond = oTime.GetSecond();CString strTime,strHour,strMinute,strSecond;strHour.Format("%d",nHour);if (nHour <10){strHour.Format("0%d",nHour);}strMinute.Format("%d",nMinute);if (nMinute <10){strMinute.Format("0%d",nMinute);}strSecond.Format("%d",nSecond);if (nSecond <10){strSecond.Format("0%d",nSecond);}strTime.Format("%s : %s : %s",strHour,strMinute,strSecond);pDc->SetTextColor(m_color);if(!m_bgColor){int nBKMode = pDc->SetBkMode(TRANSPARENT);pDc->TextOut(m_X,m_Y,strTime);pDc->SetBkMode(nBKMode);}else{pDc->TextOut(m_X,m_Y,strTime);}}void CClockNum::SetPosition(CRect rectClient){CPoint ptMiddle;ptMiddle.x = rectClient.Width()/2;ptMiddle.y = rectClient.Height()/2 - 15;int nRidius = min(ptMiddle.x,ptMiddle.y);if(m_position == 0){ // 内部switch(m_inType){case 0: // 上m_X = ptMiddle.x - 35;m_Y = ptMiddle.y - nRidius * 0.7;break;case 1: // 下m_X = ptMiddle.x - 35;m_Y = ptMiddle.y + nRidius * 0.7;break;case 2: // 左m_X = ptMiddle.x - nRidius * 0.8 + 25;m_Y = ptMiddle.y ;break;case 3: // 右m_X = ptMiddle.x + nRidius * 0.8 - 95;m_Y = ptMiddle.y ;break;}}else if(m_position == 1){ // 外部switch(m_outType){case 0: // 左上角m_X = 22;m_Y = 10;break;case 1: // 右上角m_X = rectClient.right - 100;m_Y = 10;break;case 2: // 左下角m_X = 22;m_Y = rectClient.bottom - 30 ;break;case 3: // 右下角m_X = rectClient.right - 96;m_Y = rectClient.bottom - 30;break;case 4: // 上侧居中m_X = ptMiddle.x - 33;m_Y = 2;break;case 5: // 下侧居中m_X = ptMiddle.x - 33;m_Y = rectClient.bottom - 30;break;case 6: // 左侧居中m_X = 10;m_Y = ptMiddle.y ;break;case 7: // 右侧居中m_X = rectClient.right - 86;m_Y = ptMiddle.y ;break;}}}// ClockScale.cpp: implementation of the CClockScale class.//////////////////////////////////////////////////////////////////////// #include "stdafx.h"#include "deskClock.h"#include "ClockScale.h"#include <math.h>#ifdef _DEBUG#undef THIS_FILEstatic char THIS_FILE[]=__FILE__;#define new DEBUG_NEW#endif#define PI 3.1415926////////////////////////////////////////////////////////////////////// // Construction/Destruction//////////////////////////////////////////////////////////////////////CClockScale::CClockScale(){m_color = 0xFF0000;m_style = TYPE_RHOMBUS;m_logColor = 0;}CClockScale::~CClockScale(){}void CClockScale::DrawScale(CDC *pDc,CPoint &ptMiddle){//计算钟面的中心位置m_ptMiddle.x = ptMiddle.x;m_ptMiddle.y = ptMiddle.y;if (m_ptMiddle.y < 0){m_ptMiddle.y = 0;}//计算钟面的半径UINT nRidius = min(m_ptMiddle.x,m_ptMiddle.y);//计算两个刻度之间的距离m_nPointWidth = (int)nRidius/20;// 绘制LOGCBitmap maskbmp,logbmp;maskbmp.LoadBitmap(IDB_LOGMASK);logbmp.LoadBitmap(IDB_LOG);CDC MaskDC,memDC;MaskDC.CreateCompatibleDC(pDc);MaskDC.SelectObject(&maskbmp);memDC.CreateCompatibleDC(pDc);memDC.SelectObject(&logbmp);if(m_nPointWidth > 8){if(m_logColor == 0 ){pDc->BitBlt(m_ptMiddle.x - 25 ,ptMiddle.y - nRidius * 0.7,96,96,&MaskDC,0,0,SRCAND);pDc->BitBlt(m_ptMiddle.x - 25,ptMiddle.y - nRidius * 0.7,96,96,&memDC,0,0,MERGEPAINT);}else if(m_logColor == 1 ){pDc->BitBlt(m_ptMiddle.x - 25 ,ptMiddle.y - nRidius * 0.7,96,96,&MaskDC,0,0,MERGEPAINT);pDc->BitBlt(m_ptMiddle.x - 25,ptMiddle.y - nRidius * 0.7,96,96,&memDC,0,0,SRCAND);}}if (m_nPointWidth < 2){m_nPointWidth = 2;}//保存各个刻度点的位置CPoint ptFace;//设置刻度点的颜色CBrush brPoint(m_color);CBrush* pbrOld = pDc->SelectObject(&brPoint);//刻度所在的圆半径为钟面半径的90%int nFaceLength = MulDiv(nRidius,9,10);//绘制各个刻度for (int nMin=0; nMin<60; nMin++){//bHour为假表示绘制的是分钟刻度BOOL bHour = FALSE;//计算一个刻度点的位置ptFace = ComputerFacePoint(nMin,nFaceLength);//当分钟数是5的倍数时,bHour为真表示绘制小时刻度if (nMin%5==0){bHour = true;}//绘制一个刻度点DrawFacePoint(pDc,ptFace,bHour);}pDc->SelectObject(pbrOld);return;}CPoint CClockScale::ComputerFacePoint(UINT min, int nFaceLength){CPoint ptCalc;//将分钟转换为角度数double fDegrees = 180+((15+min)%60)*6;//再转换为弧度数double fAngle = fDegrees/180;//计算刻度点位置ptCalc.x = m_ptMiddle.x + (int)(cos(fAngle*PI)*nFaceLength);ptCalc.y = m_ptMiddle.y + (int)(sin(fAngle*PI)*nFaceLength);//返回刻度点位置return(ptCalc);}void CClockScale::DrawFacePoint(CDC *pDC, const CPoint &ptFace, BOOL bMajor){CRect rectPoint(ptFace.x,ptFace.y,ptFace.x,ptFace.y);//绘制小时刻度点if (bMajor){//增加高度和宽度,使单点变为小的矩形区rectPoint.InflateRect((m_nPointWidth/2)+2,(m_nPointWidth/2)+2);DrawMajor(pDC, m_style,rectPoint);}//绘制分钟刻度点else{//只有当刻度点之间的距离足够大时才绘制if (m_nPointWidth > 2){rectPoint.InflateRect(1,1);pDC->Draw3dRect(&rectPoint,GetSysColor(COLOR_BTNHIGHLIGHT),GetSysColor(COLOR_BT NSHADOW));}}return;}void CClockScale::DrawMajor(CDC *pDC, MAJORTYPE type, CRect rectPoint){CPen *oldPen;CPoint ptRhombus[4] ;CPen penBorder(PS_SOLID,1,RGB(255,255,255));switch(type){case TYPE_ELLIPSE:pDC->Ellipse(rectPoint);break;case TYPE_RECT:pDC->Rectangle(&rectPoint);pDC->Draw3dRect(&rectPoint,GetSysColor(COLOR_BTNHIGHLIGHT),GetSysColor(COLOR_BT NSHADOW));break;case TYPE_RHOMBUS:oldPen = pDC->SelectObject(&penBorder);ptRhombus[1].x = rectPoint.left + rectPoint.Width()/2;ptRhombus[1].y = rectPoint.top;ptRhombus[0].x = rectPoint.left;ptRhombus[0].y = rectPoint.top + rectPoint.Height()/2;ptRhombus[3].x = rectPoint.left + rectPoint.Width()/2;ptRhombus[3].y = rectPoint.bottom;ptRhombus[2].x = rectPoint.right;ptRhombus[2].y = rectPoint.top + rectPoint.Height()/2;pDC->Polygon(ptRhombus,4);pDC->SelectObject(oldPen);break;case TYPE_TRIANGLE:oldPen = pDC->SelectObject(&penBorder);ptRhombus[0].x = rectPoint.left;ptRhombus[0].y = rectPoint.top + rectPoint.Height()/2;ptRhombus[1].x = rectPoint.left + rectPoint.Width()/2;ptRhombus[1].y = rectPoint.top;ptRhombus[2].x = rectPoint.right;ptRhombus[2].y = rectPoint.top + rectPoint.Height()/2;pDC->Polygon(ptRhombus,3);pDC->SelectObject(oldPen);break;}}void CClockScale::SetScaleStyle(MAJORTYPE type){m_style = type;}void CClockScale::SetScaleColor(COLORREF colorref){m_color = colorref;}void CClockScale::SetLogColor(int color){m_logColor = color;}MAJORTYPE CClockScale::GetScaleStyle(){return m_style;}COLORREF CClockScale::GetScaleColor(){return m_color;}int CClockScale::GetLogColor(){return m_logColor;}// DateSetDlg.cpp : implementation file//#include "stdafx.h"#include "deskclock.h"#include "DateSetDlg.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif///////////////////////////////////////////////////////////////////////////// // DateSetDlg dialogDateSetDlg::DateSetDlg(CWnd* pParent /*=NULL*/): CDialog(DateSetDlg::IDD, pParent){//{{AFX_DATA_INIT(DateSetDlg)m_up = 0;m_in = 0;m_show = 0;//}}AFX_DATA_INIT}void DateSetDlg::DoDataExchange(CDataExchange* pDX){CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(DateSetDlg)DDX_Control(pDX, IDC_COMOUT, m_comOut);DDX_Control(pDX, IDC_COMIN, m_comIn);DDX_Radio(pDX, IDC_UP, m_up);DDX_Radio(pDX, IDC_IN, m_in);DDX_Radio(pDX, IDC_SHOW, m_show);。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
private int DEFAULT_LOC_HEIGHT = 300;
private int DEFAULT_WIDTH = 330;
private int DEFAULT_HEIGHT = 330;
}
/**
* This class is used to defind the main panel of the clock
setLocation(DEFAULT_LOC_WIDTH, DEFAULT_LOC_HEIGHT);
ClockPanel panel = new ClockPanel();
add(panel);
}
//variables of the frame
private int DEFAULT_LOC_WIDTH = 300;
public void actionPerformed(ActionEvent evt){
repaint();
}
};
//create timer
new Timer(delay, drawClock).start();
}
//variables of the panel
//draw the clock hour line
Line2D clockHour = new Line2D.Double();
double hourTime = (double) calendar.get(Calendar.HOUR);
lenX = HOUR_LEN*Math.sin(2*Math.PI*((secondTime+60*minuteTime+3600*hourTime)/43200.0));
*/
class ClockPanel extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
//get the time of the system
private int HOUR_LEN = 50;
private int MINUTE_LEN = 70;
private int SECOND_LEN = 90;
private int RADIUS = 100;
private int INNER_RADIUS = 2;
posX = CENTER_X + lenX;
posY = CENTER_Y - lenY;
clockMinute.setLine(CENTER_X, CENTER_Y, posX, posY);
g2.setColor(Color.GREEN);
g2.draw(clockMinute);
lenX = MINUTE_LEN*Math.sin(2*Math.PI*(secondTime+60*minuteTime)/3600.0);
lenY = MINUTE_LEN*Math.cos(2*Math.PI*(secondTime+60*minuteTime)/3600.0);
g2.setColor(Color.BLUE);
g2.draw(clockFace);
//draw the clock center
Ellipse2D clockCenter = new Ellipse2D.Double();
clockCenter.seTER_Y, CENTER_X+INNER_RADIUS, CENTER_Y+INNER_RADIUS);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
/**
* This class is used to define the main frame of the clock
g2.setColor(Color.PINK);
g2.draw(clockSecond);
//draw the clock minute line
Line2D clockMinute = new Line2D.Double();
double minuteTime = (double) calendar.get(Calendar.MINUTE);
*/
class ClockFrame extends JFrame{
//constructor function
public ClockFrame(){
setTitle("小时钟");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
g2.setColor(Color.RED);
g2.fill(clockCenter);
//help to get the exact position of the lines
double lenX, lenY, posX, posY;
//draw the clock second line
/**
* This is the main Function of the program.
*/
public class Clock{
public static void main(String []args){
ClockFrame frame = new ClockFrame();
//draw the clock face
Ellipse2D clockFace = new Ellipse2D.Double();
clockFace.setFrameFromCenter(CENTER_X, CENTER_Y, CENTER_X+RADIUS, CENTER_Y+RADIUS);
g2.setColor(Color.BLUE);
g2.draw(clockHour);
int delay = 1000;
// actionListener
ActionListener drawClock;
drawClock=new ActionListener(){
lenY = SECOND_LEN*Math.cos(2*Math.PI*secondTime/60.0);
posX = CENTER_X + lenX;
posY = CENTER_Y - lenY;
clockSecond.setLine(CENTER_X, CENTER_Y, posX, posY);
private int CENTER_X = 150;
private int CENTER_Y = 150;
}
lenY = HOUR_LEN*Math.cos(2*Math.PI*((secondTime+60*minuteTime+3600*hourTime)/43200.0));
posX = CENTER_X + lenX;
posY = CENTER_Y - lenY;
clockHour.setLine(CENTER_X, CENTER_Y, posX, posY);
GregorianCalendar calendar = new GregorianCalendar();
int hour = calendar.get(Calendar.HOUR);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
Line2D clockSecond = new Line2D.Double();
double secondTime = (double) calendar.get(Calendar.SECOND);
lenX = SECOND_LEN*Math.sin(2*Math.PI*secondTime/60.0);
相关文档
最新文档