学习windows编程 day4 之 自定义映射

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;
    RECT rect;
    int iMapMode;
    static int cxClient, cyClient;
    switch (message)
    {
    case WM_SIZE:
        cxClient = LOWORD(lParam);
        cyClient = HIWORD(lParam);
        break;
    case WM_PAINT:
        hdc = BeginPaint(hwnd, &ps);
        GetClientRect(hwnd, &rect);
//创建自定义映射
        
        iMapMode = GetMapMode(hdc);
        SetMapMode(hdc, MM_ISOTROPIC);
        SetWindowExtEx(hdc, 100, 100, NULL);    
        //相当于取出原来逻辑窗口的100*100像素放大为现在视口的1/2*1/2(1/4) 所以现在视口所显示的最大尺寸为200*200
        SetViewportExtEx(hdc, cxClient / 2, cyClient / 2, NULL);
        //设置视口原点在中心
        SetViewportOrgEx(hdc, cxClient / 2, cyClient / 2, NULL);

        //windows为了保证按比例将整个视口落在视图中,会将多余的尺寸排除在外,若是有背景,则多余为黑色(会被裁剪掉),会以最大比例占据窗口(前提是和原来设置和视口和窗口之比不变的前提下)
        Rectangle(hdc, -100, -100, 100, 100);
        Ellipse(hdc, -80, -80, 80, 80);

        EndPaint(hwnd, &ps);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }


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

相关推荐