源码分析:
#include "Include.h"
/*============================================================================*/
void SimulationClock( HDC hdc,int x,int y,int dx,int dy,int bk_c,
int hour_c,int min_c,int sec_c,
int Hour,int Min,int Sec)
{
#define PI 3.1415926
int radius; //表盘半径
int s_Len; //秒针长度
int m_Len; //分针长度
int h_Len; //时针长度
int cx,cy; //表盘中点
int i,x0,y0,x1,y1;
////
Hour%=12;
radius =MIN((dx>>1),(dy>>1)); //计算表盘半径
s_Len =(radius*7)>>3; //设置秒针长度为7/8半径
m_Len =(radius*6)>>3; //设置分针长度为6/8半径
h_Len =(radius*5)>>3; //设置时针长度为5/8半径
cx =x+(dx>>1); //计算x中点
cy =y+(dy>>1); //计算y中点
////
if(bk_c!=RGB_TRANS) //如果背景非透明色
{
FillRectangle(hdc,x,y,dx,dy,bk_c); //填充背景
for(i=0;i<360;i+=6) //以6度递增绘制分钟和秒钟刻度
{
if((i%30)==0) //以30度递增绘制小时刻度
{
//这里绘制小时刻度
//计算刻度两点坐标,刻度长度为表盘半径的(1/16)-4
x0 = (int)(cx + sin(i*PI/180)*(radius-2));
y0 = (int)(cy - cos(i*PI/180)*(radius-2));
x1 = (int)(cx + sin(i*PI/180)*(radius-(radius>>4)-4));
y1 = (int)(cy - cos(i*PI/180)*(radius-(radius>>4)-4));
LineWith(hdc,x0,y0,x1,y1,3,RGB(20,20,20)); //用直线连接两点坐标
}
else
{ //这里绘制分钟和秒钟刻度,距表盘中点为:(半径-4)
x0 = (int)(cx + sin(i*PI/180)*(radius-4));
y0 = (int)(cy - cos(i*PI/180)*(radius-4));
PutPixel(hdc,x0,y0,RGB(20,20,20));
}
}
}
////
//绘制时针,时针角度=Hour*30+30*Min/60
x0 = (int)(cx + sin((Hour*30+Min/2)*PI/180)*h_Len);
y0 = (int)(cy - cos((Hour*30+Min/2)*PI/180)*h_Len);
x1 = (int)(cx + sin(((Hour+6)*30+Min/2)*PI/180)*((h_Len>>3)+2));
y1 = (int)(cy - cos(((Hour+6)*30+Min/2)*PI/180)*((h_Len>>3)+2));
LineWith(hdc,cx,cy,x0,y0,3,hour_c);
LineWith(hdc,cx,cy,x1,y1,3,hour_c);
//绘制分针,分针角度=Min*6
x0 =(int)(cx + sin((Min*6)*PI/180)*m_Len);
y0 =(int)(cy - cos((Min*6)*PI/180)*m_Len);
x1 =(int)(cx + sin(((Min+30)*6)*PI/180)*((m_Len>>3)+2));
y1 =(int)(cy - cos(((Min+30)*6)*PI/180)*((m_Len>>3)+2));
LineWith(hdc,cx,cy,x0,y0,3,min_c);
LineWith(hdc,cx,cy,x1,y1,3,min_c);
//绘制秒针,秒针角度=Sec*6
x0 =(int)(cx + sin((Sec*6)*PI/180)*s_Len);
y0 =(int)(cy - cos((Sec*6)*PI/180)*s_Len);
x1 =(int)(cx + sin(((Sec+30)*6)*PI/180)*((s_Len>>3)+2));
y1 =(int)(cy - cos(((Sec+30)*6)*PI/180)*((s_Len>>3)+2));
Line(hdc,cx,cy,x0,y0,sec_c);
Line(hdc,cx,cy,x1,y1,sec_c);
FillCircle(hdc,cx,cy,2,sec_c); //绘制秒针修饰点
}
/*============================================================================*/
/*============================================================================*/
static RESULT WinRealTimeProc(HWND hwnd,U32 msg,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
char buf[16];
RECT rc;
COORD x,y;
RTC_TIME time;
////
switch(msg)
{
case MSG_CREATE: //主窗口创建
break;
////
case MSG_TIMER://定时器消息
InvalidateRect(hwnd,0,0); //刷新窗口
break;
////
case MSG_PAINT: //绘制窗口
hdc=BeginPaint(hwnd);
RTC_GetTime(&time);
SimulationClock(hdc,0,0,WindowClientWidth(hwnd),WindowClientHeight(hwnd),
GetWindowBkColor(hwnd),RGB(20,20,20),RGB(20,20,20),RGB(200,0,0),
time.Hour,time.Min,time.Sec);
EndPaint(hwnd,hdc);
break;
////
default: DefaultWindowProc(hwnd,msg,wParam,lParam); //其它消息交给系统处理
}
return MSG_NULL;
}
/*============================================================================*/
void WinRealTime(void)
{
HWND hwnd;
MSG msg;
HANDLE hTimer0;
////
创建主窗口
hwnd =CreateMainWindow( 300,64,160,180, //位置:300,64,尺寸:160x180
RGB(96,160,96), //窗口背景色
WS_CAPTION|WS_VISIBLE|WS_BORDER_DUAL,0, //窗口风格:带标题栏,窗口可见,双边框
WinRealTimeProc, //窗口过程
"模拟时钟"); // 窗口标题
hTimer0=SetTimer(hwnd,0x3000,200,NULL); //设置定时器(ID=0x3000,时间:300ms,无回调函数)
ShowWindow(hwnd,SW_SHOW); //显示窗口
while(GetMessage(&msg,hwnd)) //进入消息循环
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
KillTimer(hTimer0); //销毁定时器
DestroyMainWindow(hwnd); //销毁主窗口
//ExitThisThread(); //退出线程
}
/*============================================================================*/
/*============================================================================*/ |