FIX: Resizing CToolbar with Dropdown Arrow Buttons Freezes Apps (190501)
The information in this article applies to:
- The Microsoft Foundation Classes (MFC), when used with:
- Microsoft Visual C++, 32-bit Enterprise Edition 6.0
- Microsoft Visual C++, 32-bit Professional Edition 6.0
- Microsoft Visual C++, 32-bit Learning Edition 6.0
This article was previously published under Q190501 SYMPTOMS
Resizing a toolbar (CToolBar class) with drop-down arrow buttons causes an
infinite loop.
CAUSE
The application is looping in CToolBar::SizeToolBar() because neither
sizeMin.cx nor sizeMax.cx changes its value. In other words, in
CToolBar::SizeToolBar(), the loop hangs on the case where sizeMax ==
sizeMid.
RESOLUTION
Because CToolBar::SizeToolBar() is not a virtual function, we need to
reimplement this function and override the virtual functions in CToolBar
class to call our SizeToolBar() function.
Below are the steps to correct this problem:
- Derive a class from CToolBar called CMyToolBar.
- Override virtual functions: CalcFixedLayout() and CalcDynamicLayout() in
CMyToolBar class because they both call CalcLayout(), which in turn
calls SizeToolBar(). Both CalcLayout() and SizeToolBar are nonvirtual
functions in CToolBar class.
The code for all these functions can be copied from the MFC source file
Bartool.cpp. - Override CalcLayout() in CMyToolBar class. The code in
CToolBar::CalcLayout() requires the _AFX_CONTROLPOS structure, which
should be copied from the Bartool.cpp file as well.
The code in CToolBar::CalcLayout() also calls the _GetButton() and
_SetButton() functions. These functions have internal linkage, so we
need to override those to avoid the LNK2001 error.
NOTE: The first statement of _GetButton() casts the "this" pointer to
CToolBar*, we have to cast it to CMyToolBar* in order to call the
protected CWnd::DefWindowProc() function. For example:
void CMyToolBar::_GetButton(int nIndex, TBBUTTON* pButton) const
{
CMyToolBar* pBar = (CMyToolBar*) this;
VERIFY(pBar->DefWindowProc(TB_GETBUTTON, nIndex,
(LPARAM)pButton));
// TBSTATE_ENABLED == TBBS_DISABLED so invert it.
pButton->fsState ^= TBSTATE_ENABLED;
}
- Override SizeToolBar() in CMyToolBar class. Replace the while-loop with
the following fix-up code:
while (sizeMin.cx < sizeMax.cx)
{
sizeMid.cx = (sizeMin.cx + sizeMax.cx) / 2;
WrapToolBar(pData, nCount, sizeMid.cx);
sizeMid = CalcSize(pData, nCount);
if (nLength < sizeMid.cy)
{
if (sizeMin == sizeMid)
{
WrapToolBar(pData, nCount, sizeMax.cx);
return;
}
sizeMin = sizeMid;
}
else if (nLength > sizeMid.cy && sizeMax != sizeMid)
sizeMax = sizeMid;
else
return;
}
- Finally, use CMyToolBar when declaring the toolbar object in the header
file of the CMainFrame class.
STATUS
Microsoft has confirmed this to be a bug in the Microsoft products listed
at the beginning of this article.
This problem was corrected in Microsoft Visual C++ .NET.
Modification Type: | Major | Last Reviewed: | 2/24/2004 |
---|
Keywords: | kbBug kbfix kbNoUpdate KB190501 |
---|
|