0

在我的代码无法正常工作之前,我遇到过类似的情况,在我寻求解决问题的过程中,我做了一些更改,而不是评论这些更改并解决了问题。突然之间,文件中某处的“编辑”已经解决了问题,但代码没有真正的变化。我又遇到了类似的问题,我只是想知道这是怎么发生的?

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);

        }

    }

}

此类/函数根据对话框大小调整控件的大小。它在调试版本中工作,但相同的代码在发布版本中不起作用(=正确调整大小)。我进行了更改并在上面的循环中为 TRACE 函数添加了三行。它也开始在发布版本中正常工作。比我评论这些行,它仍然适用于发布版本。我删除了它们,它不再在发布版本中工作。我必须让这些行刚刚被评论,以便发布版本做正确的事情。这怎么可能是正当的?什么可能真正导致文件的这种“编辑”,而实际代码中实际上没有任何变化可以解决问题?

我还想补充一点,我已经尝试过“编辑”或在文件中的其他地方注释了新代码,但不一定能解决问题。我只需要在上述函数中添加注释代码即可修复它。

更新这是完整的课程。我必须说这门课在网络上的某个地方是免费的,我不是原作者。

  • InitOnInitDialog从需要调整大小的对话框中调用。它只存储所有控件的坐标。
  • OnSize() 实际上会调整大小。

    CDlgResizeHelper::CDlgResizeHelper() { }

    无效 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);

}

}

无效 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;

}

4

1 回答 1

1

这听起来像您在某处使用了未初始化的变量。它可以根据构建模式获得不同的值,并且恰好在调试中分配了一些无害的东西。

尝试针对您的代码运行 CppCheck 应用程序。反正不会痛。

于 2012-12-21T19:29:35.497 回答