0
votes

I have run into similar situations before when my code is not working properly and on my quest to solve the problem I made some changes than comment those changes and boom it fixed the problem. All of sudden just a an 'edit' in the file somewhere has fixed the issue but there was no real change in the code. I ran into similar problem again and I am just wondering how does this happen?

void CDlgResizeHelper::Init(HWND hparent) 
{
    m_hParent = hparent;
    m_CtrlsList.clear();


    if (::IsWindow(m_hParent)) 
    {
        ::GetWindowRect(m_hParent, m_OrigParentSize);

        // get all child windows and store their original sizes and positions
        HWND hCtrl = ::GetTopWindow(m_hParent);

        while (hCtrl) 
        {
            CtrlSize cs;
            cs.hctrl = hCtrl;

            ::GetWindowRect(hCtrl, cs.orig_size);
            ::ScreenToClient(m_hParent, &cs.orig_size.TopLeft());
            ::ScreenToClient(m_hParent, &cs.orig_size.BottomRight());


        //  CString msg;
        //  msg.Format("Old Size: %d        %d      %d      %d\r\n", cs.orig_size.left, cs.orig_size.top, cs.orig_size.right, cs.orig_size.bottom );

        //  TRACE( msg );

            m_CtrlsList.push_back(cs);

            hCtrl = ::GetNextWindow(hCtrl, GW_HWNDNEXT);

        }

    }

}

This class/function resizes controls based on the dialog size. It was working in debug version but the same code doesn't work (=resize properly) in release version. I made changes and added the three lines in the loop above for TRACE function. It starts to work properly in release version as well. Than I commented these lines, it still works in release build. I removed them and it doesn't work in release build anymore. I have to have these lines just commented present for release build to do the right thing. How can this be justified? What could really cause this 'edit' of file which is really no change in the real code fix the problem?

I also want to add that I have tried 'edit' or commented new code else where in the file that doesn't necessarily fixes the problem. I only need to have the commented code in the above function that would fix it.

Update Here is the complete class. I must say this class is available free somewhere on web and I am not the original author.

  • Init is called from the OnInitDialog of the dialog which needs resizing. It just stores the coordinates of all controls.
  • OnSize() actually does the resizing.

    CDlgResizeHelper::CDlgResizeHelper() { }

    void CDlgResizeHelper::Init(HWND hparent) { m_hParent = hparent; m_CtrlsList.clear();

    if (::IsWindow(m_hParent)) 
    {
        ::GetWindowRect(m_hParent, m_OrigParentSize);
    
        // get all child windows and store their original sizes and positions
        HWND hCtrl = ::GetTopWindow(m_hParent);
    
        while (hCtrl) 
        {
            CtrlSize cs;
            cs.hctrl = hCtrl;
    
            ::GetWindowRect(hCtrl, cs.orig_size);
            ::ScreenToClient(m_hParent, &cs.orig_size.TopLeft());
            ::ScreenToClient(m_hParent, &cs.orig_size.BottomRight());
    
    
            CString msg;
            msg.Format("Old Size: %d        %d      %d      %d\r\n", cs.orig_size.left, cs.orig_size.top, cs.orig_size.right, cs.orig_size.bottom );
    
            Sleep( 50 );
    
            m_CtrlsList.push_back(cs);
    
            hCtrl = ::GetNextWindow(hCtrl, GW_HWNDNEXT);
    
        }
    
    }
    

    }

    void CDlgResizeHelper::Remove(HWND hwnd) { CtrlList::iterator it;

    for (it = m_CtrlsList.begin(); it != m_CtrlsList.end(); ++it) 
    {
        if (it->hctrl == hwnd)
        {
            m_CtrlsList.erase(it);
            return;
        }
    
    }
    

    }

    void CDlgResizeHelper::Update(HWND hwnd) { if (m_hParent && hwnd) { CtrlList::iterator it;

        for (it = m_CtrlsList.begin(); it != m_CtrlsList.end(); ++it) 
        {
            if (it->hctrl == hwnd)
            {
                ::GetWindowRect(hwnd, &(it->orig_size));
                ::ScreenToClient(m_hParent, &(it->orig_size.TopLeft()));
                ::ScreenToClient(m_hParent, &(it->orig_size.BottomRight()));
            }
    
        }
    
    
    }
    

}

void CDlgResizeHelper::Add(HWND hwnd) { if (m_hParent && hwnd) { CtrlSize cs; cs.hctrl = hwnd;

    ::GetWindowRect(hwnd, cs.orig_size);
    ::ScreenToClient(m_hParent, &cs.orig_size.TopLeft());
    ::ScreenToClient(m_hParent, &cs.orig_size.BottomRight());

    m_CtrlsList.push_back(cs);

}

}

