時鐘、背景音樂、背景圖片

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();
}


exe程序下載:http://download.csdn.net/detail/guangxyou/5328184

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章