網上的高手都說要學編程一定不能不學底層,俺也學學。
#include <windows.h>
#include <math.h>
#define PI 3.1415926535
#define IDT_TM1 1
struct TIME_POINT{
int x;
int y;
char s[2];
};//這是時鐘盤面上的時刻點結構
LRESULT CALLBACK WindProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd );
bool init_WndClass(HINSTANCE hInstance);
bool init_WINDOW(HINSTANCE hInstance,int nShowCmd);
HWND _hwnd;
WNDCLASS _windclass;
MSG _msg;
TCHAR _clsname[]="Sunjoy的類";
bool init_WndClass(HINSTANCE hInstance){
_windclass.style = CS_HREDRAW | CS_VREDRAW ;
_windclass.hInstance=hInstance;
_windclass.lpfnWndProc=WindProc;
_windclass.cbClsExtra=0;
_windclass.cbWndExtra=0;
_windclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
_windclass.hCursor=LoadCursor(NULL,IDC_ARROW);
_windclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
_windclass.lpszClassName=_clsname;
_windclass.lpszMenuName=NULL;
return (RegisterClass(&_windclass)!=NULL);
}
bool init_WINDOW(HINSTANCE hInstance,int nShowCmd){
_hwnd=CreateWindow(_clsname,
"簡易時鐘",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
450,
450,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(_hwnd,nShowCmd);
return (UpdateWindow(_hwnd)!=NULL);
}
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd )
{
if(!init_WndClass(hInstance) || !init_WINDOW(hInstance,nShowCmd)){
MessageBox(NULL,"建立視窗出錯",NULL,NULL);
}
while(GetMessage(&_msg,NULL,0,0)){
TranslateMessage ( &_msg);
DispatchMessage(&_msg);
}
return _msg.wParam;
}
LRESULT CALLBACK WindProc(HWND _hwnd,
UINT _message,
WPARAM _wparam,
LPARAM _lparasm)
{
HDC hdc;
HPEN hpen;
HBRUSH hbrush;
PAINTSTRUCT ps;
int cx=220; //圓心x座標
int cy=215; //圓心y座標
int r=200; //圓的半徑
TIME_POINT tmp[12]; //時刻
int i=0;
SYSTEMTIME systm; //系統時間
for(i=0;i<12;i++){
tmp[i].x=(int)(cx+(r-10)*sin(i*PI/6));
tmp[i].y=(int)(cy-(r-10)*cos(i*PI/6));
wsprintf(tmp[i].s,"%d",i);
}
strcpy(tmp[0].s,"12"); //初始化時鐘盤面座標
switch(_message){
case WM_CREATE:
SetTimer (_hwnd, IDT_TM1, 1000, NULL) ;
return 0;
case WM_PAINT:
hdc=BeginPaint(_hwnd,&ps);
hpen=CreatePen(PS_SOLID,2,RGB(0,0,0));
SelectObject(hdc,hpen);
MoveToEx(hdc,cx,cy,NULL);
Arc(hdc,cx-r,cy-r,r+cx,r+cy,cx+r,cy,cx+r,cy); //畫時鐘圓盤
for(i=0;i<12;i++){
TextOut(hdc,tmp[i].x-5,tmp[i].y-8,tmp[i].s,strlen(tmp[i].s));
}//初始化時鐘盤面
DeleteObject(hpen);
//刷白
hbrush=(HBRUSH)GetStockObject(WHITE_BRUSH);
hpen=CreatePen(PS_SOLID,1,RGB(255,255,255));
SelectObject(hdc,hbrush);
SelectObject(hdc,hpen);
Pie(hdc,cx-r+19,cy-r+19,cx+r-19,cy+r-19,cx+r-19,cy,cx+r-19,cy);
MoveToEx(hdc,cx,cy,NULL);
DeleteObject(hbrush);
DeleteObject(hpen);
//擷取本地時間
GetLocalTime(&systm);
//畫秒針
hpen=CreatePen(PS_SOLID,2,RGB(0,0,255));
SelectObject(hdc,hpen);
MoveToEx(hdc,cx,cy,NULL);
LineTo(hdc,(int)(cx+(r-20)*sin(systm.wSecond*PI/30)),(int)(cy-(r-20)*cos(systm.wSecond*PI/30)));
DeleteObject(hpen);
//畫分針
hpen=CreatePen(PS_SOLID,4,RGB(0,255,0));
SelectObject(hdc,hpen);
MoveToEx(hdc,cx,cy,NULL);
LineTo(hdc,(int)(cx+(r-50)*sin(systm.wMinute*PI/30)),(int)(cy-(r-50)*cos(systm.wMinute*PI/30)));
DeleteObject(hpen);
//畫時針
hpen=CreatePen(PS_SOLID,8,RGB(255,0,0));
SelectObject(hdc,hpen);
MoveToEx(hdc,cx,cy,NULL);
LineTo(hdc,(int)(cx+(r-100)*sin(systm.wHour%12*PI/6)),(int)(cy-(r-100)*cos(systm.wHour%12*PI/6)));
DeleteObject(hpen);
EndPaint(_hwnd,&ps);
return 0;
case WM_TIMER:
InvalidateRect(_hwnd,NULL,NULL); //計時器到時,重新整理無效區
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(_hwnd,_message,_wparam,_lparasm);
}