EasyX很有趣,参考EasyX官网的文章,花了几个小时做了一个时钟程序,包含背景音乐、背景图片,时钟是模仿Iphone时钟设计的,可惜不像。系数调整的比较粗略,表针走的不是非常精确,另外以后再加上个程序图标。
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <math.h>
#pragma comment(lib, "Winmm.lib") // 引用 Windows Multimedia API
#define PI 3.1415926
void Drawdail(void)
{
//大圆
setfillcolor(WHITE);
fillcircle(250, 235, 160);
//小圆
setlinecolor(0x231f20);
setlinestyle(PS_SOLID, NULL, 3);
fillcircle(250, 235, 10);
int x, y;
for (int i=1; i<=60; i++)
{
x = 250 + int(145 * sin(PI * 2 * i / 60));
y = 250 - int(145 * cos(PI * 2 * i / 60));
if (i % 5 == 0)
{
char c[2];
sprintf(c, "%d", i/5);
settextcolor(0x231f20);
setbkmode(TRANSPARENT);
setfont(25, 0, "Calibri");
outtextxy(x-10, y-28, c);
}
}
}
void DrawHand(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(145 * sin(a_sec)); y_sec = int(145 * cos(a_sec));
x_min = int(100 * sin(a_min)); y_min = int(100 * cos(a_min));
x_hour= int(80 * sin(a_hour)); y_hour= int(80 * cos(a_hour));
// 画时针
setlinestyle(PS_SOLID, NULL, 8);
setcolor(BLUE);
line(250, 235, 250 + x_hour, 250 - y_hour);
// 画分针
setlinestyle(PS_SOLID, NULL, 5);
setcolor(YELLOW);
line(250, 235, 250 + x_min, 250 - y_min);
// 画秒针
setlinestyle(PS_SOLID, NULL, 2);
setcolor(LIGHTRED);
line(250, 235, 250 + x_sec, 250 - y_sec);
}
//
// 提取指定模块中的资源文件
// 参数:
// strDstFile: 目标文件名。提取的资源将保存在这里;
// strResType: 资源类型;
// strResName: 资源名称;
// 返回值:
// true: 执行成功;
// false: 执行失败。
//
bool ExtractResource(LPCTSTR strDstFile, LPCTSTR strResType, LPCTSTR strResName)
{
// 创建文件
HANDLE hFile = ::CreateFile(strDstFile, GENERIC_WRITE, NULL, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY, NULL);
if (hFile == INVALID_HANDLE_VALUE)
return false;
// 查找资源文件中、加载资源到内存、得到资源大小
HRSRC hRes = ::FindResource(NULL, strResName, strResType);
HGLOBAL hMem = ::LoadResource(NULL, hRes);
DWORD dwSize = ::SizeofResource(NULL, hRes);
// 写入文件
DWORD dwWrite = 0; // 返回写入字节
::WriteFile(hFile, hMem, dwSize, &dwWrite, NULL);
::CloseHandle(hFile);
return true;
}
void main(void)
{
initgraph(500, 470);
//play music;
TCHAR tmpmp3[_MAX_PATH]; // 产生临时文件的文件名
::GetTempPath(_MAX_PATH, tmpmp3);
_tcscat(tmpmp3, _T("happy_temp.mp3"));
ExtractResource(tmpmp3, _T("MP3"), _T("happy.mp3")); // 将 MP3 资源提取为临时文件
TCHAR mcicmd[300];
_stprintf(mcicmd, _T("open \"%s\" alias music"), tmpmp3);
mciSendString(mcicmd, NULL, 0, NULL);
mciSendString(_T("play music repeat"), NULL, 0, NULL);
//loadimage(NULL, "image", "image", 500, 470,true);
//system("pause");
//clearrectangle(0, 0, 500, 470);
loadimage(NULL, "backgroud", "bak", 500, 470, true);
//system("pause");
setcolor(0x231f20);
Drawdail();
setwritemode(R2_XORPEN);
SYSTEMTIME T;
while(!kbhit())
{
GetLocalTime(&T);
DrawHand(T.wHour, T.wMinute, T.wSecond);
Sleep(1000);
DrawHand(T.wHour, T.wMinute, T.wSecond);
}
// 停止播放并关闭音乐
mciSendString(_T("stop mymusic"), NULL, 0, NULL);
mciSendString(_T("close mymusic"), NULL, 0, NULL);
// 删除临时文件
DeleteFile(tmpmp3);
closegraph();
}