先按照自己的思路写了一个程序,写得比较匆忙,难免有不足之处,希望大家批评指正。
#include <WINDOWS.H> #include <MATH.H> #define RCLOCK 150 #define RHOUR 8 #define RMINUTE 3 #define RCENTER 3 #define RMHAND 30 #define RHHAND 40 #define HTHETA 22 #define MTHETA 15 #define PI 3.1415926 #define TIMEID 1 void ShowSecond(WORD s,HDC hdc); void ShowMinute(WORD m,HDC hdc); void ShowHour(WORD h,WORD m,HDC hdc); LRESULT CALLBACK MainWndProc( HWND hwnd, // handle to window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM lParam // second message parameter ); int WINAPI WinMain( HINSTANCE hInstance, // handle to current instance HINSTANCE hPrevInstance, // handle to previous instance LPSTR lpCmdLine, // command line int nCmdShow // show state ) { TCHAR szAppName[]=TEXT("ACLOCK 1"); WNDCLASS wndcls; wndcls.cbClsExtra=0; wndcls.cbWndExtra=0; wndcls.hCursor=LoadCursor(0,IDC_ARROW); wndcls.hIcon=LoadIcon(0,IDI_APPLICATION); wndcls.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH); wndcls.hInstance=hInstance; wndcls.lpfnWndProc=MainWndProc; wndcls.lpszClassName=szAppName; wndcls.lpszMenuName=NULL; wndcls.style=CS_VREDRAW|CS_HREDRAW; RegisterClass(&wndcls); HWND hwnd; hwnd=CreateWindow(szAppName,TEXT("模拟时钟"),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL); ShowWindow(hwnd,nCmdShow); UpdateWindow(hwnd); MSG msg; while (GetMessage(&msg,0,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return 0; } LRESULT CALLBACK MainWndProc( HWND hwnd, // handle to window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM lParam // second message parameter ) { PAINTSTRUCT ps; HDC hdc; SYSTEMTIME st; int i,rx,ry; static int cxClient,cyClient; HBRUSH brush,oldBrush; HPEN pen,oldPen; switch (uMsg) { case WM_CREATE: SetTimer(hwnd,1,1000,NULL); return 0; case WM_SIZE: cxClient=LOWORD(lParam); cyClient=HIWORD(lParam); return 0; case WM_TIMER: InvalidateRect(hwnd,NULL,TRUE); return 0; case WM_PAINT: hdc=BeginPaint(hwnd,&ps); SetMapMode(hdc,MM_ISOTROPIC); SetWindowExtEx(hdc,330,-330,NULL); SetViewportExtEx(hdc,cxClient,cyClient,NULL); SetViewportOrgEx(hdc,cxClient/2,cyClient/2,NULL); brush=CreateSolidBrush(RGB(0,0,0)); oldBrush=(HBRUSH)SelectObject(hdc,brush); Ellipse(hdc,RCENTER,RCENTER,-RCENTER,-RCENTER); SelectObject(hdc,oldBrush); for (i=0;i<60;i++) { rx=RCLOCK*cos(i*6*PI/180); ry=RCLOCK*sin(i*6*PI/180); brush=CreateSolidBrush(RGB(0,0,0)); oldBrush=(HBRUSH)SelectObject(hdc,brush); if (0==(i%5)) { Ellipse(hdc,rx-RHOUR,ry-RHOUR,rx+RHOUR,ry+RHOUR); } else { Ellipse(hdc,rx-RMINUTE,ry-RMINUTE,rx+RMINUTE,ry+RMINUTE); } } SelectObject(hdc,oldBrush); GetLocalTime(&st); ShowSecond(st.wSecond,hdc); ShowMinute(st.wMinute,hdc); ShowHour(st.wHour,st.wMinute,hdc); EndPaint(hwnd,&ps); return 0; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd,uMsg,wParam,lParam); } void ShowSecond(WORD s,HDC hdc) { int rx,ry; rx=RCLOCK*cos((90-s*6)*PI/180); ry=RCLOCK*sin((90-s*6)*PI/180); if (rx>0) { rx-=15; } else { rx+=15; } if (ry>0) { ry-=15; } else { ry+=15; } MoveToEx(hdc,0,0,NULL); LineTo(hdc,rx,ry); } void ShowMinute(WORD m,HDC hdc) { POINT pt[5]; pt[0].x=0; pt[0].y=0; pt[4].x=0; pt[4].y=0; pt[2].x=RCLOCK*cos((90-m*6)*PI/180); pt[2].y=RCLOCK*sin((90-m*6)*PI/180); if (pt[2].x>0) { pt[2].x-=10; } else { pt[2].x+=10; } if (pt[2].y>0) { pt[2].y-=10; } else { pt[2].y+=10; } pt[1].x=RHHAND*cos(((90-m*6)+MTHETA)*PI/180); pt[1].y=RHHAND*sin(((90-m*6)+MTHETA)*PI/180); pt[3].x=RHHAND*cos(((90-m*6)-MTHETA)*PI/180); pt[3].y=RHHAND*sin(((90-m*6)-MTHETA)*PI/180); Polyline(hdc,pt,5); } void ShowHour(WORD h,WORD m,HDC hdc) { POINT pt[5]; if (h>12) { h-=12; } pt[0].x=0; pt[0].y=0; pt[4].x=0; pt[4].y=0; pt[2].x=RCLOCK*cos((90-m*0.5-30*h)*PI/180); pt[2].y=RCLOCK*sin((90-m*0.5-30*h)*PI/180); if (pt[2].x>0) { pt[2].x-=20; } else { pt[2].x+=20; } if (pt[2].y>0) { pt[2].y-=20; } else { pt[2].y+=20; } pt[1].x=RHHAND*cos(((90-m*0.5-30*h)+HTHETA)*PI/180); pt[1].y=RHHAND*sin(((90-m*0.5-30*h)+HTHETA)*PI/180); pt[3].x=RHHAND*cos(((90-m*0.5-30*h)-HTHETA)*PI/180); pt[3].y=RHHAND*sin(((90-m*0.5-30*h)-HTHETA)*PI/180); Polyline(hdc,pt,5); }
<span style="font-family: Arial, Helvetica, sans-serif;">也懒得加注释了,主要是为了以后回过头来看一看。与作者写的思路上的差距还是比较大的,效率上差距好像更大,哈哈,有待于提高啊!</span>