C語(yǔ)言基于EasyX庫(kù)實(shí)現(xiàn)有圖形界面時(shí)鐘
本文實(shí)例為大家分享了C語(yǔ)言基于EasyX庫(kù)實(shí)現(xiàn)有圖形界面時(shí)鐘的具體代碼,供大家參考,具體內(nèi)容如下
1.目標(biāo)要求:
1.實(shí)現(xiàn)一個(gè)顯示圖像的時(shí)鐘
2.時(shí)間與本地時(shí)間一致
2.C語(yǔ)言代碼:
#include<graphics.h> //需要提前安裝庫(kù)函數(shù)EasyX,網(wǎng)上官網(wǎng)下載 #include<stdio.h> #include<stdlib.h> #include<windows.h> #include<conio.h> #include<math.h> #define High 480 #define Width 640//畫布尺寸 #define PI 3.1415 /* ? SYSTEMTIME ti; ?? ?GetLocalTime(&ti);//獲得本地時(shí)間 ? 1. ? ?ti.wYear ? 2.?? ?ti.wMonth ? 3.?? ?ti.wDay ? 4.?? ?ti.wHour ? 5.?? ?ti.wMinute ? 6.?? ?ti.wSecond */ /* ?? ?outtextxy(x,y," ");//輸出文字在(x,y)上 */ int IsEnd;//是否結(jié)束? int center_x,center_y;//中點(diǎn) int second_long;//長(zhǎng)度 int second_x,second_y;//秒針位置 int minute_long;//長(zhǎng)度 int minute_x,minute_y;//分針位置 int hour_long;//長(zhǎng)度 int hour_x,hour_y;//時(shí)針位置 int second_num,minute_num,hour_num;//秒針分針時(shí)針計(jì)數(shù)用的 SYSTEMTIME ti;//定義變量存儲(chǔ)系統(tǒng)時(shí)間 void startup(){?? ?//【數(shù)據(jù)初始化】? ?? ?? ?? ?IsEnd = 0;?? ? ?? ?initgraph(Width,High);//展示畫布 ?? ?center_x=Width/2; ?? ?center_y=High/2;//中心點(diǎn)信息 ?? ?second_long= (Width>High)? High/4:Width/4; ?? ?minute_long=second_long*8/9; ?? ?hour_long=second_long*2/3;//指針長(zhǎng)度信息 ?? ?GetLocalTime(&ti);//獲取本地時(shí)間 ?? ?second_num= ti.wSecond;//秒針信息 ?? ?second_x=center_x+second_long*sin(((2*PI)/60)*second_num);//計(jì)算秒針端點(diǎn)位置 ?? ?second_y=center_y-second_long*cos(((2*PI)/60)*second_num); ?? ?minute_num= ti.wMinute*5+second_num/12;//分針信息 ?? ?minute_x=center_x+minute_long*sin(((2*PI)/(60*5))*minute_num);//計(jì)算分針端點(diǎn)位置 ?? ?minute_y=center_y-minute_long*cos(((2*PI)/(60*5))*minute_num); ?? ?hour_num= ti.wHour%12*5+minute_num/5/12;//時(shí)針信息,按照60刻度算 ?? ?hour_x=center_x+hour_long*sin(((2*PI)/(12*5))*hour_num);//計(jì)算時(shí)針端點(diǎn)位置 ?? ?hour_y=center_y-hour_long*cos(((2*PI)/(12*5))*hour_num); } void show_begin(){//【初始頁(yè)面展示】? ?? ?BeginBatchDraw();//批量繪圖開始 }? void show(){?? ?//【顯示畫面】? ?? ?int i; ?? ? ?? ?setlinestyle(PS_SOLID,2);//設(shè)置線型為實(shí)線,線寬為2 ?? ?setcolor(DARKGRAY);//設(shè)置顏色為暗灰色 ?? ?setfillcolor(DARKGRAY);//設(shè)置填充顏色也為暗灰色 ?? ?fillcircle(center_x,center_y,second_long*21/16);//畫表盤外圓 ?? ?setcolor(LIGHTGRAY);//設(shè)置顏色亮灰色 ?? ?setfillcolor(LIGHTGRAY);//設(shè)置填充顏色為亮灰色 ?? ?fillcircle(center_x,center_y,second_long*6/5);//填充表盤背景 ?? ? ?? ?for(i=0;i<=59;i++){//畫表盤上的刻度 ?? ??? ?setcolor(BLACK); ?? ??? ?if(i%5==0){//每個(gè)小時(shí)點(diǎn)線長(zhǎng)更長(zhǎng) ?? ??? ??? ?line( ?? ??? ??? ?(center_x+second_long*15/14*sin(((2*PI)/60)*i)), ?? ??? ??? ?(center_y-second_long*15/14*cos(((2*PI)/60)*i)), ?? ??? ??? ?(center_x+second_long*6/5*sin(((2*PI)/60)*i)), ?? ??? ??? ?(center_y-second_long*6/5*cos(((2*PI)/60)*i)) ?? ??? ??? ?); ?? ??? ?}else line( ?? ??? ??? ?(center_x+second_long*8/7*sin(((2*PI)/60)*i)), ?? ??? ??? ?(center_y-second_long*8/7*cos(((2*PI)/60)*i)), ?? ??? ??? ?(center_x+second_long*6/5*sin(((2*PI)/60)*i)), ?? ??? ??? ?(center_y-second_long*6/5*cos(((2*PI)/60)*i)) ?? ??? ??? ?); ?? ?} ?? ?setbkcolor(LIGHTGRAY);//設(shè)置背景顏色亮灰色 ?? ?settextcolor(WHITE);//設(shè)置顏色白色 ?? ?outtextxy(center_x-second_long/4,center_y+second_long/2,"你最珍貴");//輸出文字 ?? ?setbkcolor(BLACK);//背景顏色設(shè)置回黑色!!!不要忘記 ?? ? ?? ?setlinecolor(YELLOW);//設(shè)置線條顏色黃色 ?? ?setlinestyle(PS_SOLID,3);//設(shè)置線條風(fēng)格為實(shí)線,線寬為3 ?? ?line(center_x,center_y,second_x,second_y);//畫秒針 ?? ?setlinecolor(BLUE);//設(shè)置顏色 ?? ?setlinestyle(PS_SOLID,4);//設(shè)置線條風(fēng)格實(shí)線,線寬為4 ?? ?line(center_x,center_y,minute_x,minute_y);//畫分針 ?? ?setlinecolor(RED);//設(shè)置顏色 ?? ?setlinestyle(PS_SOLID,6);//設(shè)置線條風(fēng)格實(shí)線,線寬3 ?? ?line(center_x,center_y,hour_x,hour_y);//畫時(shí)針 ?? ?FlushBatchDraw();//更新一次畫面,解決畫面閃的問題,需要配合BeginBatchDraw函數(shù)使用 ?? ?Sleep(1000);//延時(shí) ?? ?cleardevice();//清除之前的畫跡 ?? ? ?? ? } void update_outinput(){?? ?//【與輸入無關(guān)的更新】? ?? ?GetLocalTime(&ti);//獲取本地時(shí)間 ?? ?second_num= ti.wSecond;//秒針信息 ?? ?second_x=center_x+second_long*sin(((2*PI)/60)*second_num);//計(jì)算秒針端點(diǎn)位置 ?? ?second_y=center_y-second_long*cos(((2*PI)/60)*second_num); ?? ?minute_num= ti.wMinute*5+second_num/12;//分針信息 ?? ?minute_x=center_x+minute_long*sin(((2*PI)/(60*5))*minute_num);//計(jì)算分針端點(diǎn)位置 ?? ?minute_y=center_y-minute_long*cos(((2*PI)/(60*5))*minute_num); ?? ?hour_num= ti.wHour%12*5+minute_num/5/12;//時(shí)針信息,按照60刻度算 ?? ?hour_x=center_x+hour_long*sin(((2*PI)/(12*5))*hour_num);//計(jì)算時(shí)針端點(diǎn)位置 ?? ?hour_y=center_y-hour_long*cos(((2*PI)/(12*5))*hour_num); } void update_input(){//【與輸入有關(guān)的更新】? ?? ?char input; ?? ?if(kbhit()){ ?? ??? ?input = getch(); ?? ??? ??? ??? ? ?? ?} } void show_end(){//【顯示失敗界面】? ?? ?EndBatchDraw(); } int main(){ ?? ?startup();?? ?//數(shù)據(jù)初始化 ?? ?show_begin();//初始頁(yè)面? ?? ?while(!IsEnd){?? ?//游戲循環(huán)執(zhí)行? ?? ??? ?show();?? ?// 顯示畫面? ?? ??? ?update_outinput();?? ?//與輸入無關(guān)的更新? ?? ??? ?update_input();?? ?//與輸入有關(guān)的更新? ?? ?} ?? ?show_end(); //顯示失敗界面? ?? ?return 0; }
3.運(yùn)行結(jié)果:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++實(shí)現(xiàn)對(duì)輸入數(shù)字組進(jìn)行排序
這里給大家介紹的是通過某個(gè)方法實(shí)現(xiàn)判斷命令行中輸入的數(shù)字是幾個(gè),這樣再用冒泡法排序的時(shí)候就不用擔(dān)心輸入的是幾個(gè)數(shù)字,用到的知識(shí)主要是冒泡法排序2015-11-11解讀C++編程中派生類的構(gòu)成和創(chuàng)建
這篇文章主要介紹了解讀C++編程中派生類的構(gòu)成和創(chuàng)建,是C++入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-09-09淺談C++中對(duì)象的復(fù)制與對(duì)象之間的相互賦值
這篇文章主要介紹了淺談C++中對(duì)象的復(fù)制與對(duì)象之間的相互賦值,是C語(yǔ)言入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-09-09深入理解c++實(shí)現(xiàn)Qt信號(hào)和槽機(jī)制
信號(hào)和槽機(jī)制是 Qt 的核心機(jī)制,可以讓編程人員將互不相關(guān)的對(duì)象綁定在一起,實(shí)現(xiàn)對(duì)象之間的通信,本文就來深入理解c++實(shí)現(xiàn)Qt信號(hào)和槽機(jī)制,感興趣的可以了解一下2023-08-08C語(yǔ)言實(shí)現(xiàn)循環(huán)打印星號(hào)圖形再鏤空
這篇文章主要介紹了C語(yǔ)言實(shí)現(xiàn)循環(huán)打印星號(hào)圖形再鏤空,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11