计时器(3)机械时钟

获取当前时间

SYSTEMTIME st;
GetSystemTime(&st);//是伦敦XX时间,跟北京时间相差8个时区
GetLocaltime(&st)//获取的你电脑系统上显示的时间
LRESULT CALLBACK WindowProc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
)
{
	SYSTEMTIME st;
	HDC hdc;
	PAINTSTRUCT ps;
	TCHAR szBuffer[50];

	switch(uMsg)
	{
	case WM_CREATE:
		//GetSystemTime(&st);//获取的小时数跟北京的相差8个小时
		GetLocalTime(&st);
		wsprintf(szBuffer,TEXT("the time is %d H: %d m: %d s"),(int)st.wHour,st.wMinute,st.wSecond);
		MessageBox(NULL,szBuffer,TEXT("systemtime"),MB_ICONERROR);
		return 0;

	case WM_PAINT:
		
		return 0;

	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	}
	return DefWindowProc(hwnd,uMsg,wParam,lParam);
}



再看看SYSTEMTIME的结构吧

typedef struct _SYSTEMTIME { 
    WORD wYear; 
    WORD wMonth; 
    WORD wDayOfWeek; 
    WORD wDay; 
    WORD wHour; 
    WORD wMinute; 
    WORD wSecond; 
    WORD wMilliseconds; 
} SYSTEMTIME, *PSYSTEMTIME;


下面看看机械时钟,很酷喔

#include<windows.h>
#include<windowsx.h>
#include<math.h>
#define ID_TIMER 1
#define TOWPI (2*3.14159)

LRESULT CALLBACK WindowProc(
  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
)
{
	static TCHAR szAppName[]=TEXT("leidemingzi");
	HWND hwnd;
	MSG msg;
	WNDCLASS wndclass;

	wndclass.cbClsExtra=0;
	wndclass.cbWndExtra=0;
	wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
	wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
	wndclass.hIcon=LoadIcon(NULL,IDI_ERROR);
	wndclass.hInstance=hInstance;
	wndclass.lpfnWndProc=WindowProc;
	wndclass.lpszClassName=szAppName;
	wndclass.lpszMenuName=NULL;
	wndclass.style=CS_HREDRAW|CS_VREDRAW;
	
	if(!RegisterClass(&wndclass))
	{
		MessageBox(NULL,TEXT("The program require window nt"),TEXT("this is tips"),MB_ICONERROR);
		return 0;
	}

	hwnd=CreateWindow(
	  szAppName,  // registered class name
	  TEXT("this is title"), // window name
	  WS_OVERLAPPEDWINDOW,        // window style
	  CW_USEDEFAULT,                // horizontal position of window
	  CW_USEDEFAULT,                // vertical position of window
	  CW_USEDEFAULT,           // window width
	  CW_USEDEFAULT,          // window height
	  NULL,      // handle to parent or owner window
	  NULL,          // menu handle or child identifier
	  hInstance,  // handle to application instance
	  NULL        // window-creation data
);

	ShowWindow(hwnd,nCmdShow);
	UpdateWindow(hwnd);

	while(GetMessage(&msg,NULL,0,0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return msg.wParam;
}

void SetIsotropic(HDC hdc,int cxClient,int cyClient)
{
	SetMapMode(hdc,MM_ISOTROPIC);
	SetWindowExtEx(hdc,1000,1000,NULL);
	SetViewportExtEx(hdc,cxClient/2,-cyClient/2,NULL);
	SetViewportOrgEx(hdc,cxClient/2,cyClient/2,NULL);
}

void RotatePoint(POINT pt[],int iNum,int iAngle)
{
	int i;
	POINT ptTemp;

	for(i=0;i<iNum;++i)
	{
		ptTemp.x=(int)(pt[i].x*cos(TOWPI*iAngle/360)+pt[i].y*sin(TOWPI*iAngle/360));

		ptTemp.y=(int)(pt[i].y*cos(TOWPI*iAngle/360)-pt[i].x*sin(TOWPI*iAngle/360));

		pt[i]=ptTemp;
	}
}

void DrawClock(HDC hdc)
{
	int iAngle;
	POINT pt[3];

	for(iAngle=0;iAngle<360;iAngle+=6)
	{
		pt[0].x=0;
		pt[0].y=900;

		RotatePoint(pt,1,iAngle);

		pt[2].x=pt[2].y=iAngle%5?33:100;

		pt[0].x-=pt[2].x/2;
		pt[0].y-=pt[2].y/2;

		pt[1].x=pt[0].x+pt[2].x;
		pt[1].y=pt[0].y+pt[2].y;

		SelectObject(hdc,GetStockObject(BLACK_BRUSH));
		Ellipse(hdc,pt[0].x,pt[0].y,pt[1].x,pt[1].y);
	}
}

void DrawHands(HDC  hdc,SYSTEMTIME *pst,BOOL fChange)
{
	 static POINT pt[3][5] = { 0, -150, 100, 0, 0, 600, -100, 0, 0, -150,
                               0, -200,  50, 0, 0, 800,  -50, 0, 0, -200,
                               0,    0,   0, 0, 0,   0,    0, 0, 0,  800 } ;

	int i,iAngle[3];
	POINT ptTemp[3][5];

	iAngle[0]=(pst->wHour*30)%360+pst->wMinute/2;
	iAngle[1]=pst->wMinute*6;
	iAngle[2]=pst->wSecond*6;

	memcpy(ptTemp,pt,sizeof(pt));

	for(i=fChange?0:2;i<3;++i)
	{
		RotatePoint(ptTemp[i],5,iAngle[i]);

		Polyline(hdc,ptTemp[i],5);
	}


}

LRESULT CALLBACK WindowProc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
)
{
	static int cxClient,cyClient;
	static SYSTEMTIME stPrevious;
	HDC hdc;
	bool fChange;
	PAINTSTRUCT ps;
	SYSTEMTIME st;

	switch(uMsg)
	{
	case WM_CREATE:
		SetTimer(hwnd,ID_TIMER,1000,NULL);
		GetLocalTime(&st);
		stPrevious=st;
		return 0;

	case WM_SIZE:
		cxClient=GET_X_LPARAM(lParam);
		cyClient=GET_Y_LPARAM(lParam);
		return 0;

	case WM_TIMER:
		GetLocalTime(&st);
		fChange=st.wHour!=stPrevious.wHour||st.wMinute!=stPrevious.wMinute;
		hdc=GetDC(hwnd);
		SetIsotropic(hdc,cxClient,cyClient);

		SelectObject(hdc,GetStockObject(WHITE_PEN));
		DrawHands(hdc,&stPrevious,fChange);

		SelectObject(hdc,GetStockObject(BLACK_PEN));
		DrawHands(hdc,&st,TRUE);
		//InvalidateRect(hwnd,NULL,TRUE);
		ReleaseDC(hwnd,hdc);

		stPrevious=st;
		return 0;

	case WM_PAINT:
		hdc=BeginPaint(hwnd,&ps);
		SetIsotropic(hdc,cxClient,cyClient);
		DrawClock(hdc);
		DrawHands(hdc,&stPrevious,TRUE);

		EndPaint(hwnd,&ps);
		return 0;

	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	}
	return DefWindowProc(hwnd,uMsg,wParam,lParam);



}

 

怎么能没有效果图呢:

计时器(3)机械时钟_第1张图片
需要一点三角函数的知识,弄明白位置与角度的关系。

 

你可能感兴趣的:(计时器(3)机械时钟)