# Draw a clock in the Windows Console

Source: Internet
Author: User

I have nothing to learn about C ++ programming.

:

Using a thread to repeatedly redraw:

`#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 );}`

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.