标签:
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 msg; 14 if (!InitWindowClass(hInstance, nCmdShow)) 15 { 16 MessageBox(NULL, L"创建窗口失败!", _T("创建窗口"), 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 xOrg, yOrg, rSec, rMin, rHour, rClock, xBegin, xEnd, yBegin, yEnd; 36 switch (message) 37 { 38 case WM_CREATE: //创建窗口时,响应的消息 39 SetTimer(hWnd, 9999, 1000, NULL); //设置定时器 40 break; 41 case WM_PAINT: 42 x.sec++; 43 AdjustTime(&x); 44 hDC = BeginPaint(hWnd, &ps); 45 GetClientRect(hWnd, &clientRect); //获得用户区的尺寸 46 hPen = (HPEN)GetStockObject(BLACK_PEN); //设置画笔为系统预定义的黑色画笔 47 hBrush = CreateSolidBrush(RGB(255, 220, 220)); //创建粉红色的单色画刷 48 SelectObject(hDC, hPen); //选择画笔 49 SelectObject(hDC, hBrush); //选择画刷 50 xOrg = (clientRect.left + clientRect.right) / 2; 51 yOrg = (clientRect.top + clientRect.bottom) / 2; //计算屏幕中心的坐标,它也是时钟的中心 52 rClock = min(xOrg, yOrg) - 50; //钟表的半径 53 rSec = rClock * 6 / 7; //秒针的半径 54 rMin = rClock * 5 / 6; //分针的半径 55 rHour = rClock * 2 / 3; //时针的半径 56 Ellipse(hDC, xOrg - rClock, yOrg - rClock, xOrg + rClock, yOrg + rClock);//绘制表面圆 57 for (int i = 0; i < 60; i++) //绘制表面的刻度 58 { 59 if (i % 5) //绘制表面表面的整点刻度 60 { 61 hPen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0)); 62 SelectObject(hDC, hPen); 63 xBegin = xOrg + rClock*sin(2 * 3.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 //绘制表面表面的非整点刻度 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 yEnd = 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)); //秒针的起点,它的位置在秒针的最末端 88 xEnd = xOrg + (int)(rClock*sin(sita + 3.1415926) / 8); 89 yEnd = yOrg - (int)(rClock*cos(sita + 3.1415926) / 8);//秒针的终点,它的位置在秒针的反方向的长度为秒针的1/8 90 MoveToEx(hDC, xBegin, yBegin, NULL); 91 LineTo(hDC, xEnd, yEnd); //绘制秒针 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)); //分针的起点 97 xEnd = xOrg + (int)(rClock*sin(sita + 3.1415926) / 8); 98 yEnd = yOrg - (int)(rClock*cos(sita + 3.1415926) / 8);//分针的终点 99 MoveToEx(hDC, xBegin, yBegin, NULL); 100 LineTo(hDC, xEnd, yEnd); //绘制分针 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)(rHour*sin(sita)); 105 yBegin = yOrg - (int)(rHour*cos(sita)); 106 xEnd = xOrg + (int)(rClock*sin(sita + 3.1415926) / 8); 107 yEnd = yOrg - (int)(rClock*cos(sita + 3.1415926) / 8); 108 MoveToEx(hDC, xBegin, yBegin, NULL); 109 LineTo(hDC, xEnd, yEnd); //绘制时针 110 DeleteObject(hPen); 111 DeleteObject(hBrush); 112 EndPaint(hWnd, &ps); //结束绘图 113 break; 114 case WM_TIMER: //响应定时器发出的定时消息 115 if (wParam == 9999) //判断是否是设置的定时器发出的消息 116 InvalidateRect(hWnd, NULL, true); //刷新屏幕 117 break; 118 case WM_SIZE: //窗口尺寸改变时,刷新窗口 119 InvalidateRect(hWnd, NULL, true); 120 break; 121 case WM_DESTROY: 122 PostQuitMessage(0); //调用PostQuitMessage发出WM_QUIT消息 123 break; 124 default: 125 return DefWindowProc(hWnd, message, wParam, lParam); //默认时采用系统消息默认处理函数 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 } 145 BOOLEAN InitWindowClass(HINSTANCE hInstance, int nCmdShow) 146 { 147 WNDCLASSEX wcex; 148 HWND hWnd; 149 TCHAR szWindowClass[] = L"窗口示例"; 150 TCHAR szTitle[] = L"模拟时钟"; 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 = LoadIcon(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 NULL 175 ); 176 if (!hWnd) 177 return FALSE; 178 ShowWindow(hWnd, nCmdShow); 179 UpdateWindow(hWnd); 180 return TRUE; 181 }
标签:
原文地址:http://www.cnblogs.com/yjcheng1314/p/4712140.html