当代码在没有真正更改的情况下意外地开始工作时
When code accidently starts to work without a real change
我以前遇到过类似的情况,当我的代码不能正常工作时,为了解决问题,我做了一些更改,而不是注释这些更改,然后它就解决了问题。突然间,只是在文件的某个地方进行了一次"编辑",解决了这个问题,但代码并没有真正的变化。我又遇到了类似的问题,我只是想知道这是怎么发生的?
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 %drn", 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函数循环中添加了三行代码。它在发布版本中也开始正常工作。然后我注释了这些行,它仍然可以在发布版本中工作。我删除了它们,它在发布版本中不再工作了。我必须有这些行刚刚注释现在发布构建做正确的事情。这怎么能站得住脚呢?什么可能真正导致这个"编辑"的文件,这是真的没有改变,在实际的代码解决问题?我还想补充说,我已经尝试过"编辑"或注释新代码,否则在文件中不一定能解决问题。我只需要在上面的函数中有注释的代码来修复它。
Update这是完整的类。我必须说这门课在网上是免费的,我不是原作者。
-
Init
从需要调整大小的对话框的OnInitDialog
调用。它只是存储所有控件的坐标。 OnSize()实际上会调整大小。
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 %drn", 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,
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,
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。
::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 (){如果(::IsWindow (m_hParent)){绘图用的矩形类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,
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(horizonfix a_hFix, VertFix a_vFix, BOOL resize/= true/){CtrlList:: iterator,
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;
}
这听起来像是在某个地方使用了未初始化的变量。它可以根据构建模式获得不同的值,而且碰巧在调试时它被分配了一些无害的值。
尝试运行CppCheck应用程序对您的代码。反正也没关系。
- 在没有太多条件句的情况下,我如何避免被零除
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 在未初始化映射的情况下,将值插入到映射的映射中
- 是默认情况下分配给char数组常量的值
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- 如何在不产生任何垃圾的情况下获得C中的像素
- 在已经使用Git的情况下减少编译时间
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- 如何在没有信号的情况下从C++执行QML插槽
- 如何在不知道向量大小的情况下输入向量内部的向量?
- 为什么在某些情况下不写入此文件?
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 在没有Xcode的情况下在Mac捆绑包中嵌入框架
- UE4-如何在给定4个屏幕坐标的情况下缩放纹理或材质
- 为什么需要复制构造函数,在哪些情况下它们非常有用
- 在C++中如何在没有pow的情况下进行基础计算
- 松弛原子与无同步情况下的记忆连贯性
- 虚拟继承情况下类的意外大小
- 在任何情况下都会出现意外输出.我的解决方案出了什么问题
- 当代码在没有真正更改的情况下意外地开始工作时