Drawing the dial in the custom coordinate system of win32 window

Keywords: Windows

Drawing the dial in the custom coordinate system of win32 window
Test environment: win7 64 bit vs2013

Create a win32 Application. It is an empty project. Its name is HelloDrawClockFace,
Add the source file HelloDrawClockFace.cpp to your project:

The contents are as follows:

#include <windows.h>
#include <stdlib.h>                                                 
#include <string.h>                                                 
#include <tchar.h>                                                  

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    PSTR szCmdLine, int iCmdShow)
{
    static TCHAR szAppName[] = TEXT("HelloWin");
    HWND         hwnd;
    MSG          msg;
    WNDCLASS     wndclass;

    wndclass.style = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc = WndProc;
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hInstance = hInstance;
    wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName = NULL;
    wndclass.lpszClassName = szAppName;

    if (!RegisterClass(&wndclass))
    {
        MessageBox(NULL, TEXT("This program requires Windows NT!"),
            szAppName, MB_ICONERROR);
        return 0;
    }

    hwnd = CreateWindow(szAppName,                  // window class name
        TEXT("The Hello Program"), // window caption
        WS_OVERLAPPEDWINDOW,        // window style
        CW_USEDEFAULT,              // initial x position
        CW_USEDEFAULT,              // initial y position
        CW_USEDEFAULT,              // initial x size
        CW_USEDEFAULT,              // initial y size
        NULL,                       // parent window handle
        NULL,                       // window menu handle
        hInstance,                  // program instance handle
        NULL);                     // creation parameters

    ShowWindow(hwnd, iCmdShow);
    UpdateWindow(hwnd);

    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC         hdc;
    PAINTSTRUCT ps;

    switch (message)
    {
    case WM_CLOSE:

        DestroyWindow(hwnd);

        break;

    case WM_DESTROY:

        PostQuitMessage(0);

        break;

    case WM_PAINT:{                                             //1

        hdc = BeginPaint(hwnd, &ps);                            //2

        RECT rect;                                              //3

        GetClientRect(hwnd, &rect);                             //4

        int cx = rect.right - rect.left;                        //5

        int cy = rect.bottom - rect.top;                        //6


        SetMapMode(hdc, MM_ISOTROPIC);                          //7

        SetWindowExtEx(hdc, 1000, 1000, NULL);                  //8

        SetViewportExtEx(hdc, cx, -cy, NULL);                   //9

        SetViewportOrgEx(hdc, cx/2, cy/2, NULL);                //10


        static POINT pt[] = {                                   //11
            0,300,                                              //12
            300,0,                                              //13            
            0, -300,                                            //14
            -300, 0,                                            //15
        };                                                      //16

        for (int i = 0; i < 4; i++){                            //17

            Ellipse(hdc, pt[i].x - 20,                          //18
                         pt[i].y + 20,                          //19
                         pt[i].x + 20,                          //20
                         pt[i].y - 20 );                        //21
        }                                                       //22


        EndPaint(hwnd, &ps);                                    //23

        break;                                                  //24
    }                                                           //25

    default:

        return DefWindowProc(hwnd, message, wParam, lParam);

    }

    return 0;
}

The operation effect is as follows:

Coordinate system after setting:

The key point is:

1. Modification of coordinate origin position

2. Modification of x-axis and y-axis directions

3. Mapping between logical dimension and actual dimension

4. The shape of mm? Isotopic pattern graphics will not change, but will enlarge and shrink, and will not stretch or deform

The following picture:

Posted by stewb on Tue, 07 Apr 2020 08:40:06 -0700