Analog clock, clock

Source: Internet
Author: User

Analog clock, clock

1 # include <Windows. h> 2 # include <tchar. h> 3 # include <math. h> 4 typedef struct Time 5 {6 int hour, min, sec; 7} TimeStructure; 8 BOOLEAN InitWindowClass (HINSTANCE hInstance, int nCmdShow); 9 lresult callback WndProc (HWND, UINT, WPARAM, LPARAM); 10 void AdjustTime (TimeStructure * x); 11 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 12 {13 MSG; 14 if (! InitWindowClass (hInstance, nCmdShow) 15 {16 MessageBox (NULL, L "window creation failed! ", _ T (" create window "), NULL); 17 return 1; 18} 19 while (GetMessage (& msg, NULL, 0, 0 )) 20 {21 TranslateMessage (& msg); 22 DispatchMessage (& msg); 23} 24 return (int) msg. wParam; 25} 26 lresult callback WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 27 {28 HDC hDC; 29 PAINTSTRUCT ps; 30 HBRUSH hBrush; 31 HPEN hPen; 32 RECT clientRect; 33 static TimeStructure x; 34 float sita = 0; 35 int xOr G, yOrg, rSec, rMin, rHour, rClock, xBegin, xEnd, yBegin, yEnd; 36 switch (message) 37 {38 case WM_CREATE: // when creating a window, response Message 39 SetTimer (hWnd, 9999,100 0, NULL); // set the timer 40 break; 41 case WM_PAINT: 42 x. sec ++; 43 AdjustTime (& x); 44 hDC = BeginPaint (hWnd, & ps); 45 GetClientRect (hWnd, & clientRect ); // obtain the size of the user Area 46 hPen = (HPEN) GetStockObject (BLACK_PEN); // set the paint brush to the pre-defined black paint brush 47 hBrush = CreateSolidBrush (RGB (255,220,220); // create a pink monochrome paint brush 48 SelectObject (hDC, hPen); // select paint brush 49 SelectObject (hDC, hBrush ); // select image brush 50 xOrg = (clientRect. left + clientRect. right)/2; 51 yOrg = (clientRect. top + clientRect. bottom)/2; // calculate the coordinates of the center of the screen, which is also the center of the clock 52 rClock = min (xOrg, yOrg)-50; // The radius of the clock is 53 rSec = rClock * 6/7; // the radius of the second-hand is 54 rMin = rClock * 5/6; // the radius of the sub-needle is 55 rHour = rClock * 2/3; // The radius of the hour hand is 56 Ellipse (hDC, xOrg-rCloc K, yOrg-rClock, xOrg + rClock, yOrg + rClock); // draw the surface circle 57 for (int I = 0; I <60; I ++) // plot the scale of the surface 58 {59 if (I % 5) // draw the whole scale of the surface 60 {61 hPen = CreatePen (PS_SOLID, 2, RGB (255, 0, 0); 62 SelectObject (hDC, hPen); 63 xBegin = xOrg + rClock * sin (2x3.1415926 * I/60 ); 64 yBegin = yOrg + rClock * cos (2*3.1415926 * I/60); 65 MoveToEx (hDC, xBegin, yBegin, NULL); 66 xEnd = xOrg + (rClock-20) * sin (2*3. 1415926 * I/60); 67 yEnd = yOrg + (rClock-20) * cos (2*3.1415926 * I/60 ); 68 69} 70 else // draw a non-integral scale of the surface 71 {72 hPen = CreatePen (PS_SOLID, 5, RGB (255, 0, 0); 73 SelectObject (hDC, hPen); 74 xBegin = xOrg + rClock * sin (2*3.1415926 * I/60); 75 yBegin = yOrg + rClock * cos (2*3.1415926 * I/60 ); 76 MoveToEx (hDC, xBegin, yBegin, NULL); 77 xEnd = xOrg + (rClock-25) * sin (2*3.1415926 * I/60); 78 yEn D = yOrg + (rClock-25) * cos (2*3.1415926 * I/60); 79} 80 LineTo (hDC, xEnd, yEnd); 81 DeleteObject (hPen ); 82} 83 hPen = CreatePen (PS_SOLID, 2, RGB (255, 0, 0); 84 SelectObject (hDC, hPen); 85 sita = 2*3.1415926 * x. sec/60; 86 xBegin = xOrg + (int) (rSec * sin (sita); 87 yBegin = yOrg-(int) (rSec * cos (sita )); // The starting point of the second-hand. Its position is at the end of the second-hand. 88 xEnd = xOrg + (int) (rClock * sin (sita + 3.1415926)/8); 89 yEnd = y Org-(int) (rClock * cos (sita + 3.1415926)/8); // the end point of the second needle, the length of the second-hand direction is 1/8 90 MoveToEx (hDC, xBegin, yBegin, NULL); 91 LineTo (hDC, xEnd, yEnd ); // draw the second-hand 92 hPen = CreatePen (PS_SOLID, 5, RGB (0, 0, 0); 93 SelectObject (hDC, hPen); 94 sita = 2*3.1415926 * x. min/60; 95 xBegin = xOrg + (int) (rMin * sin (sita); 96 yBegin = yOrg-(int) (rMin * cos (sita )); // the start point of the sub-needle 97 xEnd = xOrg + (int) (rClock * sin (sita + 3.14 15926)/8); 98 yEnd = yOrg-(int) (rClock * cos (sita + 3.1415926)/8); // the end point of the sub-needle 99 MoveToEx (hDC, xBegin, yBegin, NULL); 100 LineTo (hDC, xEnd, yEnd); // draw the sub-needle 101 hPen = CreatePen (PS_SOLID, 10, RGB (0, 0, 0 )); 102 SelectObject (hDC, hPen); 103 sita = 2*3.1415926 * x. hour/12; 104 xBegin = xOrg + (int) (Fig * sin (sita); 105 yBegin = yOrg-(int) (Fig * cos (sita )); 106 xEnd = xOrg + (int) (rClock * sin (sita + 3.1415 926)/8); 107 yEnd = yOrg-(int) (rClock * cos (sita + 3.1415926)/8); 108 MoveToEx (hDC, xBegin, yBegin, NULL ); 109 LineTo (hDC, xEnd, yEnd); // draw DeleteObject (hPen); 110 DeleteObject (hBrush); 111 EndPaint (hWnd, & ps ); // end drawing 113 break; 114 case WM_TIMER: // responds to the timer message sent by the timer 115 if (wParam = 9999) // determine whether the message is set by the timer 116 InvalidateRect (hWnd, NULL, true); // when the screen is refreshed 117 break; 118 case WM_SIZE: // when the window size is changed, refresh window 119 Inv AlidateRect (hWnd, NULL, true); 120 break; 121 case WM_DESTROY: 122 PostQuitMessage (0); // call PostQuitMessage to send the WM_QUIT message 123 break; 124 default: 125 return DefWindowProc (hWnd, message, wParam, lParam); // default system message processing function 126 break; 127} 128 return 0; 129} 130 void AdjustTime (TimeStructure * x) 131 {132 if (x-> sec = 60) 133 {134 x-> sec = 0; 135 x-> min ++; 136 if (x-> min = 60) 137 {138 x-> min = 0; 139 x-> hour ++; 140 if (X-> hour = 12) 141 x-> hour = 0; 142} 143} 144 BOOLEAN InitWindowClass (HINSTANCE hInstance, int nCmdShow) 145 {146 WNDCLASSEX wcex; 148 HWND hWnd; 149 TCHAR szWindowClass [] = L "window example"; 150 TCHAR szTitle [] = L "analog clock"; 151 wcex. cbSize = sizeof (WNDCLASSEX); 152 wcex. style = 0; 153 wcex. lpfnWndProc = WndProc; 154 wcex. cbClsExtra = 0; 155 wcex. cbWndExtra = 0; 156 wcex. hInstance = hInstance; 157 wcex. hIcon = Loa DIcon (hInstance, MAKEINTRESOURCE (IDI_APPLICATION); 158 wcex. hCursor = LoadCursor (NULL, IDC_ARROW); 159 wcex. hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH); 160 wcex. lpszMenuName = NULL; 161 wcex. lpszClassName = szWindowClass; 162 wcex. hIconSm = LoadIcon (wcex. hInstance, MAKEINTRESOURCE (IDI_APPLICATION), 163 if (! RegisterClassEx (& wcex) 164 return FALSE; 165 hWnd = CreateWindow (166 szWindowClass, 167 szTitle, 168 WS_OVERLAPPEDWINDOW, 169 CW_USEDEFAULT, CW_USEDEFAULT, 170 CW_USEDEFAULT, CW_USEDEFAULT, 171 NULL, 172 NULL, 173 hInstance, 174 NULL175); 176 if (! HWnd) 177 return FALSE; 178 ShowWindow (hWnd, nCmdShow); 179 UpdateWindow (hWnd); 180 return TRUE; 181}

 

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.