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;       
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值