windows编程之路一:画钟表

windows编程之路一:画钟表

画表首先要注意的地方是有以下几点:

1:把钟表上面的12个点依次画出来

  在这里我们选择用小圆点, 此时我们需要确定他们的坐标, 也就是  x = 屏幕水平宽度的一半+圆半径*sin(指针和水平方向的夹角), y = 屏幕的高度/2 - 圆半径*cos(指针和水平方向的夹角),接着用for循环,依次画出12个点,在下面的代码中有.

2: 把时分秒针动画出来

  这里实现的原理和上面实现的逻辑是一样的,就是再画一个小圆,可以这样理解.

3: 把时针分针动起来

  我们可以获取系统当前的事件,并设置一个定时器,1s刷新一次界面,这样就可以实现他们的移动.

 

 



//
画表.cpp : 定义应用程序的入口点。 // #include "stdafx.h" #include "画表.h" #include <math.h> #include <time.h> #pragma warning(disable:4996) #define MAX_LOADSTRING 100 #define _CRT_SECURE_NO_WARNINGS // 全局变量: HINSTANCE hInst; // 当前实例 WCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本 WCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名 //==================================================== //自己定义的函数 void drawClockDial(HDC hdc,HWND hwnd); void drawHand(HDC hdc, HWND hWnd);//==================================================== //自己定义的变量 const int WINDOWWIDTH = 400;//窗口的宽度 const int WINDOWHEIGHT = 400;//窗口的高度 const int SCALERADIOUS = 5; //刻度画圆的半径 const double PI = 3.1415926; const int BORDER = 20; //==================================================== // 此代码模块中包含的函数的前向声明: ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); // TODO: 在此放置代码。 // 初始化全局字符串 LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadStringW(hInstance, IDC_MY, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); // 执行应用程序初始化: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY)); MSG msg; // 主消息循环: while (GetMessage(&msg, nullptr, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return (int) msg.wParam; } // // 函数: MyRegisterClass() // // 目的: 注册窗口类。 // ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASSEXW wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MY)); wcex.hCursor = LoadCursor(nullptr, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_GRAYTEXT);//(HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = 0;//MAKEINTRESOURCEW(IDC_MY);//不显示工具栏; wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); return RegisterClassExW(&wcex); } // // 函数: InitInstance(HINSTANCE, int) // // 目的: 保存实例句柄并创建主窗口 // // 注释: // // 在此函数中,我们在全局变量中保存实例句柄并 // 创建和显示主程序窗口。 // BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { hInst = hInstance; // 将实例句柄存储在全局变量中 HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, WINDOWWIDTH, WINDOWHEIGHT, nullptr, nullptr, hInstance, nullptr); //SetTimer(ID_CLOCK_TIMER, 1000, NULL); if (!hWnd) { return FALSE; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE; } // // 函数: WndProc(HWND, UINT, WPARAM, LPARAM) // // 目的: 处理主窗口的消息。 // // WM_COMMAND - 处理应用程序菜单 // WM_PAINT - 绘制主窗口 // WM_DESTROY - 发送退出消息并返回 // // LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_COMMAND: { int wmId = LOWORD(wParam); // 分析菜单选择: switch (wmId) { case IDM_ABOUT: DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); break; case IDM_EXIT: DestroyWindow(hWnd); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } } break; case WM_CREATE: SetTimer(hWnd, 1, 1000, NULL);//设置定时器,在窗口创建的的时候 break; case WM_TIMER: InvalidateRect(hWnd, NULL, TRUE);//没一秒自动触发WM_TIMER,然后调用这个函数窗体重新绘制。 break; case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hWnd, &ps); // TODO: 在此处添加使用 hdc 的任何绘图代码... drawClockDial(hdc,hWnd); drawHand(hdc, hWnd); EndPaint(hWnd, &ps); } break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } // “关于”框的消息处理程序。 INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { UNREFERENCED_PARAMETER(lParam); switch (message) { case WM_INITDIALOG: return (INT_PTR)TRUE; case WM_COMMAND: if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) { EndDialog(hDlg, LOWORD(wParam)); return (INT_PTR)TRUE; } break; } return (INT_PTR)FALSE; } void drawClockDial(HDC hdc,HWND hWnd) { RECT rectClient; GetClientRect(hWnd, &rectClient); POINT point; point.x = (rectClient.right - rectClient.left) / 2; point.y = (rectClient.bottom - rectClient.top) / 2; SelectObject(hdc, CreateSolidBrush(RGB(255, 0, 0))); Ellipse(hdc, point.x + SCALERADIOUS , point.y + SCALERADIOUS, point.x , point.y ); for (int i = 0; i < 12; i++) { DWORD radious = (rectClient.right - rectClient.left) / 2 - BORDER; double a = sin(PI / 6 * i)*radious; double b = cos(PI / 6 * i)*radious; Ellipse(hdc, point.x + a - SCALERADIOUS, point.y - b - SCALERADIOUS, point.x + a, point.y - b ); } } void drawHand(HDC hdc, HWND hWnd) { time_t timep; struct tm *p; time(&timep); /*获得time_t结构的时间,UTC时间*/ p = localtime(&timep); /*转换为struct tm结构的当地时间*/ RECT rectClient; GetClientRect(hWnd, &rectClient); POINT point; point.x = (rectClient.right - rectClient.left) / 2; point.y = (rectClient.bottom - rectClient.top) / 2; float Radious = (point.x - BORDER) ; SelectObject(hdc, CreatePen(0, 10, RGB(0, 255, 255))); //时针 MoveToEx(hdc, point.x + SCALERADIOUS / 2, point.y + SCALERADIOUS / 2, NULL); LineTo(hdc, point.x + Radious/2*sin(PI / 6*p->tm_hour), point.y - Radious/2*cos(PI / 6*p->tm_hour)); //分针 SelectObject(hdc, CreatePen(0, 7, RGB(0, 255, 0))); MoveToEx(hdc, point.x + SCALERADIOUS / 2, point.y + SCALERADIOUS / 2, NULL); LineTo(hdc, point.x + Radious/1.5*sin(PI / 30*p->tm_min), point.y - Radious/1.5*cos(PI / 30*p->tm_min)); //秒针 SelectObject(hdc, CreatePen(0, 5, RGB(0, 0, 255))); MoveToEx(hdc, point.x + SCALERADIOUS / 2, point.y + SCALERADIOUS / 2, NULL); LineTo(hdc, point.x + Radious / 1.2*sin(PI / 180 * p->tm_sec), point.y - Radious / 1.2*cos(PI / 180 * p->tm_sec)); } }

 

windows编程之路一:画钟表

上一篇:win10系统电脑调节屏幕亮度选项无法显示


下一篇:Acwing-----482. 合唱队形