用C++编写模拟时钟程序

合集下载

模拟时钟源代码

模拟时钟源代码

void CTest13_4Doc::m_TickAdd(){this->m_sec ++;if(this->m_sec == 60){this->m_min ++;this->m_sec = 0;}if(this->m_min == 60){this->m_hour ++;this->m_min = 0;}if(this->m_hour>= 12){this->m_hour -= 12;}}void CTest13_4Doc::M_MoveClockHands(){intpx, py;double degree;//设置秒针的终点坐标degree = this->m_sec * 6 - 90;px = cos((degree/180.0)*PI)* sLen + 250;py = sin((degree/180)*PI)* sLen + 250;this->m_secEndPoint.x = px;this->m_secEndPoint.y= py;//设置分针的终点坐标degree = this->m_min * 6 - 90;px = cos((degree/180.0)*PI) * mLen + 250;py = sin((degree/180)*PI) * mLen + 250;this->m_minEndPoint.x = px;this->m_minEndPoint.y = py;//设置时针的终点坐标degree = this->m_hour * 30 - 90;px = cos((degree/180.0)*PI) * hLen + 250;py = sin((degree/180.0)*PI) * hLen + 250;this->m_hourEndPoint.x = px;this->m_hourEndPoint.y = py;}BOOL CTest13_4Doc::OnNewDocument(){if (!CDocument::OnNewDocument())return FALSE;// TODO: add reinitialization code herethis->m_Rect = CRect(100,100,400,400);this->m_org = this->m_Rect.CenterPoint();this->m_centerRect = CRect(m_Rect.TopLeft().x+140, m_Rect.TopLeft().y+140,m_Rect.BottomRight().x-140, m_Rect.BottomRight().y-140);CTime time = CTime::GetCurrentTime();this->m_hour = time.GetHour();this->m_min = time.GetMinute();this->m_sec = time.GetSecond();//设置时钟指针的终点位置// this->M_MoveClockHands();//设置指针的长度hLen = 80;mLen = 100;sLen = 120;// (SDI documents will reuse this document)return TRUE;}void CTest13_4View::OnDraw(CDC* pDC){CTest13_4Doc* pDoc = GetDocument();ASSERT_VALID(pDoc);SetTimer(0, 1000, NULL);CPen *oldPen, *cPen;CBrush *oldBrush, *cBrush;//画出大的圆cPen = new CPen(PS_SOLID, 3, RGB(255,132,123));cBrush = new CBrush(RGB(123,132,123));oldPen = pDC->SelectObject(cPen);oldBrush = pDC->SelectObject(cBrush);pDC->Ellipse(pDoc->m_Rect);pDC->SelectObject(oldPen);pDC->SelectObject(oldBrush);//画出中心的一个圆cBrush = new CBrush(RGB(255,132,123));pDC->SelectObject(cBrush);pDC->Ellipse(pDoc->m_centerRect);pDC->SelectObject(oldBrush);//得到矩形的另个点,画出四个分线cPen = new CPen(PS_SOLID, 2, RGB(255,222,123));oldPen = pDC->SelectObject(cPen);CPointtl = pDoc->m_Rect.TopLeft();CPointbr = pDoc->m_Rect.BottomRight();pDC->MoveTo((br.x+tl.x)/2, tl.y);pDC->LineTo((br.x+tl.x)/2, tl.y+20);pDC->MoveTo((br.x+tl.x)/2, br.y);pDC->LineTo((br.x+tl.x)/2, br.y-20);pDC->MoveTo(tl.x, (br.y+tl.y)/2);pDC->LineTo(tl.x+20, (br.y+tl.y)/2);pDC->MoveTo(br.x, (br.y+tl.y)/2);pDC->LineTo(br.x-20, (br.y+tl.y)/2);pDC->SelectObject(oldPen);//画出刻度pDC->MoveTo(326,122);pDC->LineTo(320,134);pDC->MoveTo(377,175);pDC->LineTo(365,183);////pDC->MoveTo(377,323);pDC->LineTo(365,317);pDC->MoveTo(326,378);pDC->LineTo(320,365);////pDC->MoveTo(175,377);pDC->LineTo(183,365);pDC->MoveTo(122,326);pDC->LineTo(134,320);////pDC->MoveTo(122,177);pDC->LineTo(134,183);pDC->MoveTo(177,122);pDC->LineTo(183,134);//画出三个时针、分针、秒针//hourpDC->MoveTo(pDoc->m_org);pDC->LineTo(pDoc->m_hourEndPoint);//minutinpDC->MoveTo(pDoc->m_org);pDC->LineTo(pDoc->m_minEndPoint);//secondpDC->MoveTo(pDoc->m_org);pDC->LineTo(pDoc->m_secEndPoint);// TODO: add draw code for native data here}void CTest13_4View::OnTimer(UINT nIDEvent){// TODO: Add your message handler code here and/or call default CTest13_4Doc* pDoc = GetDocument();ASSERT_VALID(pDoc);pDoc->m_TickAdd();pDoc->M_MoveClockHands();this->InvalidateRect(CRect(130,130,370,370));CView::OnTimer(nIDEvent);}void CTest13_4View::OnLButtonDblClk(UINT nFlags, CPoint point) {// TODO: Add your message handler code here and/or call default CTest13_4Doc* pDoc = GetDocument();ASSERT_VALID(pDoc);changeTimectime;if(ctime.DoModal() == IDOK){pDoc->m_hour = ctime.m_Hour;pDoc->m_min = ctime.m_Min;pDoc->m_sec = ctime.m_Sec;//改变系统时间if(ctime.m_ChangeSysTime){CTime time = CTime::GetCurrentTime();SYSTEMTIME tm;tm.wYear = time.GetYear();tm.wDay = time.GetDay();tm.wMonth =time.GetMonth();tm.wHour = ctime.m_Hour;tm.wMinute = ctime.m_Min;tm.wSecond = ctime.m_Sec;tm.wMilliseconds = 0;SetLocalTime(&tm);}pDoc->M_MoveClockHands();}CView::OnLButtonDblClk(nFlags, point);}大体上就用到了上面几个函数,用到的变量自己看着加吧!。

用C语言写的一个PIC16F877的时闹钟程序

用C语言写的一个PIC16F877的时闹钟程序

用C语言写的一个PIC16F877的时闹钟程序单片机用16F877,主时钟用20MHz,用32768作定时时间。

可以实现2路定闹,每一路都可分别设置和开关,采用4x4键盘,16x2的字符型LCD显示。

连线在程序开头有说明。

程序的功能:(1)上电后LCD背光打开,并显示倒计时5秒,然后时钟开始工作。

(2)用模式键(*)切换模式,如显示时间、日期、闹钟1、闹钟2等,并且可以用上、下键控制加1、减1或是闹钟的On、Off。

(3)原程序有16个键,包括0~9数字键,可以直接输入要设置的时间值,但后来将数字键取消了,你仍然可以通过修改程序的部分注释恢复此功能。

(4)闹钟有2路,时间到后闹2分钟,可按任意键取消本次闹钟。

闹钟响时有2种音调,是用PIC的PWM实现的。

(5)按任意键可打开背光,1分钟后自动关闭背光。

(6)RA0~RA3为按键扫描输入,应接下拉电阻。

主程序// FileName: Main.c// MCU: Microchip PIC16F877// Tool: CCS-C compiler// Author: KingEDA, MSN:kingeda@, skype:kingeda, E-mail:kingeda@// Website: // Description:// A timer program// Ver 0.1: 2003-03-31, all clock function with date display, 2 way alarm.// Ver 0.2: 2003-05-05, (1) Alarm default is on,modify alarm1 time to 7:00:00,// and alarm2 to 13:30:00.// (2) Backlight will be enabled when alarming.// (3) Automatic adjust day(28,30,31).// (4) Automatic move cursor to next location when set item.// PIN Connection:// RC0~1 : 32768Hz crystal// RC2 : Buzzer// RC3 : LCD Back Light,drive a PNP BJT// RD0~RD7 : to LCD DB0~DB7// RA0~RA3 : keypad col in// RC4~RC7 : keypad line out// 7 8 9 #// 4 5 6 ↑// 1 2 3 ↓// 0 ←→*// RE0 : LCD RS// RE1 : LCD RW// RE2 : LCD E#include "my16f877.h"#device ICD=true//#fuses HS,NOWDT,NOPROTECT,PUT,BROWNOUT #use delay(clock = 24000000)//#use fast_io(C)#use fast_io(E)#define lcd_busy (lcd_read_addr()&0x80) == 0x80#define time_start_addr 0x80+0x04#define time_hourh_addr time_start_addr#define time_hourl_addr time_start_addr+1#define time_minuteh_addr time_start_addr+3#define time_minutel_addr time_start_addr+4#define time_secondh_addr time_start_addr+6#define time_secondl_addr time_start_addr+7#define key_0 0x11#define key_1 0x21#define key_2 0x22#define key_3 0x24#define key_4 0x41#define key_5 0x42#define key_6 0x44#define key_7 0x81#define key_8 0x82#define key_9 0x84#define key_left 0x12#define key_right 0x14#define key_up 0x48#define key_down 0x28#define key_mode 0x18#define key_cancel 0x88char StrPower1[] = " * Power on * ";char StrSetTime[] = " * Adjust time* ";char StrSetDate[] = " * Adjust date* ";char StrAlarm1[] = " * Set alarm 1* ";char StrAlarm2[] = " * Set alarm 2* ";unsigned char PORTC_MAP;#bit BackLightEn = PORTC_MAP.3unsigned char BackLightTimer;int1 led;#bit lcd_rs = PORTE.0#bit lcd_rw = PORTE.1#bit lcd_e = PORTE.2#byte lcd_bus = PORTD#byte lcd_dir = TRISD#define PWM_on 0x0c#define PWM_off 0x00#define PWM_period 200#define PWM_DC 100unsigned char lcd_addr;unsigned char KeyLine;unsigned char KeyOld;unsigned char KeyNew;struct mTime {unsigned char hourh; // hour,0~23unsigned char hourl;unsigned char minuteh; // minute,0~59unsigned char minutel;unsigned char secondh; // second,0~59unsigned char secondl;};struct mTime CurrentTime = {1,2,0,0,0,0};struct mTime AlarmTime1 = {0,7,0,0,0,0}; // 07:00:00 struct mTime AlarmTime2 = {1,3,3,0,0,0}; // 13:30:00 unsigned char AlarmStatus;#bit Alarm1Enable = AlarmStatus.0#bit Alarm2Enable = AlarmStatus.1#bit Alarm1Alarm = AlarmStatus.2#bit Alarm2Alarm = AlarmStatus.3unsigned char Alarm1Cnt; // alarm1 second count unsigned char Alarm2Cnt;unsigned char CurrentMode;#define mode_time 0#define mode_set_time 1#define mode_set_date 2#define mode_set_alarm1 3#define mode_set_alarm2 4unsigned char adjust_item;struct mDate {unsigned char year1; //unsigned char year2;unsigned char year3;unsigned char year4;unsigned char monthh;unsigned char monthl;unsigned char dayh;unsigned char dayl;};struct mDate CurrentDate = {2,0,0,3,0,1,0,1}; unsigned char *pStr;// ------------------------------------------------------- unsigned char lcd_read_addr(){unsigned char ch;lcd_dir = 0xff; // read from lcdlcd_rs = 0;lcd_rw = 1; // instlcd_e = 1;#asmnopnopnop#endasmch = lcd_bus;lcd_e = 0;lcd_dir = 0x00; // set write to lcdreturn ch;}// ------------------------------------------------------- unsigned char lcd_write_data(unsigned char ch) {while (lcd_busy){ restart_wdt(); }lcd_rs = 1; // datalcd_rw = 0; // writelcd_bus = ch; // write outlcd_e = 1;#asmnopnopnop#endasmlcd_e = 0;return 'Y';}// ------------------------------------------------------- unsigned char lcd_write_inst(unsigned char ch) {while (lcd_busy){ restart_wdt(); }lcd_rs = 0; // instlcd_rw = 0; // writelcd_bus = ch;lcd_e = 1;#asmnopnopnop#endasmlcd_e = 0;return 'Y';}// ------------------------------------------------------- unsigned char lcd_read_data(){unsigned char ch;while (lcd_busy){ restart_wdt(); }lcd_dir = 0xff; // read from lcdlcd_rs = 1; // datalcd_rw = 1; // readlcd_e = 1;#asmnopnopnop#endasmch = lcd_bus; // read inlcd_e = 0;lcd_dir = 0x00; // set write to lcdreturn ch;}// ------------------------------------------------------- void lcd_init(){unsigned char Tempch;lcd_addr = 0;delay_ms(100);Tempch = 0x38; // 1-line mode,5x8 dotslcd_write_inst(Tempch); // Function setTempch = 0x0f; // lcd on,cursor on,blink onlcd_write_inst(Tempch); // Display on/offTempch = 0x06; // Increment mode,Entire shift offlcd_write_inst(Tempch);Tempch = 0x01; // clear displaylcd_write_inst(Tempch);delay_ms(3);}// -------------------------------------------------------//#int_timer1//void timer1_interrupt(void)#int_ccp2void ccp2_interrupt(void){//TMR1H = 0x80;if (CurrentTime.secondl==9){CurrentTime.secondl=0;if (CurrentTime.secondh==5){CurrentTime.secondh=0;if (CurrentTime.minutel==9){CurrentTime.minutel=0;if (CurrentTime.minuteh==5){CurrentTime.minuteh=0;if (CurrentTime.hourl==9){CurrentTime.hourl=0;CurrentTime.hourh++;}else if((CurrentTime.hourl==3) && (CurrentTime.hourh==2)){CurrentTime.hourl=0;CurrentTime.hourh=0;if ((((CurrentDate.dayl == 8) || (CurrentDate.dayl == 9)) && (CurrentDate.dayh == 2) && (CurrentDate.monthl == 2) && (CurrentDate.monthh == 0)) ||((CurrentDate.dayl == 0) && (CurrentDate.dayh == 3) && ((((CurrentDate.monthl == 4) || (CurrentDate.monthl == 6)|| (CurrentDate.monthl == 9)) && (CurrentDate.monthh == 0)) || ((CurrentDate.monthl == 1) && (CurrentDate.monthh == 1)))) ||((CurrentDate.dayl == 1) && (CurrentDate.dayh == 3))){CurrentDate.dayl=1;CurrentDate.dayh=0;if ((CurrentDate.monthl == 2) && (CurrentDate.monthh == 1)){CurrentDate.monthl = 1;CurrentDate.monthh = 0;if (CurrentDate.year4 == 9){CurrentDate.year4 = 0;if (CurrentDate.year3 == 9){CurrentDate.year3 = 0;if (CurrentDate.year2 == 9){CurrentDate.year2 = 0;CurrentDate.year1++;}elseCurrentDate.year2++;}elseCurrentDate.year3++;}elseCurrentDate.year4++;}else if(CurrentDate.monthl == 9){CurrentDate.monthl = 0;CurrentDate.monthh++;}elseCurrentDate.monthl++;}else if(CurrentDate.dayl == 9){CurrentDate.dayl=0;CurrentDate.dayh++;}elseCurrentDate.dayl++;}elseCurrentTime.hourl++;}elseCurrentTime.minuteh++;}elseCurrentTime.minutel++;}elseCurrentTime.secondh++;}elseCurrentTime.secondl++;if ((Alarm1Alarm == false) & (Alarm2Alarm == false)){led = 0;CCP1CON = PWM_off;}else{if (led == 1){led = 0;PR2 = PWM_period; // set pwm periodCCPR1L = PWM_DC; // set pwm duty cycle//CCP1CON = PWM_on;}else{led = 1;PR2 = PWM_period/2; // set pwm periodCCPR1L = PWM_DC/2; // set pwm duty cycle//CCP1CON = PWM_off;}}Alarm1Cnt++;Alarm2Cnt++;if (BackLightEn == 0)if (((BackLightTimer++)>=60) & (Alarm1Alarm == false) & (Alarm1Alarm == false))BackLightEn = 1; // disable backlight PORTC = PORTC_MAP;//TMR1IF = 0;//PIR1 = PIR2 = 0x00;CCP2IF = 0;}// ------------------------------------------------------- unsigned char get_key(void){unsigned char key_in,tmp;TRISC = 0x03;KeyLine = 0xf0;PORTC = KeyLine | PORTC_MAP;#asmnopnopnop#endasmif ((PORTA & 0x0f) != 0){tmp = 0x10;for (KeyLine = tmp;KeyLine!=0;KeyLine = tmp){PORTC = KeyLine | PORTC_MAP;tmp = KeyLine <<1;#asmnopnopnop#endasmkey_in = PORTA & 0x0f;if (key_in != 0){return (key_in | KeyLine);}}return 0;}elsereturn 0;}// -------------------------------------------------------void set_mode(void){if (CurrentMode == mode_set_alarm2)CurrentMode = mode_time;else{CurrentMode++;adjust_item = 0;}lcd_write_inst(0x01); // clear LCD displaylcd_write_inst(time_start_addr); // set LCD line1 if (CurrentMode == mode_set_time){lcd_write_data(CurrentTime.hourh + '0');lcd_write_data(CurrentTime.hourl + '0');lcd_write_data(':');lcd_write_data(CurrentTime.minuteh + '0');lcd_write_data(CurrentTime.minutel + '0');lcd_write_data(':');lcd_write_data(CurrentTime.secondh + '0');lcd_write_data(CurrentTime.secondl + '0');pStr = StrSetTime;}else if(CurrentMode == mode_set_date){lcd_write_data(CurrentDate.year1 + '0');lcd_write_data(CurrentDate.year2 + '0');lcd_write_data(CurrentDate.year3 + '0');lcd_write_data(CurrentDate.year4 + '0');lcd_write_data('/');lcd_write_data(CurrentDate.monthh + '0');lcd_write_data(CurrentDate.monthl + '0');lcd_write_data('/');lcd_write_data(CurrentDate.dayh + '0');lcd_write_data(CurrentDate.dayl + '0');pStr = StrSetDate;}else if(CurrentMode == mode_set_alarm1){lcd_write_data(AlarmTime1.hourh + '0');lcd_write_data(AlarmTime1.hourl + '0');lcd_write_data(':');lcd_write_data(AlarmTime1.minuteh + '0');lcd_write_data(AlarmTime1.minutel + '0');lcd_write_data(':');lcd_write_data(AlarmTime1.secondh + '0');lcd_write_data(AlarmTime1.secondl + '0');lcd_write_data(' ');lcd_write_data('O');if (Alarm1Enable){lcd_write_data('n');}else{lcd_write_data('f');lcd_write_data('f');}pStr = StrAlarm1;Alarm1Cnt =0;}else if(CurrentMode == mode_set_alarm2) {lcd_write_data(AlarmTime2.hourh + '0');lcd_write_data(AlarmTime2.hourl + '0');lcd_write_data(':');lcd_write_data(AlarmTime2.minuteh + '0');lcd_write_data(AlarmTime2.minutel + '0');lcd_write_data(':');lcd_write_data(AlarmTime2.secondh + '0');lcd_write_data(AlarmTime2.secondl + '0');lcd_write_data(' ');lcd_write_data('O');if (Alarm2Enable){lcd_write_data('n');}else{lcd_write_data('f');lcd_write_data('f');}pStr = StrAlarm2;Alarm2Cnt = 0;}lcd_write_inst(0xc0); // set LCD line2 if (CurrentMode != mode_time){for (;*pStr!=0;pStr++){ // write hint messagelcd_write_data(*pStr);}lcd_write_inst(0x0f); // LCD cursor onlcd_write_inst(time_start_addr); // move cursor to start }else // time mode,write date to second line{lcd_write_inst(0x0c); // LCD sursor off/* lcd_write_inst(0xc0 + 3); // set date start address lcd_write_data(CurrentDate.year1 + '0');lcd_write_data(CurrentDate.year2 + '0');lcd_write_data(CurrentDate.year3 + '0');lcd_write_data(CurrentDate.year4 + '0');lcd_write_data('/');lcd_write_data(CurrentDate.monthh + '0');lcd_write_data(CurrentDate.monthl + '0');lcd_write_data('/');lcd_write_data(CurrentDate.dayh + '0');lcd_write_data(CurrentDate.dayl + '0');*/ }if (CurrentMode == mode_set_time){lcd_write_inst(time_start_addr); // move cursor to start }else if (CurrentMode == mode_set_date){lcd_write_inst(time_start_addr); // move cursor to start }else if (CurrentMode == mode_set_alarm1){lcd_write_inst(time_secondl_addr+3);adjust_item = 6;}else if (CurrentMode == mode_set_alarm2){lcd_write_inst(time_secondl_addr+3);adjust_item = 6;}else{lcd_write_inst(0x0c); // LCD cursor off}}// ------------------------------------------------------- void set_date(void){if (adjust_item == 0) // adjust year{if ((KeyNew >=0) & (KeyNew <= 9)){CurrentDate.year1 = KeyNew;lcd_write_data(CurrentDate.year1 + '0');//lcd_write_inst(time_start_addr);adjust_item ++;}else if (KeyNew == key_left){adjust_item = 7;lcd_write_inst(time_start_addr + 9);}else if(KeyNew == key_right){adjust_item ++;lcd_write_inst(time_start_addr + 1);}}else if(adjust_item == 1){if ((KeyNew >=0) & (KeyNew <= 9)){CurrentDate.year2 = KeyNew;lcd_write_data(CurrentDate.year2 + '0');//lcd_write_inst(time_start_addr + 1);adjust_item ++;}else if (KeyNew == key_left){adjust_item --;lcd_write_inst(time_start_addr + 0);}else if(KeyNew == key_right){adjust_item ++;lcd_write_inst(time_start_addr + 2);}}else if(adjust_item == 2){if ((KeyNew >=0) & (KeyNew <= 9)){CurrentDate.year3 = KeyNew;lcd_write_data(CurrentDate.year3 + '0');//lcd_write_inst(time_start_addr + 2);adjust_item ++;}else if (KeyNew == key_left){adjust_item --;lcd_write_inst(time_start_addr + 1);}else if(KeyNew == key_right){adjust_item ++;lcd_write_inst(time_start_addr + 3);}}else if(adjust_item == 3){if ((KeyNew >=0) & (KeyNew <= 9)){CurrentDate.year4 = KeyNew;lcd_write_data(CurrentDate.year4 + '0');//lcd_write_inst(time_start_addr + 3);adjust_item ++;lcd_write_inst(time_start_addr + 5);}else if (KeyNew == key_left){adjust_item --;lcd_write_inst(time_start_addr + 2);}else if(KeyNew == key_right){adjust_item ++;lcd_write_inst(time_start_addr + 5);}}else if(adjust_item == 4)if (((CurrentDate.monthl>2) & (KeyNew == 0)) | ((CurrentDate.monthl == 0) & (KeyNew == 1))| (((CurrentDate.monthl == 1) | (CurrentDate.monthl == 2)) & (KeyNew <2))) {CurrentDate.monthh = KeyNew;lcd_write_data(CurrentDate.monthh + '0');//lcd_write_inst(time_start_addr + 5);adjust_item ++;}else if (KeyNew == key_left){adjust_item --;lcd_write_inst(time_start_addr + 3);}else if (KeyNew == key_right){adjust_item ++;lcd_write_inst(time_start_addr + 6);}}else if(adjust_item == 5){if (((CurrentDate.monthh == 3) & (KeyNew <2)) | ((CurrentDate.monthh != 3) & (KeyNew >=0) & (KeyNew <=9))){CurrentDate.monthl = KeyNew;lcd_write_data(CurrentDate.monthl + '0');//lcd_write_inst(time_start_addr + 6);adjust_item ++;lcd_write_inst(time_start_addr + 8);}else if (KeyNew == key_left){adjust_item --;lcd_write_inst(time_start_addr + 5);}else if (KeyNew == key_right){adjust_item ++;lcd_write_inst(time_start_addr + 8);}}else if(adjust_item == 6)if (((CurrentDate.dayl>1) & ((KeyNew == 1) | (KeyNew == 2))) | ((CurrentDate.dayl == 0) & (KeyNew >0) & (KeyNew<4))| ((CurrentDate.dayl == 1) & (KeyNew <4))){CurrentDate.dayh = KeyNew;lcd_write_data(CurrentDate.dayh + '0');//lcd_write_inst(time_start_addr + 8);adjust_item ++;}else if (KeyNew == key_left){adjust_item --;lcd_write_inst(time_start_addr + 6);}else if (KeyNew == key_right){adjust_item ++;lcd_write_inst(time_start_addr + 9);}}else if(adjust_item == 7){if (((CurrentDate.dayh == 3) & (KeyNew <2)) | ((CurrentDate.dayh != 3) & (KeyNew >=0) & (KeyNew <=9))){CurrentDate.dayl = KeyNew;lcd_write_data(CurrentDate.dayl + '0');//lcd_write_inst(time_start_addr + 9);adjust_item ++;lcd_write_inst(time_start_addr + 0);}else if (KeyNew == key_left){adjust_item --;lcd_write_inst(time_start_addr + 8);}else if (KeyNew == key_right){adjust_item = 0;lcd_write_inst(time_start_addr + 0);}}}// -------------------------------------------------------void set_time(void){if (adjust_item == 0) // set hourh{if (((CurrentTime.hourl <4) & (KeyNew < 3)) | ((CurrentTime.hourl >3) & (KeyNew <2))){CurrentTime.hourh = KeyNew;lcd_write_data(CurrentTime.hourh + '0'); // refresh hourh//lcd_write_inst(0x10); // move cursor backadjust_item ++;}else if(KeyNew == key_left){adjust_item = 5;lcd_write_inst(time_secondl_addr);}else if(KeyNew == key_right){adjust_item ++;lcd_write_inst(time_hourl_addr);}}else if (adjust_item == 1) // set hourl{if (((CurrentTime.hourh == 2) & (KeyNew < 4)) | ((CurrentTime.hourh < 2) & (KeyNew <=9))){CurrentTime.hourl = KeyNew;lcd_write_data(CurrentTime.hourl + '0'); // refresh hourl//lcd_write_inst(0x10); // move cursor backadjust_item ++;lcd_write_inst(time_minuteh_addr);}else if(KeyNew == key_left){adjust_item --;lcd_write_inst(time_hourh_addr);}else if(KeyNew == key_right){adjust_item ++;lcd_write_inst(time_minuteh_addr);}}else if (adjust_item == 2) // set minuteh{if (KeyNew <6){CurrentTime.minuteh = KeyNew;lcd_write_data(CurrentTime.minuteh + '0');//lcd_write_inst(0x10); // move cursor backadjust_item ++;}else if(KeyNew == key_left){adjust_item --;lcd_write_inst(time_hourl_addr);}else if(KeyNew == key_right){adjust_item ++;lcd_write_inst(time_minutel_addr);}}else if (adjust_item == 3) // set minutel{if ((KeyNew >=0) & (KeyNew <=9)){CurrentTime.minutel = KeyNew;lcd_write_data(CurrentTime.minutel + '0');//lcd_write_inst(0x10); // move cursor backadjust_item ++;lcd_write_inst(time_secondh_addr);}else if(KeyNew == key_left){adjust_item --;lcd_write_inst(time_minuteh_addr);}else if(KeyNew == key_right){adjust_item ++;lcd_write_inst(time_secondh_addr);}}else if (adjust_item == 4) // set secondh{if (KeyNew <6){CurrentTime.secondh = KeyNew;lcd_write_data(CurrentTime.secondh + '0');//lcd_write_inst(0x10); // move cursor backadjust_item ++;}else if(KeyNew == key_left){adjust_item --;&nb, sp; lcd_write_inst(time_minutel_addr);}else if(KeyNew == key_right){adjust_item ++;lcd_write_inst(time_secondl_addr);}}else if (adjust_item == 5) // set secondl{if ((KeyNew >=0) & (KeyNew <=9)){CurrentTime.secondl = KeyNew;lcd_write_data(CurrentTime.secondl + '0');//lcd_write_inst(0x10); // move cursor backadjust_item = 0;lcd_write_inst(time_hourh_addr);}else if(KeyNew == key_left){adjust_item --;lcd_write_inst(time_secondh_addr);}else if(KeyNew == key_right){adjust_item = 0;lcd_write_inst(time_hourh_addr);}}}// -------------------------------------------------------void set_alarm1(void){if (adjust_item == 0) // set hourh{if (((AlarmTime1.hourl <4) & (KeyNew < 3)) | ((AlarmTime1.hourl >3) & (KeyNew <2))){AlarmTime1.hourh = KeyNew;lcd_write_data(AlarmTime1.hourh + '0'); // refresh hourh//lcd_write_inst(0x10); // move cursor backadjust_item ++;}else if(KeyNew == key_left){adjust_item = 6;lcd_write_inst(time_secondl_addr + 3);}else if(KeyNew == key_right){adjust_item ++;lcd_write_inst(time_hourl_addr);}}else if (adjust_item == 1) // set hourl{if (((AlarmTime1.hourh == 2) & (KeyNew < 4)) | ((AlarmTime1.hourh < 2) & (KeyNew <=9))){AlarmTime1.hourl = KeyNew;lcd_write_data(AlarmTime1.hourl + '0'); // refresh hourl//lcd_write_inst(0x10); // move cursor backadjust_item ++;lcd_write_inst(time_minuteh_addr);}else if(KeyNew == key_left){adjust_item --;lcd_write_inst(time_hourh_addr);}else if(KeyNew == key_right){adjust_item ++;lcd_write_inst(time_minuteh_addr);}}else if (adjust_item == 2) // set minuteh{if (KeyNew <6){AlarmTime1.minuteh = KeyNew;lcd_write_data(AlarmTime1.minuteh + '0');//lcd_write_inst(0x10); // move cursor backadjust_item ++;}else if(KeyNew == key_left){adjust_item --;lcd_write_inst(time_hourl_addr);}else if(KeyNew == key_right){adjust_item ++;lcd_write_inst(time_minutel_addr);}}else if (adjust_item == 3) // set minutel{if ((KeyNew >=0) & (KeyNew <=9)){AlarmTime1.minutel = KeyNew;lcd_write_data(AlarmTime1.minutel + '0');//lcd_write_inst(0x10); // move cursor backadjust_item ++;lcd_write_inst(time_secondh_addr);}else if(KeyNew == key_left){adjust_item --;lcd_write_inst(time_minuteh_addr);}else if(KeyNew == key_right){adjust_item ++;lcd_write_inst(time_secondh_addr);}}else if (adjust_item == 4) // set secondh{if (KeyNew <6){AlarmTime1.secondh = KeyNew;lcd_write_data(AlarmTime1.secondh + '0');//lcd_write_inst(0x10); // move cursor backadjust_item ++;}else if(KeyNew == key_left){adjust_item --;lcd_write_inst(time_minutel_addr);}else if(KeyNew == key_right){adjust_item ++;lcd_write_inst(time_secondl_addr);}}else if (adjust_item == 5) // set secondl{if ((KeyNew >=0) & (KeyNew <=9)){AlarmTime1.secondl = KeyNew;lcd_write_data(AlarmTime1.secondl + '0');//lcd_write_inst(0x10); // move cursor backadjust_item ++;lcd_write_inst(time_secondl_addr+3);}else if(KeyNew == key_left){adjust_item --;lcd_write_inst(time_secondh_addr);}else if(KeyNew == key_right){adjust_item ++;lcd_write_inst(time_secondl_addr+3);}}else if (adjust_item == 6) // set on/off{if ((KeyNew == key_up) | (KeyNew == key_down)){if (Alarm1Enable){Alarm1Enable =false; // disable alarm1lcd_write_data('f');lcd_write_data('f');}else{Alarm1Enable =true; // enable alarm1lcd_write_data('n');lcd_write_data(' ');}//lcd_write_inst(time_secondl_addr+3);adjust_item = 0;lcd_write_inst(time_hourh_addr);Alarm1Cnt = 0;}else if(KeyNew == key_left){adjust_item --;lcd_write_inst(time_secondl_addr);}else if(KeyNew == key_right){adjust_item = 0;lcd_write_inst(time_hourh_addr);}}}// -------------------------------------------------------void set_alarm2(void){if (adjust_item == 0) // set hourh{if (((AlarmTime2.hourl <4) & (KeyNew < 3)) | ((AlarmTime2.hourl >3) & (KeyNew <2))){AlarmTime2.hourh = KeyNew;lcd_write_data(AlarmTime2.hourh + '0'); // refresh hourh//lcd_write_inst(0x10); // move cursor backadjust_item ++;}else if(KeyNew == key_left){adjust_item = 6;lcd_write_inst(time_secondl_addr+3);}else if(KeyNew == key_right){adjust_item ++;lcd_write_inst(time_hourl_addr);}}else if (adjust_item == 1) // set hourl{if (((AlarmTime2.hourh == 2) & (KeyNew < 4)) | ((AlarmTime2.hourh < 2) & (KeyNew <=9))){AlarmTime2.hourl = KeyNew;lcd_write_data(AlarmTime2.hourl + '0'); // refresh hourl//lcd_write_inst(0x10); // move cursor backadjust_item ++;lcd_write_inst(time_minuteh_addr);}else if(KeyNew == key_left){adjust_item --;lcd_write_inst(time_hourh_addr);}else if(KeyNew == key_right){adjust_item ++;lcd_write_inst(time_minuteh_addr);}}else if (adjust_item == 2) // set minuteh{if (KeyNew <6){AlarmTime2.minuteh = KeyNew;lcd_write_data(AlarmTime2.minuteh + '0');//lcd_write_inst(0x10); // move cursor backadjust_item ++;}else if(KeyNew == key_left){adjust_item --;lcd_write_inst(time_hourl_addr);}else if(KeyNew == key_right){adjust_item ++;lcd_write_inst(time_minutel_addr);}}else if (adjust_item == 3) // set minutel{if ((KeyNew >=0) & (KeyNew <=9)){AlarmTime2.minutel = KeyNew;lcd_write_data(AlarmTime2.minutel + '0');//lcd_write_inst(0x10); // move cursor backadjust_item ++;lcd_write_inst(time_secondh_addr);}else if(KeyNew == key_left){adjust_item --;lcd_write_inst(time_minuteh_addr);}else if(KeyNew == key_right){adjust_item ++;lcd_write_inst(time_secondh_addr);}}else if (adjust_item == 4) // set secondh{if (KeyNew <6){AlarmTime2.secondh = KeyNew;lcd_write_data(AlarmTime2.secondh + '0');//lcd_write_inst(0x10); // move cursor backadjust_item ++;}else if(KeyNew == key_left){adjust_item --;lcd_write_inst(time_minutel_addr);}else if(KeyNew == key_right){adjust_item ++;lcd_write_inst(time_secondl_addr);}}else if (adjust_item == 5) // set secondl{if ((KeyNew >=0) & (KeyNew <=9)){AlarmTime2.secondl = KeyNew;lcd_write_data(AlarmTime2.secondl + '0');//lcd_write_inst(0x10); // move cursor backadjust_item ++;lcd_write_inst(time_secondl_addr+3);}else if(KeyNew == key_left){adjust_item --;lcd_write_inst(time_secondh_addr);}else if(KeyNew == key_right){adjust_item ++;lcd_write_inst(time_secondl_addr+3);}}else if (adjust_item == 6) // set on/off{if ((KeyNew == key_up) | (KeyNew == key_down)){if (Alarm2Enable){Alarm2Enable =false; // disable alarm2lcd_write_data('f');lcd_write_data('f');}else{Alarm2Enable =true; // enable alarm2lcd_write_data('n');lcd_write_data(' ');}//lcd_write_inst(time_secondl_addr+3);adjust_item = 0;lcd_write_inst(time_hourh_addr);Alarm2Cnt = 0;}else if(KeyNew == key_left){adjust_item --;lcd_write_inst(time_secondl_addr);}else if(KeyNew == key_right){adjust_item = 0;lcd_write_inst(time_hourh_addr);}}}// -------------------------------------------------------void main(void){unsigned char cnt;TRISC = 0x03; // PORTC.3 drive led,low activePORTC_MAP = 0x00;led = 0;BackLightEn = 0;BackLightTimer = 0;PORTC = PORTC_MAP;TRISA = 0xff; // low half byte as keyscan inTRISE = 0x00;ADCON0 = 0x00;ADCON1 = 0x06; // all digital I/Oslcd_init();INTCON = 0x00;lcd_write_inst(0x80); // set lcd ddram addressfor (pStr = StrPower1;*pStr!=0;pStr++){lcd_write_data(*pStr);}lcd_write_inst(0x0c); // LCD cursor offPIR1 = PIR2 = 0x00;T1CON = 0x0f; // T1CON: -- T1CKPS1 T1CPS0 T1OSCEN /T1SYNC TMR1CS TMR1ONTMR1H = 0x80;TMR1L = 0x00;。

用c语言实现钟表

用c语言实现钟表
要使用 C 语言编写一个简单的钟表程序,你可以按照以下步骤进行:
1. 引入所需的头文件:你需要引入一些头文件来实现程序的功能,例如 stdio.h、stdlib.h、time.h 等。
2. 定义程序的数据结构:你可以定义一个结构体来表示钟表的时间,包括时、分、秒等字段。
3. 获取当前时间:使用 time 函数获取当前系统时间,并将其转换为时、分、秒等字段。
localTime->tm_sec);
22.
23.
// 暂停一秒钟
Байду номын сангаас
24.
sleep(1);
25. }
26.
27. return 0;
28. }
29. 这个程序会不断获取当前系统时间,并输出格式为"时:分:秒"的钟表界面。程序会每 秒钟更新一次时间,并在终端上显示。你可以根据需要进行适当的修改和扩展,例
time_t currentTime = time(NULL);
17.
struct tm *localTime = localtime(&currentTime);
18.
19.
// 输出钟表界面
20.
system("clear"); // 清屏(适用于 Linux 和 Mac)
21.
printf("%02d:%02d:%02d\n", localTime->tm_hour, localTime->tm_min,
7. 下面是一个简单的 C 语言实现钟表的代码示例:
8. c 复制代码
9. #include <stdio.h>
10. #include <stdlib.h>

模拟时钟程序

模拟时钟程序

1模拟时钟程序分析模拟时钟程序是tc图形模式编程的典型程序,主要用画圆、画线函数实现,程序流程图如图1:图1 时钟程序流程图代码如下:#include<graphics.h>#include<math.h>#include<dos.h>#define PI 3.1416void init_screen(int x0,int y0,int r0);void sec();main(){int x0=320,y0=240,r0=150; /* x0,y0 圆心坐标点*/init_screen(x0,y0,r0);sec();closegraph(); /* 关闭图形模式*/}void init_screen(int x0,int y0,int r0){int i,x,y,graphdriver,graphmode;char s[10];float alpha,a0=PI/2;graphdriver=DETECT;initgraph(&graphdriver,&graphmode,"");setbkcolor(3);setcolor(2);circle(x0,y0,r0); /* 内圆*/circle(x0,y0,r0+30); /* 外圆*/setfillstyle(SOLID_FILL,10);/*设置填充模式和颜色*/floodfill(x0-r0-10,y0,2); /*填充一个有界区域*//* 填写时钟数字 */for(i=1;i<=12;i++){alpha=a0-30*i*PI/180;x=x0+cos(alpha)*r0-16;y=y0-sin(alpha)*r0;setcolor(4);settextstyle(0,0,2);/*为图形输出设置当前的文本属性settextstyle设置fornt 为字体:DEFAULT_FONT,TRIPLEX_FONT,SMALL_FONT,SANSSERIF_FONT,GOTHIC_FONT,也可以用0 ~ 4代替。

模拟时钟行走 C语言程序

模拟时钟行走   C语言程序

模拟时钟行走课程设计总体要求采用模块儿化程序设计;鼓励可视化编程;源程序中有足够的注释;学生可自行增加新功能模块儿;必须上机调试通过;注重算法运用,优化存储效率与运算效率;需提交源程序及相关文件;目录1 课程任务书2 系统设计3 模块设计3.1 总体结构3.2 流程图3.3 使用的主要函数4 调试及测试1、调试过程中的问题2、调试结果5 设计总结6 心得体会及致谢7 答辩记录8 教师意见一、课程设计任务书在屏幕上显示一个活动时钟;能模拟机械钟表行走;准确地利用数字显示日期和时间;按任意键时程序退出。

二、系统设计总体结构:在绘图窗口中先画出表盘后获取系统的时间,利用得到的系统时间计算表针的位置,并将时间在屏幕上输出。

每隔一秒读取一次时间,直到键盘有输入为止。

流程图开始初始化绘画窗口画表盘否获取系统时间结束使用的主要函数:setlinestyle 设置画线setcolor 设置颜色line(int x1,int y1,int x2,int y2)画直线circle(int x,int y,int r)画圆outtextxy(int x,int y,char *textstring)在指定位置输出字符initgraph(int x, int y); 初始化绘图窗口setwritemode( ); 设置绘图模式kbhit() 检查是否有键盘输入GetLocalTime(&ti); 获取当前时间sleep() 程序暂停若干时间三、模块设计主要模块功能、源代码及注释:1.计算表针的位置并画出表针void Drawzhizhen(int hour, int minute, int second){double a_hour, a_min, a_sec; // 时、分、秒针的弧度值int x_hour, y_hour, x_min, y_min, x_sec, y_sec; // 时、分、秒针的位置a_sec = second * 2 * PI / 60; // 计算时、分、秒针的弧度值a_min = minute * 2 * PI / 60 + a_sec / 60;a_hour= hour * 2 * PI / 12 + a_min / 12;x_sec = int(120 * sin(a_sec)); y_sec = int(120 * cos(a_sec)); ///计算时、分、秒针的位置x_min = int(100 * sin(a_min)); y_min = int(100 * cos(a_min));x_hour= int(70 * sin(a_hour)); y_hour= int(70 * cos(a_hour));setlinestyle(PS_SOLID, NULL, 10); // 画时针setcolor(BLUE);line(300 + x_hour, 240 - y_hour, 300, 240 );setlinestyle(PS_SOLID, NULL, 5);// 画分针setcolor(GREEN);line(300 + x_min, 240 - y_min, 300, 240 );setlinestyle(PS_SOLID, NULL, 2); // 画秒针setcolor(WHITE);line(300 + x_sec, 240 - y_sec, 300, 240 );}2.画表盘void Drawbiaopan(){int i,l,x1,x2,y1,y2;setbkcolor(1);circle(300,240,180);circle(300,240,185);for(i=0;i<60;i++) /*划钟点上的短线*/ {if(i%5==0)l=15;elsel=5;x1=180*sin(i*6*PI/180)+300;y1=180*cos(i*6*PI/180)+240;x2=(180-l)*sin(i*6*PI/180)+300;y2=(180-l)*cos(i*6*PI/180)+240;line(x1,y1,x2,y2);}outtextxy(300,80,"12") ;outtextxy(300,390,"6");outtextxy(140,230,"9");outtextxy(460,230,"3");outtextxy(380,100,"1");outtextxy(220,100,"11");outtextxy(430,160,"2");outtextxy(430,310,"4");outtextxy(380,370,"5");outtextxy(220,370,"7");outtextxy(160,160,"10");outtextxy(160,310,"8");}3.将获取的整数型时间变量转换成字符型void ch1(int a,int x) //将年转换成字符型并输出{char b,c,d,e;b=(char)(a/1000+48);c=(char)((a%1000)/100+48);d=(char)((a%100)/10+48);e=(char)(a%10+48);outtextxy(x,10,b);outtextxy(x+10,10,c);outtextxy(x+20,10,d);outtextxy(x+30,10,e);}void ch2(int a,int x) //其他时间转换成字符型并输出{char b,c;b=(char)(a/10+48);c=(char)(a%10+48);outtextxy(x,10,b);outtextxy(x+10,10,c);}主函数:void main(){initgraph(640, 480); // 初始化绘图窗口Drawbiaopan(); // 绘制表盘setwritemode(R2_XORPEN); // 设置绘图模式SYSTEMTIME ti; // 定义ti保存当前时间while(!kbhit()) // 按任意键退出钟表程序{GetLocalTime(&ti); // 获取当前时间ch1(ti.wYear,10);outtextxy(50,10,"年"); //输出时间ch2(ti.wMonth,65);outtextxy(85,10,"月");ch2(ti.wDay,100);outtextxy(120,10,"日");ch2(ti.wHour,135);outtextxy(155,10,":");ch2(ti.wMinute,165);outtextxy(185,10,":");ch2(ti.wSecond,195);Drawzhizhen(ti.wHour, ti.wMinute, ti.wSecond); // 画表针Sleep(1000); // 延时1 秒Drawzhizhen(ti.wHour, ti.wMinute, ti.wSecond); // 画表针}closegraph(); // 关闭绘图窗口}四调试及测试使用vc企业版进行反复调试五设计总结调试过程出现的问题:1.Vc++6.0中没有“graphics.h”,安装turboc.2.0中的函数库EasyX后出现权限问题。

vc编写一个模拟时钟的程序(VC write an analog clock program)

vc编写一个模拟时钟的程序(VC write an analog clock program)

vc编写一个模拟时钟的程序(VC write an analog clock program)I'm sorry, my program is too big, 4M, because there are other functionsNo way. Towel section proceduresVoid CFill_areaView:: myShowColok (CDC, *pDC){M_time=CTime:: GetCurrentTime ();PDC->FillRect (&CRect (0,0, rect.Width (), rect.Height ()), &CBrush (RGB (100100100));CBrush brush (RGB (150150150));CBrush *oldbrush=pDC->SelectObject (&brush);CPen pen (PS_SOLID, 3, RGB (0,0,0));CPen *oldpen=pDC->SelectObject (&pen);PDC->Ellipse (rect.Width (), /2- (rect.Height ()), /2, rect.Height () (/2-10 ()), /2- (rect.Height ()), /2, rect.Width (), /2+ (rect.Height ()), /2, rect.Height () (&CRect (), /2+ (rect.Height ()), /2-10 (/2-10) /2) /2-10 ());PDC->Ellipse (&CRect (rect.Width ()), /2-3, rect.Height (), /2-3, rect.Width (), /2+3, rect.Height () /2+3);CPen PEN2 (PS_SOLID, 7, RGB (0,0,0));PDC->SelectObject (&pen2);PDC->MoveTo (rect.Width (), /2, rect.Height (), /2-16* (rect.Height (), /2-10) /20);PDC->LineTo (rect.Width (), /2-15* (rect.Height ()), /20, rect.Height (), /2-6* (rect.Height ()) /2-10) /20 (/2-10);PDC->LineTo (rect.Width (), /2-15* (rect.Height ()), /20, rect.Height (), /2+12* (rect.Height ()) /2-10) /20 (/2-10);PDC->LineTo (rect.Width (), /2+15* (rect.Height ()), /20, rect.Height (), /2+12* (rect.Height ()) /2-10) /20 (/2-10);PDC->LineTo (rect.Width (), /2+15* (rect.Height ()), /20, rect.Height (), /2-6* (rect.Height ()) /2-10) /20 (/2-10);PDC->LineTo (rect.Width (), /2, rect.Height (), /2-16* (rect.Height (), /2-10) /20);PDC->MoveTo (rect.Width (), /2-15* (rect.Height ()), /20+25, rect.Height (), /2+12* (rect.Height ()) /2-10) /20 (/2-10);PDC->LineTo (rect.Width (), /2-15* (rect.Height ()), /20+25, rect.Height (), /2+12* (rect.Height ()) /2-10) /20+30 (/2-10);PDC->LineTo (rect.Width (), /2-15* (rect.Height ()), /20+25+40, rect.Height (), /2+12* (rect.Height ()) /2-10) /20+30 (/2-10);PDC->LineTo (rect.Width (), /2-15* (rect.Height ()), /20+25+40, rect.Height (), /2+12* (rect.Height ()) /2-10) /20 (/2-10);PDC->MoveTo (rect.Width (), /2+15* (rect.Height ()), /20-25, rect.Height (), /2+12* (rect.Height ()) /2-10) /20 (/2-10);PDC->LineTo (rect.Width (), /2+15* (rect.Height ()), /20-25, rect.Height (), /2+12* (rect.Height ()) /2-10) /20+30 (/2-10);PDC->LineTo (rect.Width (), /2+15* (rect.Height ()), /20-25-40, rect.Height (), /2+12* (rect.Height ()) /2-10) /20+30 (/2-10);PDC->LineTo (rect.Width (), /2+15* (rect.Height ()), /20-25-40, rect.Height (), /2+12* (rect.Height ()) /2-10) /20 (/2-10);CPen pen1 (PS_SOLID, 1, RGB (0,0,0));PDC->SelectObject (&pen1);Double alpha=0;MyPoint, P1, P2, p3;P1=m_pointColok[0], p2=m_pointColok[4], p3=m_pointColok[5];For (int i=0; i<=60; i++){Alpha=i*6*pi/180;P1.x=m_pointColok[0].x*cos (alpha) -m_pointColok[0].y*sin (alpha);P1.y=m_pointColok[0].x*sin (alpha) +m_pointColok[0].y*cos (alpha);PDC->MoveTo (ChangePoint (P1).X, ChangePoint (P1).Y);P2.x=m_pointColok[4].x*cos (alpha) -m_pointColok[4].y*sin (alpha);P2.y=m_pointColok[4].x*sin (alpha) +m_pointColok[4].y*cos (alpha);P3.x=m_pointColok[5].x*cos (alpha) -m_pointColok[5].y*sin (alpha);P3.y=m_pointColok[5].x*sin (alpha) +m_pointColok[5].y*cos (alpha);If (i%5==0)PDC->LineTo (ChangePoint (P2).X, ChangePoint (P2).Y);ElsePDC->LineTo (ChangePoint (P3).X, ChangePoint (P3).Y);PDC->SetBkMode (TRANSPARENT);CFont font;Font.CreateFont (16,12,0,0, FW_NORMAL),假,假,假,ansi_charset,out_default_precis,clip_default_precis,default_quality,default_pitch | ff_swiss,“Arial”);CFont * oldfont = PDC -> SelectObject(与字体);/ /改变字体PDC -> SetTextColor(RGB(255205100));PDC -> TextOut(矩形。

C#实例——钟表模拟

C#实例——钟表模拟

目录摘要: (1)1.C#语言简介 (2)2.实现目标 (2)3.主要方法 (3)4.实验过程中的难点 (3)5.实验过程 (4)6.总结 (6)附录(源程序): (7)用C#实现时钟模拟用C#实现时钟模拟摘要:我们日常生活中有各种各样的时钟。

随着计算机的普及和计算机科学的飞速发展人们开始越来越多的利用计算机解决实际问题,开发一个易于查看的时钟有较大的现实意义。

VisualStudiio 2008是面向Windows Vista、Office 2007、Web 2.0的下一代开发工具,其强大的兼容性以及高速处理能力帮助程序人员轻松应对研究、测试、开发过程中的各种问题。

对于Web,ASP NET AJAX 及其他新技术使开发人员能够迅速创建更高效、交互式更强和更个性化的新一代Web 体验。

本文基于VisualStudiio 2008采用C#语言,应用GDI+应用程序编程借口(API)实现时钟模拟。

关键词:C#开发时钟模拟GDI+ VisualStudio20081.C#语言简介C#是一种安全的、稳定的、简单的、优雅的,由C和C++衍生出来的面向对象的编程语言。

它在继承C和C++强大功能的同时去掉了一些它们的复杂特性(例如没有宏以及不允许多重继承)。

C#综合了VB简单的可视化操作和C++的高运行效率,以其强大的操作能力、优雅的语法风格、创新的语言特性和便捷的面向组件编程的支持成为.NET开发的首选语言。

C#旨在设计成为一种“简单、现代、通用”,以及面向对象的程序设计语言,此种语言的实现,应提供对于以下软件工程要素的支持:强类型检查、数组维度检查、未初始化的变量引用检测、自动垃圾收集(Garbage Collection,指一种自动内存释放技术)。

软件必须做到强大、持久,并具有较强的编程生产力。

此种语言为在分布式环境中的开发提供适用的组件开发应用。

为使程序员容易迁移到这种语言,源代码的可移植性十分重要,尤其是对于那些已熟悉C和C++的程序员而言。

7用C#编的模拟时钟(附源代码)

7用C#编的模拟时钟(附源代码)

使用C#模拟时钟表的一种简单制作方法PointF center=new PointF(s_pinlen+3,s_pinlen+3);//中心点位置SolidBrush sb=new SolidBrush(Color.Black);//时钟圆心的刷子除上述变量声明外,时钟表功能编写子方法,方法名为:AngleToPos和myClock,方法AngleToPos是根据角度和百分比计算出一个点的坐标函数,代码如下:PointF AngleToPos(int angle,float percent){PointF pos=new PointF();double radian=angle*Math.PI/180;pos.Y=center.Y-s_pinlen*percent*(float)Math.Sin(radian);pos.X=center.X+s_pinlen*percent*(float)Math.Cos(radian);return pos;}方法myClock主要是绘制时钟表的中心位置、秒针、分针及时针的结束位置。

代码如下: Pen pDisk=new Pen(Color.Orange,3);//时钟背景的笔Pen pScale=new Pen(Color.Coral);//刻度的笔Graphics myGraphics=pictureBox1.CreateGraphics();myGraphics.Clear(Color.White);Pen myPen=new Pen(Color.Black,2);Point CPoint=new Point(s_pinlen,s_pinlen);Point SPoint=new Point((int)(CPoint.X+(Math.Sin(6*s*Math.PI/180))* s_pinlen),(int)(CPoint.Y-(Math.Cos(6*s*Math.PI/180))*s_pinlen));Point MPoint=new Point((int)(CPoint.X+(Math.Sin(6*m*Math.PI/180))* m_pinlen),(int)(CPoint.Y-(Math.Cos(6*m*Math.PI/180))*m_pinlen));Point HPoint=new Point((int)(CPoint.X+(Math.Sin(((30*h)+(m/2))*Ma th.PI/180))*h_pinlen),(int)(CPoint.Y-(Math.Cos(((30*h)+(m/2))*Mat h.PI/180))*h_pinlen));myGraphics.FillEllipse(sb,center.X-8,center.Y-7,14,14);myGraphics.DrawLine(myPen,CPoint,SPoint);myPen=new Pen(Color.Blue,4);。

《C#程序设计(第2版)》电子教案 项目十 绘制模拟时钟

《C#程序设计(第2版)》电子教案 项目十 绘制模拟时钟
提示: 2个步骤分2天时间实现。
任务一 自定义用户控件的设计
新建项目,为窗体设置背景图 片并在相应事件中写入代码。
ቤተ መጻሕፍቲ ባይዱ
任务一 自定义用户控件设计步骤:
1. 创建 ClockDesign控件库 2. 创建 ClockControl控件 3. 创建clockTimer控件,拖放Timer控
件 4. 自定义OnPaint事件处理程序 5. 定时(1秒)重绘控件
自定义用户控件设计的相关理论知识
一、c# 如何重写控件的OnPaint事件 二、C# 图形绘制——指定线条端部形状
1、使用预定义形状 2、使用自定义形状
任务二 自定义用户控件的使用
在同一个解决方案中,新建工程窗体,使用昨天大家创 建的用户自定义控件。最终效果图如下:
项目十 绘制模拟时钟 小结
通过本项目,学生能绘制出模 拟时钟。在此过程中,学生掌握了 如何自定义用户控件,也掌握测试 与使用用户控件的基本方法。
C#程序设计
项目十 绘制模拟时钟
项目十 绘制模拟时钟
游戏公司最近请软件工程师小李绘制模拟时钟用于 显示当前系统时间,小李在分析了网络上现有的代 码后,再考虑到代码的重用性,决定先自定义一个 时钟控件,然后用拖放的方式使用。效果图如下:
项目十 绘制模拟时钟分析
绘制模拟时钟主要的步骤: 1.自定义用户控件的设计 2.自定义用户控件的使用

数字时钟C程序及仿真

数字时钟C程序及仿真

数字时钟C程序及仿真-CAL-FENGHAI-(2020YEAR-YICAI)_JINGBIAN**************************************/#include <>unsigned char code SEG7[12]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xbf,0xff,};unsigned short int tcount;//sbit set=P1^0;//sbit up=P1^1;//sbit down=P1^2;unsigned charDisSecondL,DisSecondH,DisMinutL,DisMinutH,DisHourL,DisHourH;//结构体变量struct time{unsigned char second;unsigned char minute;unsigned char hour;}time1;//延时子程序void delay(k)unsigned int k;{unsigned char j;while((k--)!=0)for(j=0;j<125;j++);}//时钟调整子程序TimeUpDown(n,x,y)unsigned char n,x,y;{if(up==0){delay(50); //消除抖动if(up==0){n++;if(n==x)//上限越界显示为0n=0;while(up==0);}}if(down==0){delay(50);if(down==0){if(n==0)//下限越界显示为最大可显示n=y;elsen--;while(down==0);}}return n;//返回调整后的值} *///显示子程序void display(DisSecondL,DisSecondH,DisMinutL,DisMinutH,DisHourL,DisHourH) {// 日日日日日日// 1 2 3 4 5 6// hH hL mH mL sH sL// 01 02 04 08 10 20P2=0x00;P0=SEG7[DisSecondL]; //显示秒钟的个位P2=0x20;delay(3);P2=0x00;P0=SEG7[DisSecondH]; //显示秒钟的十位P2=0x10;delay(3);P2=0x00;P0=SEG7[DisMinutL]; //显示分钟的个位P2=0x08;delay(3);P2=0x00;P0=SEG7[DisMinutH]; //显示分钟的十位P2=0x04;delay(3);P2=0x00;P0=SEG7[DisHourL]; //显示小时的个位P2=0x02;delay(3);P2=0x00;if(DisHourH==0){;}else{P0=SEG7[DisHourH]; //显示小时的十位P2=0x01;delay(3);}}void main(){//unsigned char Flag;=40;=03;=05;TMOD=0x01;TH0=(65536-50000)/256;TL0=(65536-50000)%256;EA=1;ET0=1;TR0=1;while(1){display(DisSecondL,DisSecondH,DisMinutL,DisMinutH,DisHourL,DisHourH);if(set == 0) //设置标志位 Flag{delay(50);if(set == 0){TR0 = 0;Flag++;if(Flag > 3) //0:显示时间,1:小时调整,2:分钟调整,3:秒调整Flag = 0;}while(set == 0);}switch(Flag){case 0:TR0=1;display%10,10,%10,10,%10,10);break;case 1: //时钟小时调整TR0=0;=TimeUpDown,24,23);display(10,10,10,10,%10,10);break;case 2: //时钟分钟调整=TimeUpDown,60,59);display(10,10,%10,10,10,10);break;case 3: //时钟秒调整=TimeUpDown,60,59);display%10,10,10,10,10,10);break;//default:// break;}}}void t0(void) interrupt 1{TH0=(65536-50000)/256; TL0=(65536-50000)%256; tcount++;if(tcount==20){tcount=0;++;}if >=60){=0;++;}if >=60){=0;++;}if>=12){=1;}DisSecondL=%10;DisSecondH=10;DisMinutL=%10;DisMinutH=10;DisHourL=%10;DisHourH=10;}。

基于单片机C语言电子时钟完整版(闹钟,整点报时)

基于单片机C语言电子时钟完整版(闹钟,整点报时)

《单片机技术》课程设计说明书数字电子钟系、部:电气与信息工程学院学生姓名:指导教师:职称专业:班级:完成时间:2013-06-07摘要电子钟在生活中应用非常广泛,而一种简单方便的数字电子钟则更能受到人们的欢迎。

所以设计一个简易数字电子钟很有必要。

本电子钟采用ATMEL公司的AT89S52单片机为核心,使用12MHz 晶振与单片机AT89S52 相连接,通过软件编程的方法实现以24小时为一个周期,同时8位7段LED数码管(两个四位一体数码管)显示小时、分钟和秒的要求,并在计时过程中具有定时功能,当时间到达提前定好的时间进行蜂鸣报时。

该电子钟设有四个按键KEY1、KEY2、KEY3、KEY4和KEU5键,进行相应的操作就可实现校时、定时、复位功能。

具有时间显示、整点报时、校正等功能。

走时准确、显示直观、运行稳定等优点。

具有极高的推广应用价值。

关键词电子钟;AT89S52;硬件设计;软件设计ABSTRACTClock is widely used in life, and a simple digital clock is more welcomed by people. So to design a simple digital electronic clock is necessary.The system use a single chip AT89S52 of ATMEL’s as its core to control The crystal oscillator clock,using of E-12MHZ is connected with the microcontroller AT89S52, through the software programming method to achieve a 24-hour cycle, and eight 7-segment LED digital tube (two four in one digital tube) displays hours, minutes and seconds requirements, and in the time course of a timing function, when the time arrived ahead of scheduled time to buzz a good timekeeping. The clock has four buttons KEY1, KEY2, KEY3,KEY4 and KEY5 key, and make the appropriate action can be achieved when the school, timing, reset. With a time display, alarm clock settings, timer function, corrective action. Accurate travel time, display and intuitive, precision, stability, and so on. With a high application value.Key words Electronic clock;;AT89S52;Hardware Design;Software Design目录1设计课题任务、功能要求说明及方案介绍 (1)1.1设计课题任务 (1)1.2功能要求说明 (1)1.3设计总体方案介绍及原理说明 (1)2设计课题硬件系统的设计 (2)2.1设计课题硬件系统各模块功能简要介绍 (2)2.2设计课题电路原理图、PCB图、元器件布局图 (2)2.3设计课题元器件清单 (5)3设计课题软件系统的设计 (6)3.1设计课题使用单片机资源的情况 (6)3.2设计课题软件系统各模块功能简要介绍 (6)3.3设计课题软件系统程序流程框图 (6)3.4设计课题软件系统程序清单 (10)4设计结论、仿真结果、误差分析、教学建议 (21)4.1设计课题的设计结论及使用说明 (21)4.2设计课题的仿真结果 (21)4.3设计课题的误差分析 (22)4.4设计体会 (22)4.5教学建议 (22)结束语 (23)参考文献 (24)致谢 (25)附录 (26)1 设计课题任务、功能要求说明及方案介绍1.1 设计课题任务设计一个具有特定功能的电子钟。

用 C 语言编写一个闹钟程序

用 C 语言编写一个闹钟程序

要用C 语言编写一个闹钟程序,你需要按照以下步骤进行:
包含必要的头文件:首先,你需要包含<time.h> 和<unistd.h> 两个头文件。

这两个头文件将为你提供时间和睡眠函数,这些函数是实现闹钟功能所必需的。

输入闹钟时间:接下来,你需要让用户输入闹钟的触发时间。

你可以使用scanf() 函数来获取用户输入。

计算睡眠时间:接下来,你需要计算出当前时间和闹钟触发时间之间的时间差,并使用睡眠函数等待该时间差。

你可以使用time() 函数获取当前时间,并使用difftime() 函数计算时间差。

你还可以使用sleep() 函数来等待相应的时间。

播放提醒声音:最后,你需要在闹钟触发时向用户播放一个提醒声音。

你可以使用system() 函数来执行一个系统命令,播放提醒声音。

示例代码如下:
#include <time.h>
#include <unistd.h>
#include <stdio.h>
int main() {
// 输入闹钟时间
int hour, minute;
printf("请输入闹钟时间(小时。

单片机c 语言编程时钟及闹钟程序

单片机c 语言编程时钟及闹钟程序

一·功能1、计时功能,数码管显示数值从00:00:00--23:59:59循环替换,且周期时间与实际时间吻合。

2、定时闹钟功能,按下“定时”键后,可以设定所需要的任意时间,定时完成后,当到达设定时间后,蜂鸣器发声。

3、调整时间功能,根据此项功能可将时钟调至正确的时间。

4、查看定时功能,当设定完成后可以查看上次定时的时间,且能在此基础上进行重新定时。

二·按键说明设定键:按一次开始设定时间,并将设定过程显示在数码管上。

若未按此键,则其他按键无效。

设定过程中,再按一次此键,定时结束,数码管显示返回时钟。

当第一次按下设定键时,显示值为00:00:00,在此基础上调节定时时间。

第一次设定完成后,以后再按设定键,显示初值则为上次定时的时间。

确定键:在定时过程中按下此键,则保留当前设定的时间为定时时间。

若定时过程未按此键,定时无效。

向上键:按下此键,使得当前设定值在现有数值上加一,当加至满位时,当前值变为零。

向下键:按下此键,使得当前设定值在现有数值上减一,当减至零时,当前值变为满位减一。

向左键:按下此键,使得设定值移向左边一位,若已经在最左边,则移至最右边。

向右键:按下此键,使得设定值移向右边一位,若已经在最右边,则移至最左边。

三·具体操作演示(一)·定时及查看定时演示1.仿真开始。

如图:2、按键如图:3、按下设定键,开始设定时间,如图:4、如图所示,当前设定时位。

按向上键,使数值加一。

5、按下向右键,设定位移至分位。

6、按下向下键,使数字减一。

7、按确定键,确定当前设定的时间。

再按设定键,退出定时,开始时钟显示。

8、设定完成后按设定键,显示前次设定值,可在此基础上重新设定,也可直接再按设定键推出。

9、当时钟运行到设定时间时,蜂鸣器发声。

(二)·调整时间演示1、计时开始。

2、按照定时的方法开始设定时间,使其显示20:10:09。

3、调整到正确时间后,按下确定键不放,同时再按一下设定键,将目前设定值送入时钟,使其开始从设定值计时。

用VC++制作-模拟时钟应用程序

用VC++制作-模拟时钟应用程序

案例五模拟时钟应用程序以实例为背景学习基于MFC的WINDOWS应用程序设计,编写一个模拟时钟程序,此程序在屏幕左边有一个指针式钟面,右方有两个矩形框,上面以数字方式显示日期和时间,该时间应与指针显示的时间一致,下方的矩形框作为秒表。

用菜单选项或按钮设置时间和秒表。

时间不必与机器系统时间相同,可任意设置。

模拟时钟示意图1 编程要求(1)为该程序设计一个美观大方的图标。

(2)程序界面设计合理,色彩得体大方,显示正确。

(3)时针、分针和秒针形象美观,即使各指针重合也可辨认。

(4)各指针运动规律正确。

为便于演示,时钟速度应比实际时间快20倍,即1小时相当于3分钟。

(5)数字式时钟的时间显示与指针式时钟显示一致。

(6)按下设置时间按钮或菜单项可弹出一对话框,用于设置当前的时间和日期。

(7)按下秒表控制按钮后,秒表显示窗中显示从0开始的时间,单位为百分之一秒。

再次按下秒表控制按钮后计时停止,该窗口显示累计时间。

2 问题分析本题主要涉及到的知识点有:时钟指针运动算法、屏幕重绘方法、定时器消息、鼠标消息、菜单命令、对话框、画笔/画刷、显示文字等。

指针运动算法和屏幕重绘方法是本程序主要难点所在。

不论何种指针,每次转动均以π/30弧度(一秒的角度)为基本单位,且都以表盘中心为转动圆心。

计算指针端点(x, y)的公式如下:x =圆心x坐标+ 指针长度* cos (指针方向角)y =圆心y坐标+ 指针长度* sin (指针方向角)注意,指针长度是指自圆心至指针一个端点的长度(是整个指针的一部分),由于指针可能跨越圆心,因此一个指针需要计算两个端点。

三个指针的运动是相关联的,秒针转一圈引起分针运动一格,分针转一圈引起时针运动一格,因此应该使用一个定时器消息来处理指针的运动。

若用三个定时器消息分别处理时针、分针和秒针的运动,就会使问题复杂化且不易实现三个指针联动的正确规律。

采用一个定时器消息可以很容易实现指针联动算法。

由于屏幕的重绘速度很快(50 ms一次),如果采用全屏删除式重绘则闪烁十分明显,显示效果不佳。

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

模拟时钟程序1 基本功能描述本次课程设计是基于面向对象的应用程序设计,主要运用C++语言在VC++开发环境下的MFC中编程实现。

模拟时钟的基本功能是程序初始在屏幕上有一指针式时钟表盘,表盘为椭圆形,内部分布有12个刻度,表盘上有三个长度和颜色不同的时针分针和秒针,相互之间容易辨认,指针的运动通过数学推导之后以代码实现。

表盘的下方是一个数字形式显示的数字钟,其显示时间的格式是时:分:秒,指针式时钟和数字式时钟显示的时间同步,且两个时钟所显示的时间与系统时间相致,页面的菜单项设有时间设置项,可以对所显示的时间进行调整,能进行调整的具体内容是年、月、日、时、分、秒。

设计成功之后,此应用程序便可以起到时钟显示的作用。

2 设计思路2.1 程序流程图图1 模拟时钟程序流程图2.2 程序流程分析(1) 绘制指针式的时钟和数字式的时钟图形时,要在CView类下进行。

其中OnDraw()函数在绘制视图窗口时被调用,在定义了画刷CBrush和画笔CPen之后,调用GetClientRect()定义屏幕大小并确定椭圆中心的坐标,然后调用Ellipse绘制椭圆,即指针式的时钟表盘,SetTextColor绘制文本颜色,调用MoveTo和LineTo绘制表盘指针,同时调用CreateFont()创建数字钟字体,TextOut则是用以数字钟的文本输出。

(2) 模拟时钟处理消息的过程:首先调用SetTimer函数定义时钟消息,包括参数指定计时器的ID,消息产生的时间间隔,回调函数为NULL;调用消息处理函数OnTimer()刷新窗口显示。

在相应的WM_TIMER消息处理里添加时钟消息响应代码;最后调用KillTimer 释放该时钟。

(3) 要实现时钟的动态效果,即时间窗显示的时间每隔一秒钟更新一次,需要在时间窗格的正文调用CStatusBar::SetPaneText()函数。

要定时更新,则应利用WM_TIMER消息,计时器每隔一定的时间间隔就会发出一个WM_TIMER消息,而这个时间间隔可由用户指定。

MFC的Windows类提供了WM_TIMER消息处理函数OnTimer(),应在该函数内进行更新时间窗格的工作。

先利用ClassWazard给CMainFrame类加入WM_TIMER消息处理函数OnTimer(),CMainFrame:: OnTimer()函数是在系统发给框架窗口消息WM_TIMER时调用的,在CMainFrame::OnCreate()函数内调用CWnd::SetTimer()以安装一个计时器,使OnTimer()在设定的时间间隔被调用。

(4) 通过调用GetCurrentTime()函数来实现时钟时间与计算机系统时间保持一致,,在时间与计算机同步后,然后在File View选项卡中的Source File资源组里面的little1Doc.cpp 中的void Clittle1Doc::OnEditSet()添加相应的变量以实现对时间的调整。

(5) 在程序设计过程之中添加“时钟控制”菜单,用菜单项控制弹出对话框设置时间,使时钟达到可调的效果。

(6) 程序运行正常,界面设置合理,指针式的时钟和数字式的时钟的运动达到任务书的要求,并且能用菜单项控制对话框以实现对当前时间的调整,程序达到各项功能之后,退出界面,程序结束。

3 软件设计3.1 设计步骤(1)用AppWizard创建一个单文档的的MFC工程,将之命名为little1。

所有步骤均使用AppWizard给出的默认设置。

(2)编辑项目的菜单资源,在主框架窗口的主菜单(IDR_MAINFRAME)中添加一个名为“时钟控制”的下拉菜单。

在“时钟控制”菜单中添加三个菜单选项“启动时钟”、“停止时钟”和“时间设置”。

并在菜单属性项中设定“启动时钟”菜单的ID标号为ID_START,“停止时钟”菜单的ID标号为ID_STOP,“时间设置”菜单的ID标号为ID_SETTIME。

图2 “时钟控制”菜单效果(3)利用ClassWizard为视图类添加菜单命令处理函数。

进入ClassWizard的Message Maps选项卡,选择Class Name项为Clittle1View类,在Object IDs列表框中分别选择新添加的菜单选项的ID,在Messages列表框中选择COMMAND,按下Add Function按钮添加成员函数。

ClassWizard会为“启动时钟”、“停止时钟”和“时间设置”菜单选项添加相应的消息响应函数OnStart ( )、OnStop ( )和OnSettime ( )。

(4)利用ClassWizard为视图类添加定时器和鼠标消息处理函数。

进入ClassWizard 的Message Maps选项卡,选择Class Name项为Clittle1View类,在Messages列表框中分别选择定时器消息WM_TIMER和鼠标消息WM_LBUTIONDOWN,按下Add Function按钮添加成员函数。

ClassWizard会添加相应的定时器和鼠标消息响应函数OnTimer和OnLButtonDown。

(5)使用Developer Studio菜单的Insert / Resource…选项调出Insert Source对话框,为项目添加新的对话框资源。

在对话框属性中,修改对话框名为“时间设定”。

在对话框中增加用来输入年、月、日、时、分、秒的编辑框控件,如下图所示。

图3 时间设置对话框(6)设计好这个对话框模板后,利用ClassWizard自动建立对话框类。

进入ClassWizard 后,它会发现已建立的对话框模板资源,并弹出一个对话框询问是否要为该对话框模板建立类。

按下“OK”按钮,会弹出New Class对话框,在Name栏填写对话框类的名称CSetTimeDlg后按“OK”按钮,即可为对话框创建一个对应的类。

(7)利用ClassWizard为对话框类添加与各控件对应的数据成员。

选择MemberVariables 选项卡,确保Class Name项为对话框CSetTimeDlg类,然后在选项卡下方的窗口中选择各控件的ID并按下“Add Variable…”按钮,为其添加对应成员变量,如表1所示。

表1 编辑控件属性Control IDs Variable Type Member variablename Variable RangeIDC_YEAR Int m_Year0~3000 IDC_MONTH Int m_Month1~12 IDC_DAY Imt m_Day1~31IDC_HOUR Imt m_Hour0~23IDC_MINUTE Int m_Minute0~59IDC_SECOND Int m_Second0~59最后一列为变量取值范围。

(8)完成以上工作后,即可修改程序框架,添加必要的代码。

在Clittle1View类中加入消息处理函数OnTimer(),OnEraseBkgnd()和OnCreate()并添加代码,在Clittle1View::OnDraw(CDC* pDC)中添加绘制时钟钟面、刻度、指针以及输出数字时钟的代码。

(9) 在头文件中添加对各种函数及变量的声明,同时在类的原文件中添加实现功能所需要的各种函数,并添加相应的代码。

(10) 对完成后的程序不断进行调试,分析并修改其中的错误,直到程序运行不再报错,并能够实现所要求的各项功能。

3.2 界面设计模拟时钟的界面设计比较简单,如图3所示,只需6个编辑框和相应的6个静态文本框,由于显示的时间都是整数,所以控件的类型都选为int型,因而所需要设计的控件也比较简单,界面设计的控件表如表2所示。

表2 界面控件表控件序号控件名称控件类型控件ID相应变量1 年编辑框(int)IDC_EDIT1m_edit12 月编辑框(int)IDC_EDIT2m_edit23 日编辑框(int)IDC_EDIT3m_edit34 时编辑框(int)IDC_EDIT4m_edit45 分编辑框(int)IDC_EDIT5m_edit56 秒编辑框(int)IDC_EDIT6m_edit63.3 关键功能的实现3.3.1 模拟时钟显示模拟时钟的显示主要在于绘图这一方面,也是本次模拟时钟课程设计的重中之重。

这一部分所要添加的代码在File View选项卡中的Source File资源组里little1View.cpp中:(1) 首先要调用SetTimer()函数设置时间间隔,语句是SetTimer(1,1000,NULL),SetTimer()的第一个参数指定计时器ID为1,第二个参数规定了计时器的时间间隔为1000ms,即1s,这样,每隔1s就调用一次OnTimer()函数。

(2) 定义画笔CPen和画刷CBrush,并通过调用GetClientRect()定义屏幕大小并确定椭圆中心坐标的位置,横坐标定义为int nCenterX = Rect.right / 2,纵坐标定义为int nCenterY = 10+(Rect.bottom-110 )/ 2,接着调用Ellipse()绘制椭圆的钟面pDC->Ellipse( nCenterX - 300, 10, nCenterX + 300,Rect.bottom - 100 )。

(3) 接着画出表盘12个刻度,先算出每一个刻度对应的角度Radians = (double) i * 6.28 / 12.0,然后算出每一个刻度的横坐标是x = nCenterX -( size.cx / 2 ) +(int) ( 285*sin( Radians ) ),纵坐标是y = nCenterY -( size.cy / 2 ) -(int) ( (double) ( nCenterY - 30 )*cos( Radians ) )。

(4) 调用MoveTo()和LineTo()绘制时钟钟面的指针和刻度,并定义其类型、粗细和颜色,例如时针,CPen HourPen( PS_SOLID, 5, RGB( 0, 255, 0 ) )定义了时针的类型、粗细和颜色,pDC->MoveTo( nCenterX, nCenterY )定义了时针的起点,pDC->LineTo(nCenterX + (int) ( (double) 295/2 *sin( Radians )), nCenterY - (int) ( (double) ( nCenterY -10)/2 *cos( Radians ) ) )定义了时针的终点,刻度、分针和秒针也是如此。

(5) 对于数字钟,先编写数字钟输出时间的格式,具体语句是strDigits.Format( "%d:%02d:%02d",Time.GetHour(),Time.GetMinute(),Time.GetSecond() ),然后定义数字钟的宽度和高度,定义宽度为int nWidth = 25,定义高度为int nHeight = 40,接着调用CreateFont()创建数字钟字体。

相关文档
最新文档