windows programming a road: painting watches

Painting table first place to note is the following:

1: The 12 points above the watch in turn drawn

  Here we choose with a small dot, then we need to determine their coordinates, i.e. half + (the angle between the pointer and the horizontal direction) of radius * sin x = horizontal screen width, y = the screen height / 2 - radius * cos (angle between the pointer and the horizontal direction), followed by a for loop, 12 points are sequentially drawn with the following code.

2: The animation division seconds out

  Principle implemented here and logic implementation of the above is the same, that is, and then draw a small circle, it can be understood.

3: The hour and minute hands move

  We can get the current system events, and set a timer, 1s refresh interface, so that you can realize their move.

 

 



//
draw Table .cpp: Defines the entry point of the application. // #include " stdafx.h " #include " painting table .h " #include <math.h> #include <time.h> #pragma warning (disable: 4996) #define MAX_LOADSTRING 100 #define _CRT_SECURE_NO_WARNINGS // globals : HINSTANCE hInst; // current instance WCHAR szTitle [MAX_LOADSTRING]; // title bar text WCHAR szWindowClass [MAX_LOADSTRING]; // main window class name // ================================================== == // own definition of function void drawClockDial (HDC HDC, HWND hwnd); void drawHand (HDC HDC, HWND hWnd); // ==================== ================================ // own definition of the variable const int WINDOWWIDTH = 400 ; // width of the window const int WINDOWHEIGHT = 400 ; // window height const int SCALERADIOUS = . 5 ; // graduated circle radius const Double the PI = 3.1415926 ; const int= BORDER 20 is ; // ============================================ ======== // function code contained in the module forward declaration: the ATOM MyRegisterClass (the hINSTANCE hInstance); BOOL the InitInstance (the hINSTANCE, int ); LRESULT CALLBACK the WndProc (the HWND, UINT, WPARAM, LPARAM); CALLBACK the About INT_PTR (the HWND, UINT, WPARAM, LPARAM); int APIENTRY wWinMain (_In_ the hINSTANCE hInstance, _In_opt_ the hINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int the nCmdShow) { UNREFERENCED_PARAMETER (hPrevInstance); UNREFERENCED_PARAMETER (lpCmdLine); // the TODO: the code is placed here. // initialize global string LoadStringW (hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadStringW (hInstance, IDC_MY, szWindowClass, MAX_LOADSTRING); MyRegisterClass (hInstance); // execute application initialization: IF (! InitInstance (hInstance, nCmdShow)) { return FALSE; } HACCEL hAccelTable = LoadAccelerators (hInstance, the MAKEINTRESOURCE (IDC_MY)); the MSG MSG; // main message loop: the while (the GetMessage (& MSG, nullptr a, 0 , 0 )) { IF ! (the 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 =The LoadIcon (wcex.hInstance, the MAKEINTRESOURCE (IDI_SMALL)); return RegisterClassExW (& wCEX); } // // Function: the InitInstance (the HINSTANCE, int) // // Objective: to save instance handle and creates the main window // // NOTE : // // in this function, we save instance handle in a global variable and // create and display the main program window. // BOOL the InitInstance (the HINSTANCE hInstance, int the nCmdShow) { hInst = hInstance; // instance handle is stored in the global variable in the HWND the hWnd = CreateWindowW (szWindowClass, szTitle, the WS_OVERLAPPEDWINDOW, to CW_USEDEFAULT, 0, WINDOWWIDTH, WINDOWHEIGHT, nullptr a, nullptr a, hInstance, nullptr a); // the SetTimer (ID_CLOCK_TIMER, 1000, NULL); IF (! The hWnd) { return FALSE; } the ShowWindow (the hWnd, the nCmdShow); the UpdateWindow (the hWnd); return TRUE; } // // function: the WndProc (the HWND, UINT, WPARAM, LPARAM) // // Objective: the main window message processing. // // the WM_COMMAND - processing application menu // the WM_PAINT - Draw main window // the WM_DESTROY - sending an exit message and returns // // LRESULT CALLBACK the WndProc (the HWND the hWnd, UINT Message, WPARAM the wParam, LPARAM the 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 the WM_CREATE: SetTimer (hWnd,1 , 1000 , NULL); // set the timer when the window is created BREAK ; Case WM_TIMER: InvalidateRect (hWnd, NULL, TRUE); // not automatically trigger one second WM_TIMER, and then call this function form redrawn. BREAK ; Case the WM_PAINT: { the PAINTSTRUCT PS; the HDC hdc = the BeginPaint (the hWnd, & PS); // the TODO: hdc added using any drawing code here ... drawClockDial (hdc, the hWnd); drawHand (hdc, the hWnd) ; of EndPaint (the hWnd, & PS); } BREAK ; Case the WM_DESTROY: the 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 the HDC, the HWND the hWnd) { time_t timep; struct (TM) * P; Time ( & timep); / * get structure time_t time, UTC time * / P = localtime (& timep); / * converted to local structure struct tm * / the RECT rectClient; the GetClientRect (the hWnd, & rectClient); the 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)); } }

 

Guess you like

Origin www.cnblogs.com/jianmoxiansheng-Guo/p/12578099.html