VC++SDK编程——模拟时钟
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 }
↑
界面太丑了,后来代码又被我改了:
↓
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 HFONT CreateMyFont(TCHAR * fontName, int height, int width); 11 void AdjustTime(TimeStructure *x); 12 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 13 { 14 MSG msg; 15 if (!InitWindowClass(hInstance, nCmdShow)) 16 { 17 MessageBox(NULL, L"创建窗口失败!", _T("创建窗口"), NULL); 18 return 1; 19 } 20 while (GetMessage(&msg, NULL, 0, 0)) 21 { 22 TranslateMessage(&msg); 23 DispatchMessage(&msg); 24 } 25 return(int)msg.wParam; 26 } 27 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 28 { 29 HDC hDC; 30 PAINTSTRUCT ps; 31 HFONT font; 32 HBRUSH hBrush; 33 HPEN hPen; 34 LPCTSTR num[12] = { L"12", L"1", L"2", L"3", L"4", L"5", L"6", L"7", L"8", L"9", L"10", L"11" }; 35 RECT clientRect; 36 SYSTEMTIME sys; 37 GetLocalTime(&sys); 38 static TimeStructure x = { sys.wHour, sys.wMinute, sys.wSecond };//获取系统当前时间 39 float sita = 0; 40 int xOrg, yOrg, rSec, rMin, rHour, rClock, xBegin, xEnd, yBegin, yEnd; 41 switch (message) 42 { 43 case WM_CREATE: //创建窗口时,响应的消息 44 SetTimer(hWnd, 9999, 1000, NULL); //设置定时器 45 break; 46 case WM_PAINT: 47 x.sec++; 48 AdjustTime(&x); 49 hDC = BeginPaint(hWnd, &ps); 50 GetClientRect(hWnd, &clientRect); //获得用户区的尺寸 51 hPen = (HPEN)GetStockObject(BLACK_PEN); //设置画笔为系统预定义的黑色画笔 52 SelectObject(hDC, hPen); //选择画笔 53 xOrg = (clientRect.left + clientRect.right) / 2; 54 yOrg = (clientRect.top + clientRect.bottom) / 2; //计算屏幕中心的坐标,它也是时钟的中心 55 rClock = min(xOrg, yOrg) - 50; //钟表的半径 56 rSec = rClock * 6 / 7; //秒针的半径 57 rMin = rClock * 5 / 6; //分针的半径 58 rHour = rClock * 2 / 3; //时针的半径 59 hBrush = CreateSolidBrush(RGB(0, 0, 0)); //创建黑色的单色画刷 60 SelectObject(hDC, hBrush); //选择画刷 61 Ellipse(hDC, xOrg - rClock - 20, yOrg - rClock - 20, xOrg + rClock + 20, yOrg + rClock + 20); 62 hBrush = CreateSolidBrush(RGB(204, 204, 255)); //创建长春花色的单色画刷 63 SelectObject(hDC, hBrush); //选择画刷 64 Ellipse(hDC, xOrg - rClock - 5, yOrg - rClock - 5, xOrg + rClock + 5, yOrg + rClock + 5); 65 for (int i = 0; i < 60; i++) //绘制表面的刻度 66 { 67 if (i % 5) //绘制表面表面的整点刻度 68 { 69 hPen = CreatePen(PS_SOLID, 2, RGB(0, 0, 0)); 70 SelectObject(hDC, hPen); 71 xBegin = xOrg + rClock*sin(2 * 3.1415926*i / 60); 72 yBegin = yOrg + rClock*cos(2 * 3.1415926*i / 60); 73 MoveToEx(hDC, xBegin, yBegin, NULL); 74 xEnd = xOrg + (rClock - 20)*sin(2 * 3.1415926*i / 60); 75 yEnd = yOrg + (rClock - 20)*cos(2 * 3.1415926*i / 60); 76 } 77 else //绘制表面表面的非整点刻度 78 { 79 hPen = CreatePen(PS_SOLID, 5, RGB(0, 0, 0)); 80 SelectObject(hDC, hPen); 81 xBegin = xOrg + rClock*sin(2 * 3.1415926*i / 60); 82 yBegin = yOrg + rClock*cos(2 * 3.1415926*i / 60); 83 MoveToEx(hDC, xBegin, yBegin, NULL); 84 xEnd = xOrg + (rClock - 25)*sin(2 * 3.1415926*i / 60); 85 yEnd = yOrg + (rClock - 25)*cos(2 * 3.1415926*i / 60); 86 } 87 LineTo(hDC, xEnd, yEnd); 88 if (i % 5 == 0) 89 { 90 int j = 18 - i / 5; 91 LPCTSTR outInfo = num[j % 12]; 92 font = CreateMyFont(L"楷体_GB2312", 24, 12); 93 SelectObject(hDC, font); 94 SetBkColor(hDC, RGB(204, 204, 255)); 95 if (j % 13 / 10 == 0) 96 TextOut(hDC, xOrg + (rClock - 33)*sin(2 * 3.1415926*i / 60) - 6, yOrg + (rClock - 33)*cos(2 * 3.1415926*i / 60) - 12, outInfo, 1); 97 else 98 TextOut(hDC, xOrg + (rClock - 33)*sin(2 * 3.1415926*i / 60) - 12, yOrg + (rClock - 33)*cos(2 * 3.1415926*i / 60) - 12, outInfo, 2); 99 DeleteObject(font); 100 } 101 DeleteObject(hPen); 102 } 103 hPen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0)); 104 SelectObject(hDC, hPen); 105 sita = 2 * 3.1415926*x.sec / 60; 106 xBegin = xOrg + (int)(rSec*sin(sita)); 107 yBegin = yOrg - (int)(rSec*cos(sita)); //秒针的起点,它的位置在秒针的最末端 108 xEnd = xOrg + (int)(rClock*sin(sita + 3.1415926) / 8); 109 yEnd = yOrg - (int)(rClock*cos(sita + 3.1415926) / 8);//秒针的终点,它的位置在秒针的反方向的长度为秒针的1/8 110 MoveToEx(hDC, xBegin, yBegin, NULL); 111 LineTo(hDC, xEnd, yEnd); //绘制秒针 112 hPen = CreatePen(PS_SOLID, 5, RGB(0, 0, 0)); 113 SelectObject(hDC, hPen); 114 sita = 2 * 3.1415926*x.min / 60; 115 xBegin = xOrg + (int)(rMin*sin(sita)); 116 yBegin = yOrg - (int)(rMin*cos(sita)); //分针的起点 117 xEnd = xOrg + (int)(rClock*sin(sita + 3.1415926) / 8); 118 yEnd = yOrg - (int)(rClock*cos(sita + 3.1415926) / 8);//分针的终点 119 MoveToEx(hDC, xBegin, yBegin, NULL); 120 LineTo(hDC, xEnd, yEnd); //绘制分针 121 hPen = CreatePen(PS_SOLID, 10, RGB(0, 0, 0)); 122 SelectObject(hDC, hPen); 123 sita = 2 * 3.1415926*x.hour / 12; 124 xBegin = xOrg + (int)(rHour*sin(sita)); 125 yBegin = yOrg - (int)(rHour*cos(sita)); 126 xEnd = xOrg + (int)(rClock*sin(sita + 3.1415926) / 8); 127 yEnd = yOrg - (int)(rClock*cos(sita + 3.1415926) / 8); 128 MoveToEx(hDC, xBegin, yBegin, NULL); 129 LineTo(hDC, xEnd, yEnd); //绘制时针 130 DeleteObject(hPen); 131 DeleteObject(hBrush); 132 EndPaint(hWnd, &ps); //结束绘图 133 break; 134 case WM_TIMER: //响应定时器发出的定时消息 135 if (wParam == 9999) //判断是否是设置的定时器发出的消息 136 InvalidateRect(hWnd, NULL, true); //刷新屏幕 137 break; 138 case WM_SIZE: //窗口尺寸改变时,刷新窗口 139 x = { sys.wHour, sys.wMinute, sys.wSecond }; 140 InvalidateRect(hWnd, NULL, true); 141 break; 142 case WM_DESTROY: 143 PostQuitMessage(0); //调用PostQuitMessage发出WM_QUIT消息 144 break; 145 default: 146 return DefWindowProc(hWnd, message, wParam, lParam); //默认时采用系统消息默认处理函数 147 break; 148 } 149 return 0; 150 } 151 void AdjustTime(TimeStructure *x) 152 { 153 if (x->sec == 60) 154 { 155 x->sec = 0; 156 x->min++; 157 if (x->min == 60) 158 { 159 x->min = 0; 160 x->hour++; 161 if (x->hour == 12) 162 x->hour = 0; 163 } 164 } 165 } 166 HFONT CreateMyFont(TCHAR * fontName, int height, int width) 167 { 168 return CreateFont( 169 height, 170 width, 171 0, 172 0, 173 FW_HEAVY, 174 0, 175 0, 176 0, 177 GB2312_CHARSET, 178 OUT_DEFAULT_PRECIS, 179 CLIP_DEFAULT_PRECIS, 180 DEFAULT_QUALITY, 181 DEFAULT_PITCH | FF_DONTCARE, 182 fontName 183 ); 184 } 185 BOOLEAN InitWindowClass(HINSTANCE hInstance, int nCmdShow) 186 { 187 WNDCLASSEX wcex; 188 HWND hWnd; 189 TCHAR szWindowClass[] = L"窗口示例"; 190 TCHAR szTitle[] = L"模拟时钟"; 191 wcex.cbSize = sizeof(WNDCLASSEX); 192 wcex.style = 0; 193 wcex.lpfnWndProc = WndProc; 194 wcex.cbClsExtra = 0; 195 wcex.cbWndExtra = 0; 196 wcex.hInstance = hInstance; 197 wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION)); 198 wcex.hCursor = LoadCursor(NULL, IDC_ARROW); 199 wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); 200 wcex.lpszMenuName = NULL; 201 wcex.lpszClassName = szWindowClass; 202 wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION)); 203 if (!RegisterClassEx(&wcex)) 204 return FALSE; 205 hWnd = CreateWindow( 206 szWindowClass, 207 szTitle, 208 WS_OVERLAPPEDWINDOW, 209 CW_USEDEFAULT, CW_USEDEFAULT, 210 CW_USEDEFAULT, CW_USEDEFAULT, 211 NULL, 212 NULL, 213 hInstance, 214 NULL 215 ); 216 if (!hWnd) 217 return FALSE; 218 ShowWindow(hWnd, nCmdShow); 219 UpdateWindow(hWnd); 220 return TRUE; 221 }