windows 簡易clock實現

 效果圖

 

代碼 


/***************************************************
 *CopyRight @piaoxiang.zhang
 ***************************************************/
#include <windows.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#define PI 3.14159265
static HWND mainHwnd ;
static char szAppName[]={"clock"};
typedef struct {
   char hour;
   char min;
   char sec;
}Time;
Time time_pre;
void Debug(int i,char *s)
{
  char v[10];
  sprintf(v,"%d",i);
  MessageBox(0,v,s,0);
}
void SetTimeStruct(char hour,char min,char sec)
{
   time_pre.hour=hour;
   time_pre.min =min;
   time_pre.sec =sec;
}
void InitTimeStruct(void)
{
   SetTimeStruct(0,0,0);
}
void GetCurTime(Time *time_cur)
{
  struct tm *p;
  time_t t;
  t=time(NULL);
  p=gmtime(&t);
  time_cur->hour=(char)(p->tm_hour+5)%12;
  time_cur->min= (char) p->tm_min-15;
  time_cur->sec= (char) p->tm_sec;
}
BOOL IsTimeChange(Time c,Time p)
{
  if(c.hour==p.hour&&c.min==p.min&&c.sec==p.sec)return FALSE;
  return TRUE;
}
DWORD WINAPI ThreadProc(LPVOID lpParam)
{
   Time time_cur;
   InitTimeStruct();
   while(1){
      Sleep(10);
      GetCurTime(&time_cur);
      if(IsTimeChange(time_cur,time_pre)){
        time_pre=time_cur;
        InvalidateRect(mainHwnd,NULL,FALSE);
      }           
   }
}
void DrawLine(HDC hdc,int len,char c,char t)
{
  double arc=PI*2*c/t;
  if(t==12)arc+=PI*time_pre.min/60/3 ;
  MoveToEx(hdc,220,220,NULL);
  LineTo(hdc,220+len*cos(arc),220+len*sin(arc)) ;
}
void DrawPoint(HDC hdc)
{
  Ellipse(hdc,5,5,445,445);
  for(int i=0;i<60;i++){
    if(i%5==0){
      MoveToEx(hdc,220*cos(i*PI/30)+225,220*sin(i*PI/30)+225,NULL);
      LineTo(hdc,210*cos(i*PI/30)+225,210*sin(i*PI/30)+225) ;
    }else {
      MoveToEx(hdc,220*cos(i*PI/30)+224,220*sin(i*PI/30)+224,NULL);
      LineTo(hdc,214*cos(i*PI/30)+224,214*sin(i*PI/30)+224) ;  
    }
  }
  DrawLine(hdc,200,time_pre.sec,60);
  DrawLine(hdc,150,time_pre.min,60);
  DrawLine(hdc,100,time_pre.hour,12);
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
 HDC hdc;
 PAINTSTRUCT ps ;
 mainHwnd=hwnd;
 static HANDLE hThread;
 switch(message){
   case WM_CREATE:           
       hThread=CreateThread(NULL,0,ThreadProc,NULL,0,NULL);      
       CloseHandle(hThread);     
       return 0;   
   case WM_PAINT :
        hdc = BeginPaint (hwnd, &ps) ;                       
        DrawPoint(hdc);                     
        EndPaint (hwnd, &ps) ;
       return 0 ;
   case WM_DESTROY:
      PostQuitMessage(0);    
 }
 return DefWindowProc(hwnd,message,wParam,lParam);
}                                                                       
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
                                   PSTR szCmdLine,int nCmdShow)
{
   HWND hwnd=NULL;
   HICON hicon;
   MSG msg;
   WNDCLASS wc;
   hicon=LoadIcon(hInstance,MAKEINTRESOURCE(101));
   ZeroMemory(&wc,sizeof wc);
   wc.hInstance     = hInstance;
   wc.lpszClassName = szAppName;
   wc.lpfnWndProc   = (WNDPROC)WndProc;
   wc.style         = CS_DBLCLKS|CS_VREDRAW|CS_HREDRAW;
   wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
   wc.hIcon         = hicon;
   wc.hCursor       = LoadCursor(NULL,IDC_ARROW);
   if (FALSE==RegisterClass(&wc))return 0;
   hwnd = CreateWindow (szAppName,szAppName,
           WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX,
           260,100,455,480,HWND_DESKTOP,NULL,hInstance,NULL);
   ShowWindow(hwnd,nCmdShow);UpdateWindow(hwnd);
   while(GetMessage(&msg,NULL,0,0)>0){
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
   return msg.wParam;       
}

 

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