c++mfcsdi

Change client area of View


I have MFC SDI application.
It has Ribbon, status bars, properties windows, ...

I need to make client area of a view be x % 16. So I can't adjust entire window, but I need to resize CMyView to be divisible by 16 pixels.

Is there a way to do so?

This code does not work: =(

void CMyView::OnSize(UINT nType, int cx, int cy)
{
    cx -= cx % 16;
    cy -= cy % 16;

    CView::OnSize(nType, cx, cy);

    RECT wr = { 0, 0, cx, cy };
    AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
}

Solution

  • Handling this in WM_SIZE/OnSize is too late because window has already been resized by that time. Use ON_WM_WINDOWPOSCHANGING instead, to monitor changes to window size and apply the following changes:

    void CMyWnd::OnWindowPosChanging(WINDOWPOS* wpos)
    {
        wpos->cx -= wpos->cx % 16;
        wpos->cy -= wpos->cy % 16;
        __super::OnWindowPosChanging(wpos);
    }