全程代碼小編就在這里發(fā)出來了哈奠货,最終的效果圖窘奏,
// c語言+圖形編程 電腦時(shí)鐘
//包含頭文件
#include
#include
#include
void Draw_Dial();//繪制靜態(tài)的表盤
void Draw_Hand(int hour, int minute, int secend); //繪制表針
//主函數(shù)
int main()
{
initgraph(640, 480);//初始化一個(gè)圖形界面 640*480
//circle(320,240,150);//畫圓的函數(shù)
//rectangle(0, 0, 50, 50);//畫正方體的函數(shù)
Draw_Dial();
setwritemode(R2_XORPEN);//設(shè)置XOR繪圖模式
SYSTEMTIME time;//定義一個(gè)時(shí)間結(jié)構(gòu)體 用來保存當(dāng)前的時(shí)間
while (!_kbhit())
{
GetLocalTime(&time);//獲取當(dāng)前的時(shí)間
Draw_Hand(time.wHour,time.wMinute,time.wSecond); //把表針繪制出來
Sleep(1000);//間隔1秒 1000毫秒
Draw_Hand(time.wHour, time.wMinute, time.wSecond); //把表針繪制出來
}
closegraph();//關(guān)閉圖形界面
return 0;
}
//繪制靜態(tài)的表盤
void Draw_Dial()
{
//繪制邊界
setcolor(GREEN);
circle(320, 240, 160);//畫圓函數(shù)的參數(shù) 320 240坐標(biāo) 160是圓的半徑大小
circle(320, 240, 60);
circle(320, 240, 2);
//圖形界面的輸出文本信息 txt text 文字
outtextxy(260,420,L"GOOD GOOD STUDY");
outtextxy(283, 440, L"DAY DAY UP");
//line(0, 0, 639, 479);
//繪制刻度
//circle(320, 85, 2);
setcolor(WHITE);
int x, y;
for (int i = 0; i
{
x = 320 + int(145 * sin(PI * 2 * i / 60));
y = 240 + int(145 * cos(PI * 2 * i / 60));
if (i % 15 == 0)
bar(x - 5, y - 5, x + 5, y + 5);
else if (i % 5 == 0)
circle(x, y, 3);
else
putpixel(x, y, WHITE); //點(diǎn)
}
}
//繪制表針
void Draw_Hand(int hour, int minute, int second)
{
double h_hour, h_minute, h_second; //三個(gè)弧度值
int x_hour, y_hour, x_minute, y_minute, x_second, y_second; //末端位置
//計(jì)算弧度值
h_second = second * 2 * PI / 60;
h_minute = minute * 2 * PI / 60 + h_second / 60;
h_hour = hour * 2 * PI / 12 + h_minute / 12;
//計(jì)算末端位置
x_second = int(120 * sin(h_second)); y_second=int(120 * cos(h_second));
x_minute = int(100 * sin(h_minute)); y_minute = int(100 * cos(h_minute));
x_hour = int(70 * sin(h_hour)); y_hour = int(70 * cos(h_hour));
//繪制秒針
setlinestyle(PS_SOLID, 2); //PS_SOILD 實(shí)線 2像素 線條的粗細(xì)
setcolor(RED);
line(320 + x_second, 240 - y_second, 320 - x_second / 3, 240 + y_second / 3);
//繪制分針
setlinestyle(PS_SOLID, 6); //PS_SOILD 實(shí)線 2像素 線條的粗細(xì)
setcolor(YELLOW);
line(320 + x_minute, 240 - y_minute, 320 - x_minute / 5, 240 + y_minute/5);
//繪制時(shí)針
setlinestyle(PS_SOLID, 7); //PS_SOILD 實(shí)線 2像素 線條的粗細(xì)
setcolor(GREEN);
line(320 + x_hour, 240 - y_hour, 320 - x_hour / 5, 240 + y_hour/5);
}
演示一番 是不是萄凤?减俏??逼格不高,這只怕是最簡易的一個(gè)時(shí)鐘了吧爽撒。對了蕊唐。注意一下!<蚴檬某!記得沒安裝圖形庫的,記得要去安裝喲螟蝙。恢恼。。胰默。