I have nothing to learn about C ++ programming.
:
Using a thread to repeatedly redraw:
Header file:
#include "stdafx.h"#include <Windows.h>#include <time.h>extern "C"{WINBASEAPI HWND WINAPI GetConsoleWindow();}class TimeWatch{public:TimeWatch(int x,int y,int radius);~TimeWatch();void Start();void DrawTimeWatch();private:HWND hwnd;HDC hdc;HANDLE hThread;DWORD ThreadID;int radius;int x,y;void DrawTimeLine();void DrawTimePoint();};
Code implementation:
# Include "stdafx. H "# include" intarray. HPP "# include <math. h ># include <iostream> using namespace STD; timewatch: timewatch (int x, int y, int radius) {This-> X = x; this-> Y = y; this-> radius = radius;} timewatch ::~ Timewatch () {}# define Pai 3.1415926 # define hourlinelen 30 # define minutelinelen 45 # define secondlinelen 50 void timewatch: drawtimepoint () {int COUNT = 12; int step = 30; int PX, Py; double rad; hwnd = getconsolewindow (); HDC = getdc (hwnd); step = 30; ellipse (HDC, x-10-radius, y-10-radius, x + 10 + radius, Y + 10 + radius); Int J = 1; setpixel (HDC, X, Y, 255); For (INT I = 0; I <360; I + = step) {rad = 2 * PAI * 1 * I/360; PX = (INT) (COS (RAD) * radius); py = (INT) (sin (RAD) * radius); setpixel (HDC, x + px, Y + py, 255) ;}} void timewatch: drawtimeline () {double rad; struct TM * ltm; time_t now; int minutetime, PX, Py; double hourtimelinearc, minutelinearc, senlinearc; time (& now); // draw the clock ltm = localtime (& now ); minutetime = ltm-> tm_hour % 12*60 + ltm-> tm_min; hourtimelinearc = minutetime * 1.0/(12*60) * 360-90; rad = 2 * PAI * 1 * hourtimelinearc/360; PX = (INT) (COS (RAD) * hourlinelen); py = (INT) (sin (RAD) * hourlinelen ); movetoex (HDC, X, Y, null); lineto (HDC, x + px, Y + Py); // minutelinearc = ltm-> tm_min * (360/60)-90; rad = 2 * PAI * 1 * minutelinearc/360; PX = (INT) (COS (RAD) * minutelinelen); py = (INT) (sin (RAD) * minutelinelen ); movetoex (HDC, X, Y, null); lineto (HDC, x + px, Y + Py); // second senlinearc = ltm-> tm_sec * (360/60)-90; rad = 2 * PAI * 1 * senlinearc/360; PX = (INT) (COS (RAD) * secondlinelen); py = (INT) (sin (RAD) * secondlinelen ); movetoex (HDC, X, Y, null); lineto (HDC, x + px, Y + Py);} void threadfunc (timewatch * tw) {While (1) {Tw-> drawtimewatch (); sleep (1000) ;}} void timewatch: drawtimewatch () {drawtimepoint (); drawtimeline ();} void timewatch: Start () {hthread = createthread (null, 0, (lpthread_start_routine) threadfunc, (void *) This, 0, & threadid );}