How to Get the Current State of a Button in a CToolbar

Last reviewed: October 10, 1997
Article ID: Q115306
1.00 1.50 1.51 1.52 | 2.00 2.10 4.00
WINDOWS             | WINDOWS NT
kbprg

The information in this article applies to:

  • The Microsoft Foundation Classes (MFC) included with:

        - Microsoft Visual C++ for Windows, versions 1.0, 1.5, 1.51, and 1.52
        - Microsoft Visual C++ 32-bit Edition, versions 2.0, 2.1, and 4.0
    

SUMMARY

Although the Microsoft Foundation Classes (MFC) offer a useful and easy method of keeping the state of a menu command and/or toolbar button in a CControlBar tied together, there is no easy means of finding out the command's or button's current state.

MORE INFORMATION

To find the current state of a menu command or toolbar button in a CControlBar-derived class, use the following piece of code:

      UINT iButtonID;
      UINT iButtonStyle;
      int iButtonImage;

      // If the Toolbar is not based upon the default "AFX_IDW_TOOLBAR"
      // constant, then replace its use with the valid Toolbar constant.

      // If the following line is used from the Frame Window remove
      //"GetParentFrame()->":
      CToolBar* pBar =
      (CToolBar*)GetParentFrame()->GetDescendantWindow(AFX_IDW_TOOLBAR);

      // If this code sample is called from an AppWizard generated,
      // Frame Window member function in Visual C++ 4.0, replace the above
      // statement with the following. Recall that the CMDIFrameWnd-derived
      // Frame Window generated by AppWizard has a CToolBar m_wndToolBar
      // member.
      CToolBar* pBar = &m_wndToolBar;

      if (pBar != NULL) {

           // Use the relevant Button ID for the following line:
           int iButtonIndex = pBar->CommandToIndex(ID_MY_BUTTON);

           pBar->GetButtonInfo(iButtonIndex, iButtonID, iButtonStyle,
      iButtonImage);

           // The following code checks for all possible states.
           // In practice, check only for those states that you need.

           if (iButtonStyle & TBBS_PRESSED)
                // Button Down
           else
                if (iButtonStyle & (TBBS_CHECKED & TBBS_DISABLED))
                     // Button Down & Unavailable
                else
                     if (iButtonStyle & TBBS_DISABLED)
                          // Button Disabled
                     else
                          if (iButtonStyle & TBBS_INDETERMINATE)
                               // Button State Indeterminate
                          else
                               if (iButtonStyle & TBBS_CHECKED)
                                    // Button Checked
                               else
                                    // Button Up & Enabled

      }


Additional reference words: 1.00 1.50 2.00 2.10 2.50 2.51 2.52 3.00 3.10
4.00 kbinf

KBCategory: kbprg
KBSubcategory: MfcUI
Keywords : MfcUI kbprg
Technology : kbMfc
Version : 1.00 1.50 1.51 1.52 | 2.00 2.10
Platform : NT WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: October 10, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.