Search code examples
visual-c++mfcdialogscrollbar

How can I stop having white background after switching off scroll bars in CDialog?


My dialog (in resource editor) has the resizing and maximize/minimize switched on.

But, if I detect that I don't need this functionality because their display is of a sufficient size I want to switch them off.

So I did this:

int CResizingDialog::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    const CDC* pDC = GetDC();
    if (pDC != nullptr)
    {
        const int iHorz = pDC->GetDeviceCaps(HORZRES);
        const int iVert = pDC->GetDeviceCaps(VERTRES);

        bool showHorz = true;
        bool showVert = true;

        if (lpCreateStruct->cx < iHorz)
        {
            // Remove the WS_HSCROLL style to disable the horizontal scroll bar
            ModifyStyle(WS_HSCROLL, 0);
            showHorz = false;
        }

        if (lpCreateStruct->cy < iVert)
        {
            // Remove the WS_VSCROLL style to disable the vertical scroll bar
            ModifyStyle(WS_VSCROLL, 0);
            showVert = false;
        }

        if (!showHorz && !showVert && m_bModalWindow)
        {
            // Remove the WS_THICKFRAME style rtc to disable resizing
            ModifyStyle(WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX, 0);
        }
    }

    if (__super::OnCreate(lpCreateStruct) == -1)
        return -1;

    return 0;
}

It works, but look:

enter image description here

I now have two white rectangles on the right / bottom where I switched off the scroll bars. How do we address this?


Solution

  • I needed to make a call to SetWindowPos, like this:

    int CResizingDialog::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
        const CDC* pDC = GetDC();
        if (pDC != nullptr)
        {
            const auto style = GetStyle();
            const int iHorz = pDC->GetDeviceCaps(HORZRES);
            const int iVert = pDC->GetDeviceCaps(VERTRES);
    
            bool showHorz{true};
            bool showVert{true};
            bool redrawWindow{false};
    
            bool hasHorzScrollBar{ false };
            if (style & WS_HSCROLL)
            {
                hasHorzScrollBar = true;
                // Do we need to switch off the horizontal scroll?
                if (lpCreateStruct->cx < iHorz)
                {
                    // Remove the WS_HSCROLL style to disable the horizontal scroll bar
                    ModifyStyle(WS_HSCROLL, 0);
                    showHorz = false;
                    redrawWindow = true;
                }
            }
    
            bool hasVertScrollBar{ false };
            if (style & WS_VSCROLL)
            {
                hasVertScrollBar = true;
                // Do we need to switch off the vertical scroll?
                if (lpCreateStruct->cy < iVert)
                {
                    // Remove the WS_VSCROLL style to disable the vertical scroll bar
                    ModifyStyle(WS_VSCROLL, 0);
                    showVert = false;
                    redrawWindow = true;
                }
            }
    
            if ((hasHorzScrollBar && !showHorz) &&
                (hasVertScrollBar && !showVert) && m_bModalWindow)
            {
                // Remove the WS_THICKFRAME style etc to disable resizing
                ModifyStyle(WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX, WS_DLGFRAME);
                redrawWindow = true;
            }
    
            // Apply new frame styles (important for cache update)
            if (redrawWindow)
            {
                // SM_CXSIZEFRAME thickness horizontal
                // SM_CYSIZEFRAME thickness vertical
                // SM_CXVSCROLL thickness vertical
                // SM_CYHSCROLL thickness horizontal
    
                int scrollVert{ 0 };
                if (hasVertScrollBar && !showVert)
                {
                    scrollVert = GetSystemMetrics(SM_CXVSCROLL);
                }
    
                int scrollHorz{ 0 };
                if (hasHorzScrollBar && !showHorz)
                {
                    scrollHorz = GetSystemMetrics(SM_CYHSCROLL);
                }
    
                SetWindowPos(nullptr,
                    lpCreateStruct->x,
                    lpCreateStruct->y,
                    lpCreateStruct->cx - scrollVert,
                    lpCreateStruct->cy - scrollHorz, SWP_NOMOVE | SWP_NOZORDER | SWP_FRAMECHANGED);
            }
        }
    
        if (__super::OnCreate(lpCreateStruct) == -1)
            return -1;
    
        return 0;
    }