用到編譯工具 是vs2013(VC Vs2015也行)記得安裝圖形庫
涉及到的知識點:
1.圖形庫 界面 DONE
2.鼠標操作
3.時間 DONE
4.音樂DONE
5.計算問題 角度 DONE
源碼如下:
#include<graphics.h>//圖形庫 需要安裝 VS VC /<graphics.h>
#include<stdio.h>
#include<time.h> //time clock/<time.h>
#include<math.h>//計算/<math.h>
#include<stdlib.h>//關機命令/<stdlib.h>
//playsound 只能放wav 可以用於資源文件
//mciSendSring 可以放mp3
#include<mmsystem.h>
#pragma comment(lib,"WINMM.LIB")//播放多媒體的一個庫
#define PI 3.141592654
void draw(int,int,int);
int main()
{
initgraph(640, 480);//新建窗口
//錶盤界面
//1.獲取時間
SYSTEMTIME time;//結構體
//char arr[50];
setbkmode(TRANSPARENT);
settextstyle(30,0,"華文彩雲");//高度和默認寬度
//setbkcolor(LIGHTGRAY);//grey gray
while (1)
{
loadimage(NULL, "1.jpg");
setlinecolor(WHITE);
rectangle(480, 0, 640, 160);
outtextxy(500, 70, "放音樂");
rectangle(480, 160, 640, 320);
outtextxy(500, 230, "不要點");
rectangle(480, 320, 640, 480);
outtextxy(500, 380, "退出");
//界面
GetLocalTime(&time);//獲取當前時間 年 月 日 小時 分 秒
draw(time.wHour, time.wMinute, time.wSecond);
Sleep(1000);
}
//while (1)
//{
//Sleep(1000);//單位毫秒
//GetLocalTime(&time);//獲取當前時間 年 月 日 小時 分 秒
//sprintf(arr, "%d年%d月%d日%d小時%d分%d秒", time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond);//格式化寫入
//outtextxy(0, 0, arr);//圖形庫的輸出函數 printf
////
//}
getchar();
closegraph();//關閉窗口
return 0;
}
void draw(int hour,int min,int sec)
{
//畫表盤
setlinecolor(WHITE);
setlinestyle(PS_SOLID, 1);
circle(240,240,200);//圓心加半徑 直徑<480 半徑<240
//圓心座標 200 是半徑
char arr[3];
for (int i = 1; i <= 12; i++)//刻度
{
sprintf(arr, "%d", i);
outtextxy(240+(int)(200*sin(30*i*PI/180))-5, 240-(int)(200 * cos(30 * i*PI / 180))-5, arr);
}
//強轉
//開始畫指針 時針 分針 秒針 時針最短120 分針160 秒針最長200
//粗細 顏色
//畫時針
setlinecolor(RED);
setlinestyle(PS_SOLID,7);
line(240, 240, 240 + (int)(120 * sin(30 * (hour+min/60.0)*PI / 180)), 240 - (int)(120 * cos(30 * (hour+min/60.0)*PI / 180)));
//畫分針
setlinecolor(BLUE);
setlinestyle(PS_SOLID, 6);
line(240, 240, 240 + (int)(160 * sin(6 * min*PI / 180)), 240 - (int)(160 * cos(6* min*PI / 180)));
//畫秒針
setlinecolor(YELLOW);
setlinestyle(PS_SOLID, 2);
line(240, 240, 240 + (int)(200 * sin(6 * sec*PI / 180)), 240 - (int)(200 * cos(6 * sec*PI / 180)));
}
void play()
{
if (MouseHit())//判斷有沒有鼠標信息
{
MOUSEMSG msg = GetMouseMsg();//鼠標信息
switch (msg.uMsg)
{
case WM_LBUTTONDOWN:
if (msg.x >= 480)
{
if (msg.y <= 160)
{
//放音樂
mciSendString("open Fade.mp3 alias bgm", 0, 0, 0);//
mciSendString("play bgm repeat", 0, 0, 0);
}
else if (msg.y > 320)
{
//結束 整個程序
//return ? break?
closegraph();
exit(0);
}
else
{
//關機
MessageBox(GetHWnd(), "將在60秒後關機", "關機", MB_OK);//句柄 對應一個窗口
system("shutdown -s -t 60");//關機和取消關機
//shutdown -s -t 60
//shutdown -s 立刻關機
//23:30關機
//at 23:30 shutdown -s
//shutdown -a 取消關機
}
}
}
}
}
最終的效果圖:小編的設計感不強,配色,字體也不是很美觀,但是做出來的。可以加下小編的群466572167,群內有視頻資料學習交流
小編就暫時說到這,也有好看的背景可以放到時針後面的,只是要配上好圖,有個美觀才好看,但是最終的話我們是做項目的,程序出來才是最重要的。
閱讀更多 java架構社區 的文章