Microsoft KB Archive/190501

= FIX: Resizing CToolbar with Dropdown Arrow Buttons Freezes Apps =

Article ID: 190501

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 6.0 Enterprise Edition

 Microsoft Visual C++ 6.0 Professional Edition

 Microsoft Visual C++ 6.0 Standard Edition 

-

<div class="notice_section">

This article was previously published under Q190501

<div class="symptoms_section">

SYMPTOMS
Resizing a toolbar (CToolBar class) with drop-down arrow buttons causes an infinite loop.

<div class="cause_section">

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.

<div class="resolution_section">

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:

<ol> Derive a class from CToolBar called CMyToolBar.</li> 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.</li>  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; }                       </li>  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; }                       </li> Finally, use CMyToolBar when declaring the toolbar object in the header file of the CMainFrame class.</li></ol>

<div class="status_section">

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.

<div class="moreinformation_section">

MORE INFORMATION
In CToolBar::SizeToolBar, the loop hangs on the case where sizeMax == sizeMid: 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; else return; }

Steps to Reproduce Behavior
<ol> Run MFC AppWizard to create an SDI .EXE.</li>  Add the following code to CMainFrame::OnCreate, to add a drop-down arrow to the Open button: // Set the drop-down buttons to have the appropriate styles.

CToolBarCtrl& rCtrl = m_wndToolBar.GetToolBarCtrl;

TBBUTTONINFO tbbi; tbbi.dwMask = TBIF_STYLE; tbbi.cbSize = sizeof tbbi;

rCtrl.GetButtonInfo(ID_FILE_OPEN, &tbbi); tbbi.fsStyle |= TBSTYLE_DROPDOWN;

VERIFY(rCtrl.SetButtonInfo(ID_FILE_OPEN, &tbbi));

rCtrl.SetExtendedStyle(        rCtrl.GetExtendedStyle | TBSTYLE_EX_DRAWDDARROWS); </li> Build and run the .EXE.</li> Undock the toolbar.</li> Grab the BOTTOM border and resize, making the toolbar three rows in height (resizing using the right border seems to work fine).</li></ol>

(c) Microsoft Corporation 1998, All Rights Reserved. Contributions by Yeong-Kah Tam, Microsoft Corporation.

Additional query words: kbvc600bug

Keywords: kbbug kbfix kbnoupdate KB190501

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.