Trainer-use Windows SDK to write a simple clock

Source: Internet
Author: User

Experts on the Internet say that learning programming must be at the underlying level, and I will also learn it.

# Include <windows. h>
# Include <math. h>

# Define PI 3.1415926535
# Define idt_tm1 1
Struct time_point {
Int X;
Int y;
Char s [2];
}; // This is the time point structure on the clock disk.

Lresult callback windproc (hwnd, uint, wparam, lparam );
Int winapi winmain (hinstance, hinstance hprevinstance, lpstr lpcmdline, int nshowcmd );
Bool init_wndclass (hinstance );
Bool init_window (hinstance, int nshowcmd );

Hwnd _ hwnd;
Wndclass _ windclass;
MSG _ MSG;
Tchar _ clsname [] = "sunjoy class ";

Bool init_wndclass (hinstance ){
 
_ Windclass. Style = cs_hredraw | cs_vredraw;
_ Windclass. hinstance = hinstance;
_ Windclass. lpfnwndproc = windproc;
_ Windclass. cbclsextra = 0;
_ Windclass. cbwndextra = 0;
_ Windclass. hbrbackground = (hbrush) getstockobject (white_brush );
_ Windclass. hcursor = loadcursor (null, idc_arrow );
_ Windclass. hicon = loadicon (null, idi_application );
_ Windclass. lpszclassname = _ clsname;
_ Windclass. lpszmenuname = NULL;
 
Return (registerclass (& _ windclass )! = NULL );
}

Bool init_window (hinstance, int nshowcmd ){
_ Hwnd = createwindow (_ clsname,
"Simple Clock ",
Ws_overlappedwindow,
Cw_usedefault,
Cw_usedefault,
450,
450,
NULL,
NULL,
HInstance,
NULL );
ShowWindow (_ hwnd, nShowCmd );
Return (UpdateWindow (_ hwnd )! = NULL );
}

Int WINAPI WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
Int nShowCmd)
{
If (! Init_WndClass (hInstance) |! Init_WINDOW (hInstance, nShowCmd )){
MessageBox (NULL, "Create window error", NULL, NULL );
}

 
 
While (GetMessage (& _ msg, NULL, 0, 0 )){
TranslateMessage (& _ msg );
DispatchMessage (& _ msg );
}
Return _ msg. wParam;
}

Lresult callback WindProc (HWND _ hwnd,
UINT _ message,
WPARAM _ wparam,
LPARAM _ lparasm)
{
HDC hdc;
HPEN hpen;
HBRUSH hbrush;
PAINTSTRUCT ps;
Int cx = 220; // x coordinate of the center
Int cy = 215; // y coordinate of the center
Int r = 200; // radius of the circle
TIME_POINT tmp [12]; // time
Int I = 0;
SYSTEMTIME running m; // system time
For (I = 0; I <12; I ++ ){
Tmp [I]. x = (int) (cx + (R-10) * sin (I * PI/6 ));
Tmp [I]. y = (int) (cy-(R-10) * cos (I * PI/6 ));
Wsprintf (TMP [I]. S, "% d", I );
}
Strcpy (TMP [0]. S, "12"); // initialize the coordinates of the clock disk.
 
Switch (_ message ){
Case wm_create:
Settimer (_ hwnd, idt_tm1, 1000, null );
Return 0;
Case wm_paint:

HDC = beginpaint (_ hwnd, & PS );
Hpen = createpen (ps_solid, 2, RGB (0, 0 ));
SelectObject (HDC, Hpen );
Movetoex (HDC, CX, Cy, null );
ARC (HDC, CX-R, cy-R, R + cx, R + cy, cx + R, Cy, cx + R, CY); // draw a clock Disc
For (I = 0; I <12; I ++ ){
Textout (HDC, TMP [I]. x-5, TMP [I]. y-8, TMP [I]. S, strlen (TMP [I]. s ));
} // Initialize the clock Disk
Deleteobject (Hpen );

// White
Hbrush = (HBRUSH) GetStockObject (WHITE_BRUSH );
Hpen = CreatePen (PS_SOLID, 1, RGB (255,255,255 ));
SelectObject (hdc, hbrush );
SelectObject (hdc, hpen );
Pie (hdc, cx-r + 19, cy-r + 19, cx + r-19, cy + r-19, cx + r-19, cy, cx + r-19, cy );
MoveToEx (hdc, cx, cy, NULL );

DeleteObject (hbrush );
DeleteObject (hpen );

// Obtain the local time
GetLocalTime (& amp; m );
// Seconds
Hpen = CreatePen (PS_SOLID, 2, RGB (0, 0, 255 ));
SelectObject (hdc, hpen );
MoveToEx (hdc, cx, cy, NULL );
LineTo (hdc, (int) (cx + (R-20) * sin (small M. wSecond * PI/30), (int) (cy-(R-20) * cos (cost M. wSecond * PI/30 )));
DeleteObject (hpen );

// Draw the sub-needle
Hpen = CreatePen (PS_SOLID, 4, RGB (0,255, 0 ));
SelectObject (hdc, hpen );
MoveToEx (hdc, cx, cy, NULL );
LineTo (hdc, (int) (cx + (r-50) * sin (small M. wMinute * PI/30), (int) (cy-(r-50) * cos (cost M. wMinute * PI/30 )));
DeleteObject (hpen );

// Draw the hour hand

Hpen = CreatePen (PS_SOLID, 8, RGB (255, 0 ));
SelectObject (hdc, hpen );
MoveToEx (hdc, cx, cy, NULL );
LineTo (hdc, (int) (cx + (r-100) * sin (small M. wHour % 12 * PI/6), (int) (cy-(r-100) * cos (Bytes M. wHour % 12 * PI/6 )));
Deleteobject (Hpen );

Endpaint (_ hwnd, & PS );

Return 0;
Case wm_timer:
Invalidaterect (_ hwnd, null, null); // when the timer arrives, refresh the invalid Zone
Return 0;
Case wm_destroy:
Postquitmessage (0 );
Return 0;
}

Return defwindowproc (_ hwnd, _ message, _ wparam, _ lparasm );

}

Related Article

Contact Us

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.