C++时钟代码

来源:互联网 发布:淘宝店知名古琴排行 编辑:程序博客网 时间:2024/05/17 03:32

//C++编写时钟源代码

 

#include <windows.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define pi 3.1415926
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)

 MSG Message;
    WNDCLASS WndClass;
    WndClass.cbClsExtra=0;
    WndClass.cbWndExtra=0;
    WndClass.hbrBackground=(HBRUSH)(GetStockObject(WHITE_BRUSH));
    WndClass.hCursor=LoadCursor(NULL,IDC_ARROW);
    WndClass.hIcon=LoadIcon(NULL,"END");
    WndClass.hInstance=hInstance;
    WndClass.lpfnWndProc=WndProc;
    WndClass.lpszClassName="WinFill";
    WndClass.lpszMenuName=NULL;
    WndClass.style=CS_HREDRAW|CS_VREDRAW;
    RegisterClass(&WndClass);

 HWND hWnd;
 hWnd=CreateWindow("WinFill",  //生成窗口
        "时       钟        ",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,
        0,
        CW_USEDEFAULT,
        0,
        NULL,
        NULL,
        hInstance,
        NULL);
 
  ShowWindow(hWnd,nCmdShow); //显示窗口
  UpdateWindow(hWnd);
 
 while(GetMessage(&Message,0,0,0)) //消息循环
       { 
  TranslateMessage(&Message);
  DispatchMessage(&Message);
       }
 return Message.wParam;
}


int m=1,n=60;
int p,q=12;
//VOID WINAPI GetLocalTime(LPSYSTEMTIME lpSystemTime); //获取当前系统时间函
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM  wParam,LPARAM  lParam)
{  
 HDC hdc;
    PAINTSTRUCT ps;
 SYSTEMTIME lpSystemTime;
    HPEN hpen,HourPen,MinutePen,SecondPen;
    switch(message)
   {
    case WM_PAINT:
  GetLocalTime(&lpSystemTime);
  hdc=BeginPaint(hwnd,&ps);
  hpen=CreatePen(PS_SOLID,4,RGB(0,255,255));
  SelectObject(hdc,hpen); 
     Ellipse(hdc,200,200,400,400);
  Ellipse(hdc,295,295,305,305);   
  double Rad,RadS;
  //RadS=2*pi*m/n;
  hpen=CreatePen(PS_SOLID,3,RGB(255,0,0));
  SelectObject(hdc,hpen); 
  for(p=0;p<=q;p++)
  {
   Rad=2*pi*p/q;
   MoveToEx(hdc,(int)(300+100*sin(Rad)),(int)(300-100*cos(Rad)),NULL);
   LineTo(hdc,(int)(300+80*sin(Rad)),(int)(300-80*cos(Rad)));
  }
  m=(double)lpSystemTime.wSecond;
  RadS=2*pi*m/n;
  SecondPen=CreatePen(PS_SOLID,2,RGB(0,255,0));
     SelectObject(hdc,SecondPen);
  MoveToEx(hdc,300,300,NULL);
  LineTo(hdc,(int)(300+80*sin(RadS)),(int)(300-80*cos(RadS)));
  m=(double)(lpSystemTime.wMinute*60.0+lpSystemTime.wSecond);
     RadS=2*pi*m/n;
  MinutePen=CreatePen(PS_SOLID,3,RGB(0,255,255));
     SelectObject(hdc,MinutePen);
  MoveToEx(hdc,300,300,NULL);
  LineTo(hdc,(int)(300+60*sin(RadS/60.0)),(int)(300-60*cos(RadS/60.0)));
     m=(double)(lpSystemTime.wHour*3600.0+lpSystemTime.wMinute*60.0+lpSystemTime.wSecond);
  RadS=2*pi*m/n;
     HourPen=CreatePen(PS_SOLID,4,RGB(255,255,0));
     SelectObject(hdc,HourPen);
  MoveToEx(hdc,300,300,NULL);
  LineTo(hdc,(int)(300+40*sin(5*RadS/3600.0)),(int)(300-40*cos(5*RadS/3600.0)));

  if(1)
  {
   
   Sleep(1000);
         InvalidateRect(hwnd,NULL,1);
   m++;
  }
  

     EndPaint(hwnd,&ps);
     break;
 case WM_DESTROY:
  PostQuitMessage(0); 
  break;  
    default: 
  return  DefWindowProc(hwnd,message,wParam,lParam);
 }
 return 0;
}

原创粉丝点击