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//畫(huà)布尺寸
#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;//秒針?lè)轴槙r(shí)針計(jì)數(shù)用的
SYSTEMTIME ti;//定義變量存儲(chǔ)系統(tǒng)時(shí)間
void startup(){?? ?//【數(shù)據(jù)初始化】?
?? ??
?? ?IsEnd = 0;?? ?
?? ?initgraph(Width,High);//展示畫(huà)布
?? ?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();//批量繪圖開(kāi)始
}?
void show(){?? ?//【顯示畫(huà)面】?
?? ?int i;
?? ?
?? ?setlinestyle(PS_SOLID,2);//設(shè)置線(xiàn)型為實(shí)線(xiàn),線(xiàn)寬為2
?? ?setcolor(DARKGRAY);//設(shè)置顏色為暗灰色
?? ?setfillcolor(DARKGRAY);//設(shè)置填充顏色也為暗灰色
?? ?fillcircle(center_x,center_y,second_long*21/16);//畫(huà)表盤(pán)外圓
?? ?setcolor(LIGHTGRAY);//設(shè)置顏色亮灰色
?? ?setfillcolor(LIGHTGRAY);//設(shè)置填充顏色為亮灰色
?? ?fillcircle(center_x,center_y,second_long*6/5);//填充表盤(pán)背景
?? ?
?? ?for(i=0;i<=59;i++){//畫(huà)表盤(pán)上的刻度
?? ??? ?setcolor(BLACK);
?? ??? ?if(i%5==0){//每個(gè)小時(shí)點(diǎn)線(xià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è)置線(xiàn)條顏色黃色
?? ?setlinestyle(PS_SOLID,3);//設(shè)置線(xiàn)條風(fēng)格為實(shí)線(xiàn),線(xiàn)寬為3
?? ?line(center_x,center_y,second_x,second_y);//畫(huà)秒針
?? ?setlinecolor(BLUE);//設(shè)置顏色
?? ?setlinestyle(PS_SOLID,4);//設(shè)置線(xiàn)條風(fēng)格實(shí)線(xiàn),線(xiàn)寬為4
?? ?line(center_x,center_y,minute_x,minute_y);//畫(huà)分針
?? ?setlinecolor(RED);//設(shè)置顏色
?? ?setlinestyle(PS_SOLID,6);//設(shè)置線(xiàn)條風(fēng)格實(shí)線(xiàn),線(xiàn)寬3
?? ?line(center_x,center_y,hour_x,hour_y);//畫(huà)時(shí)針
?? ?FlushBatchDraw();//更新一次畫(huà)面,解決畫(huà)面閃的問(wèn)題,需要配合BeginBatchDraw函數(shù)使用
?? ?Sleep(1000);//延時(shí)
?? ?cleardevice();//清除之前的畫(huà)跡
?? ?
?? ?
}
void update_outinput(){?? ?//【與輸入無(wú)關(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();?? ?// 顯示畫(huà)面?
?? ??? ?update_outinput();?? ?//與輸入無(wú)關(guān)的更新?
?? ??? ?update_input();?? ?//與輸入有關(guān)的更新?
?? ?}
?? ?show_end(); //顯示失敗界面?
?? ?return 0;
}3.運(yùn)行結(jié)果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語(yǔ)言 動(dòng)態(tài)分配數(shù)組案例詳解
這篇文章主要介紹了C語(yǔ)言 動(dòng)態(tài)分配數(shù)組案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
c++使用regex報(bào)錯(cuò)regex_error兩種解決方案
C++正則表達(dá)式是一個(gè)非常強(qiáng)大和實(shí)用的工具,但是使用它們時(shí)需要注意仔細(xì)檢查代碼是否符合語(yǔ)法規(guī)則,這篇文章主要給大家介紹了關(guān)于c++使用regex報(bào)錯(cuò)regex_error的兩種解決方案,需要的朋友可以參考下2024-03-03
聊聊Qt+OpenCV聯(lián)合開(kāi)發(fā)之圖像的創(chuàng)建與賦值問(wèn)題
這篇文章主要介紹了Qt+OpenCV聯(lián)合開(kāi)發(fā)之圖像的創(chuàng)建與賦值問(wèn)題,給大家介紹了圖像的克隆及拷貝問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01
關(guān)于win32 gettimeofday替代方案
下面小編就為大家?guī)?lái)一篇關(guān)于win32 gettimeofday替代方案。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12
C語(yǔ)言實(shí)現(xiàn)五子棋人人對(duì)戰(zhàn)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)五子棋人人對(duì)戰(zhàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12
C語(yǔ)言手寫(xiě)多級(jí)時(shí)間輪定時(shí)器
這篇文章主要為大家詳細(xì)介紹了如何利用C語(yǔ)言實(shí)現(xiàn)手寫(xiě)多級(jí)時(shí)間輪定時(shí)器,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下2022-09-09

