Search code examples
winapigdi

Why do view port extent and window extent of a DC keep constant even if I resize the window?


Here is my WindowProc function's code:

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 
{
    PAINTSTRUCT ps;
    switch (uMsg)
    {
        case WM_SIZE:
            {
                RECT rect;
                rect.left = 0;
                rect.right = LOWORD(lParam);
                rect.top = 0;
                rect.bottom = HIWORD(lParam);
                InvalidateRect(hwnd, &rect, true);
            }
        case WM_PAINT:
            {
                int height = 50;
                TCHAR buff[256] = { 0 };
                HDC hdc = BeginPaint(hwnd, &ps);
                SetMapMode(hdc, MM_TEXT);
                SIZE size1;
                GetWindowExtEx(hdc, &size1);
                SIZE size2;
                GetViewportExtEx(hdc, &size2);
                _stprintf_s(buff, TEXT("TEXT MODE:WindowExtent=[%d, %d], ViewportExtent=[%d,%d]"),size1.cx,size1.cy, size2.cx,size2.cy);
                TextOut(hdc,0, height * 0,buff, _tcslen(buff));
                
                
                SetMapMode(hdc, MM_HIENGLISH);
                GetWindowExtEx(hdc, &size1);
                GetViewportExtEx(hdc, &size2);
                memset(buff, 0, 256);
                _stprintf_s(buff, TEXT("HIENGLISH MODE:WindowExtent=[%d, %d], ViewportExtent=[%d,%d]"), size1.cx, size1.cy, size2.cx, size2.cy);
                TextOut(hdc, 0, -height * 1, buff, _tcslen(buff));
                
                
                SetMapMode(hdc, MM_LOENGLISH);
                GetWindowExtEx(hdc, &size1);
                GetViewportExtEx(hdc, &size2);
                memset(buff, 0, 256);
                _stprintf_s(buff, TEXT("LOENGLISH MODE:WindowExtent=[%d, %d], ViewportExtent=[%d,%d]"), size1.cx, size1.cy, size2.cx, size2.cy);
                TextOut(hdc, 0, -height * 2, buff, _tcslen(buff));
                
                
                SetMapMode(hdc, MM_HIMETRIC);
                GetWindowExtEx(hdc, &size1);
                GetViewportExtEx(hdc, &size2);
                memset(buff, 0, 256);
                _stprintf_s(buff, TEXT("HIMETRIC MODE:WindowExtent=[%d, %d], ViewportExtent=[%d,%d]"), size1.cx, size1.cy, size2.cx, size2.cy);
                TextOut(hdc, 0, -height * 3, buff, _tcslen(buff));

                SetMapMode(hdc, MM_LOMETRIC);
                GetWindowExtEx(hdc, &size1);
                GetViewportExtEx(hdc, &size2);
                memset(buff, 0, 256);
                _stprintf_s(buff, TEXT("LOMETRIC MODE:WindowExtent=[%d, %d], ViewportExtent=[%d,%d]"), size1.cx, size1.cy, size2.cx, size2.cy);
                TextOut(hdc, 0,  -height * 4, buff, _tcslen(buff));

                SetMapMode(hdc, MM_TWIPS);
                GetWindowExtEx(hdc, &size1);
                GetViewportExtEx(hdc, &size2);
                memset(buff, 0, 256);
                _stprintf_s(buff, TEXT("TWIPS MODE:WindowExtent=[%d, %d], ViewportExtent=[%d,%d]"), size1.cx, size1.cy, size2.cx, size2.cy);
                TextOut(hdc, 0, -height * 5, buff, _tcslen(buff));

                SetMapMode(hdc, MM_ISOTROPIC);
                GetWindowExtEx(hdc, &size1);
                GetViewportExtEx(hdc, &size2);
                memset(buff, 0, 256);
                _stprintf_s(buff, TEXT("ISOTROPIC MODE:WindowExtent=[%d, %d], ViewportExtent=[%d,%d]"), size1.cx, size1.cy, size2.cx, size2.cy);
                TextOut(hdc, 0,  -height * 6, buff, _tcslen(buff));

                SetMapMode(hdc, MM_ANISOTROPIC);
                GetWindowExtEx(hdc, &size1);
                GetViewportExtEx(hdc, &size2);
                memset(buff, 0, 256);
                _stprintf_s(buff, TEXT("ANISOTROPIC MODE:WindowExtent=[%d, %d], ViewportExtent=[%d,%d]"), size1.cx, size1.cy, size2.cx, size2.cy);
                TextOut(hdc, 0,  -height * 7, buff, _tcslen(buff));
                EndPaint(hwnd, &ps);
                break;
            }
        case WM_DESTROY:
            {
                PostQuitMessage(0);
                break;
            }
    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

It is not complex that I just want to print window extent and view port extent under different mapping mode in the window. But I find that the printed values do not change at all even though I resize the window by dragging the edges of it. Could someone tell me why?


Solution

  • The extents do not change with the window size. For some mapping modes, they depend on the (pixel, or assumed physical) size of the screen, but for example MM_TEXT always has viewport and window extents of 1.

    If what you're actually looking for the height and width of your window (client area), but in logical coordinates, you could try something like this:

    RECT rect;
    GetClientRect(hwnd, &rect);
    SetMapMode(hdc, MM_HIENGLISH);
    DPtoLP(hdc, (PPOINT)&rect, 2);
    SetMapMode(hdc, MM_TEXT);
    

    GetClientRect gets the height and width of the window, in device units. DPtoLP converts those to logical units, in this case MM_HIENGLISH units (thousandths of an inch); the window width is now in rect.right and the height in logical units is in rect.bottom.

    I've added a SetMapMode back to MM_TEXT; you may want to do the same before calling TextOut, or whatever map mode you prefer for that.