To create a useful digital clock, you must first obtain the system time, then separate the hours, minutes, And seconds, then use polygon to draw a picture, and then use the timer to display it together every second, here, we set the "Seven-segment decoder" for good looks. In fact, it is almost the same as the display of the digital clock of the single chip microcomputer.
Next, let's take a look at how to get the current time. First, let's look at the parameters of the systemtime structure.
typedef struct _SYSTEMTIME { WORD wYear; WORD wMonth; WORD wDayOfWeek; WORD wDay; WORD wHour; WORD wMinute; WORD wSecond; WORD wMilliseconds; } SYSTEMTIME, *PSYSTEMTIME
The parameter description is clear at a glance.
// Use getlocaltime (& St) to obtain the instantiated systemtime object
Next let's take a look at a complete program, a digital clock
# Include <windows. h> # include <windowsx. h> # define id_timer 1 lresult callback windowproc (hwnd, // handle to window uint umsg, // message identifier wparam, // first message parameter lparam // second message parameter); int winapi winmain (hinstance, // handle to current instance hinstance hprevinstance, // handle to previous instance lpstr lpcmdline, // command line int NC Mdshow // show state) {static tchar szappname [] = text ("leidemingzi"); hwnd; MSG; wndclass. cbclsextra = 0; wndclass. cbwndextra = 0; wndclass. hbrbackground = (hbrush) getstockobject (white_brush); wndclass. hcursor = loadcursor (null, idc_arrow); wndclass. hicon = loadicon (null, idi_error); wndclass. hinstance = hinstance; wndclass. lpfnwndproc = windowproc; wndclass. lpszclassname = szappname; wndclass. lpszmen Uname = NULL; wndclass. Style = cs_hredraw | cs_vredraw; If (! Registerclass (& wndclass) {MessageBox (null, text ("the program require Windows NT"), text ("Tips"), mb_iconerror); Return 0 ;} hwnd = createwindow (szappname, // registered Class Name text ("this is Title"), // window name ws_overlappedwindow, // window style cw_usedefault, // horizontal position of window cw_usedefault, // vertical position of window cw_usedefault, // window width cw_usedefault, // window Hei Ght null, // handle to parent or owner window null, // menu handle or child identifier hinstance, // handle to application instance null // window-creation data ); showwindow (hwnd, ncmdshow); updatewindow (hwnd); While (getmessage (& MSG, null, 0, 0) {translatemessage (& MSG); dispatchmessage (& MSG );} return MSG. wparam;} void displaydigit (HDC, int inumber) {static bool fsevensegment [10] [7] = // ten digits. When the corresponding bit is 1, show {1, 1,, 1, //, 0, //, 1, //, 1,, 0, //, 1, // 51,1, 1, // 61,0, 0, // 71,1,, 1, // 81,1, 1 // 9 }; static point ptsegment [7] [6] = // a small diamond consists of six points. A point consists of (x, y) {7, 6, 11, 2, 31, 2, 35, 6, 31, 10, 11, 10, 6, 7, 10, 11, 10, 31, 6, 35, 2, 31, 2, 11, 36, 7, 40, 11, 40, 31, 36, 35, 32, 31, 32, 11, 7, 36, 11, 32, 31, 32, 35, 36, 31, 40, 1 1, 40, 6, 37, 10, 41, 10, 61, 6, 65, 2, 61, 2, 41, 36, 37, 40, 41, 40, 61, 36, 65, 32, 61, 32, 41, 7, 66, 11, 62, 31, 62, 35, 66, 31, 70, 11, 70}; int iseg; for (iseg = 0; iseg <7; ++ iseg) if (fsevensegment [inumber] [iseg]) polygon (HDC, ptsegment [iseg], 6 );} void displaytwodigits (HDC, int inumber, bool fsuppress) {If (! Fsuppress | (inumber/10 )! = 0) {displaydigit (HDC, inumber/10);} offsetworgex (HDC,-, null); // move 42px to the right to write the last digit displaydigit (HDC, inumber % 10); offsetworgex (HDC,-, null);} void displaycolon (HDC) // draw a colon {point ptcolon [2] [4] =, ,}; polygon (HDC, ptcolon [0], 4); polygon (HDC, ptcolon [1], 4 ); offset0000worgex (HDC,-12, 0, null);} void displaytime (HDC, bool f24hour, bool fsuppress) {systemtime St; Getlocaltime (& St); // obtain the system time struct if (f24hour) displaytwodigits (HDC, St. whour, fsuppress); elsedisplaytwodigits (HDC, (St. whour % = 12 )? St. whour: 12, fsuppress); displaycolon (HDC); displaytwodigits (HDC, St. wminute, false); displaycolon (HDC); displaytwodigits (HDC, St. wsecond, false);} lresult callback windowproc (hwnd, // handle to window uint umsg, // message identifier wparam, // first message parameter lparam // second message parameter) {static bool f24hour, fsuppress; static hbrush; static int cxclient, cyclient; HDC; paintstruct pS; tchar szbuffer [2]; Switch (umsg) {Case wm_create: hbrush = createsolidbrush (RGB (1000, 0, 0); settimer (hwnd, id_timer, null); Case wm_settingchange: // when there is a change, determine whether to use the 12-hour or 24-hour getlocaleinfo (locale_user_default, locale_itime, szbuffer, 2 ); f24hour = (szbuffer [0] = '1'); getlocaleinfo (locale_user_default, locale_itlzero, szbuffer, 2); fsuppress = (szbuffer [1] = '0 '); invalidaterect (hwnd, null, true); Return 0; Case wm_size: cxclient = get_x_lparam (lparam); // obtain the client length and width cyclient = get_y_lparam (lparam); Return 0; Case wm_timer: invalidaterect (hwnd, null, true); Return 0; Case wm_paint: HDC = beginpaint (hwnd, & PS); setmapmode (HDC, mm_isotropic ); // GDI ing mode setjavaswextex (HDC, 276,72, null); setviewportextex (HDC, cxclient, cyclient, null); setjavasworgex (HDC, 138,36, null); setviewportorgex (HDC, cxclient/2, cyclient/2, null); SelectObject (HDC, getstockobject (null_pen); // no border SelectObject (HDC, hbrush); displaytime (HDC, f24hour, fsuppress); // display the time endpaint (hwnd, & PS); Return 0; Case wm_destroy: postquitmessage (0); Return 0;} return defwindowproc (hwnd, umsg, wparam, lparam );}
Let's see:
If you do not understand the GDI ing, you can refer to a previously written GDI ing blog. The content in the customer zone is automatically scaled according to the screen size.