void CDlgResizeHelper::OnSize() { if (::IsWindow(m_hParent)) { CRect currparentsize; ::GetWindowRect(m_hParent, currparentsize);

    double xratio = ((double) currparentsize.Width()) / m_OrigParentSize.Width();
    double yratio = ((double) currparentsize.Height()) / m_OrigParentSize.Height();

    HDWP hdwp; 
    hdwp = BeginDeferWindowPos((int)m_CtrlsList.size()); 

    // resize child windows according to their fix attributes
    CtrlList::const_iterator it;

    for (it = m_CtrlsList.begin(); it != m_CtrlsList.end(); ++it) 
    {
        CRect currctrlsize;
        HorizFix horiz_fix = it->horiz_fix;
        VertFix vert_fix = it->vert_fix;

        if (horiz_fix & LEFT) 
            currctrlsize.left = it->orig_size.left;
        else 
            currctrlsize.left = (LONG)( ((horiz_fix & WIDTH) && (horiz_fix & RIGHT)) ? (it->orig_size.left + currparentsize.Width() - m_OrigParentSize.Width()) : (it->orig_size.left * xratio));

        if (horiz_fix & RIGHT) 
            currctrlsize.right = it->orig_size.right + currparentsize.Width() - m_OrigParentSize.Width();
        else 
            currctrlsize.right = (LONG)((horiz_fix & WIDTH) ? (currctrlsize.left + it->orig_size.Width()) : (it->orig_size.right * xratio));

        if (vert_fix & TOP) 
            currctrlsize.top = it->orig_size.top;
        else 
            currctrlsize.top = (LONG)(((vert_fix & HEIGHT) && (vert_fix & BOTTOM)) ? (it->orig_size.top + currparentsize.Height() - m_OrigParentSize.Height()) : (it->orig_size.top * yratio));

        if (vert_fix & BOTTOM) 
            currctrlsize.bottom = it->orig_size.bottom + currparentsize.Height() - m_OrigParentSize.Height();
        else 
            currctrlsize.bottom = (LONG)((vert_fix & HEIGHT) ? (currctrlsize.top + it->orig_size.Height()) : (it->orig_size.bottom * yratio));

        UINT flags = SWP_NOZORDER;

        if (! it->resize)
            flags |= SWP_NOSIZE;


        hdwp = ::DeferWindowPos(hdwp, it->hctrl, NULL, currctrlsize.left, currctrlsize.top, (it->resize)? currctrlsize.Width() : 0, (it->resize)? currctrlsize.Height() : 0, flags); 

        if (hdwp == NULL)
            return;

    } //end for (it = m_CtrlsList.begin(); it != m_CtrlsList.end(); ++it) 

    EndDeferWindowPos(hdwp);

} //end if (::IsWindow(m_hParent)) 

}

BOOL CDlgResizeHelper::Fix(HWND a_hCtrl, HorizFix a_hFix, VertFix a_vFix, bool resize /= true/) { CtrlList::iterator it;

for (it = m_CtrlsList.begin(); it != m_CtrlsList.end(); ++it) 
{
    if (it->hctrl == a_hCtrl) 
    {
        it->horiz_fix = a_hFix;
        it->vert_fix = a_vFix;
        it->resize = resize;

        return TRUE;

    }

}

return FALSE;

}

BOOL CDlgResizeHelper::Fix(int a_itemId, HorizFix a_hFix, VertFix a_vFix, bool resize /= true/) { return Fix(::GetDlgItem(m_hParent, a_itemId), a_hFix, a_vFix, resize); }

BOOL CDlgResizeHelper::Fix(HorizFix a_hFix, VertFix a_vFix, bool resize /= true/) { CtrlList::iterator it;

for(it = m_CtrlsList.begin(); it!=m_CtrlsList.end(); ++it) 
{
    it->horiz_fix = a_hFix;
    it->vert_fix = a_vFix;
    it->resize = resize;
}

return TRUE;

}

UINT CDlgResizeHelper::Fix(LPCTSTR a_pszClassName, HorizFix a_hFix, VertFix a_vFix, bool resize /= true/) { char cn_buf[200];
memset(cn_buf, 0, 200);

UINT cnt = 0;
CtrlList::iterator it;

for (it = m_CtrlsList.begin(); it!= m_CtrlsList.end(); ++it) 
{
    ::GetClassName(it->hctrl, cn_buf, sizeof(cn_buf));

    if (strcmp(cn_buf, a_pszClassName) == 0) 
    {
        cnt++;
        it->horiz_fix = a_hFix;
        it->vert_fix = a_vFix;
        it->resize = resize;
    }

}

return cnt;

}

1
Sometimes VS uses old objects files and small changes can force it to update the files.andre
@ahenderson I often do 'clean solution' and start fresh build to make sure that is not the case and I have ruled that out.zar
You never know until you find and fix the problem. It can be any number of things and you won't know until you work it through.David Schwartz
I wonder if it's a timing issue? Perhaps a small delay is the difference and the debug and TRACE both do it because debug is slower and TRACE incurs a delay. It could even be that running something before does it.RonaldBarzell
+1 @RonaldBarzell That's plausible but it's hard to justify since we are not doing any multithreading and time critical operation.zar

1 Answers

1
votes

This sounds like you have uninitialized variable used somewhere. It can get different values depending on build mode and it just so happens that it is assigned something harmless in debug.

Try running CppCheck application against your code. Won't hurt anyway.