Click here to Skip to main content
15,071,480 members
Articles / Desktop Programming / MFC
Article
Posted 18 Jul 2007

Stats

69.7K views
1.2K downloads
42 bookmarked

How to Create a Dynamic ToolBar

Rate me:
Please Sign up or sign in to vote.
2.09/5 (8 votes)
24 Jul 20073 min read
Create a dynamic toolbar, adding a dynamic bitmap and status and tool tip text

Screenshot - DynamicToolBar.gif

Introduction

This article shows a simple way to add buttons to a toolbar and set the image, tooltip and status bar text dynamically.

Strategy

Override CToolBar::OnNotify to set dynamic tooltips and CMainFrame::GetMessageString to provide dynamic status text. Create a bitmap and draw the buttons onto it. Use the SetBitmap toolbar method to assign the bitmap to the toolbar control. Insert and set the button information with the SetButtons and SetButtonInfo methods. Then create the MFC Application Project and add a CMyDynamicToolBar CToolBar derived class:

  1. File -> New -> New project. Create a new DynamicToolBar Visual C++, MFC application.
  2. In the class view, right click the DynamicToolBar project. Add->Add Class... Select an MFC class on the right pane and click Open.
  3. Select CToolBar from the Base class drop-down list. Type CMyDynamicToolBar into the Class Name edit box.
  4. Click Finish.

We need to store the tooltip and status texts. For this example, we will use the same text vector array. Add the following code to the MyDynamicToolBar.h file:

C++
#pragma once
typedef std::vector <CString> V_CString;
  1. Right click the CMyDynamicToolBar class in Class view -> Add-> Add Variable...
  2. From the Access drop down list, select private.
  3. Into the Variable Type edit box, type V_CString and into the Variable Name edit box, type m_asToolTips.
  4. Into the Comment edit box, type Variable for storing tool tip text.

Now we will override the CToolBar::OnNotify event for responding to the TTN_NEEDTEXT message of the tool bar.

  1. Right click the CMyDynamicToolBar class in Class view -> Properties.
  2. On Properties, click the Override button.
  3. Scroll down to the OnNotify event and click on the cell right next to it.
  4. Select <Add> OnNotify from the drop-down list.
  5. Press Ctrl+Shift+Enter to change to full screen view.
  6. Replace CMyDynamicToolBar::OnNotify with:
C++
BOOL CMyDynamicToolBar::OnNotify(WPARAM wParam, 
    LPARAM lParam, LRESULT* pResult)
{
    LPNMHDR pNMHDR = ((LPNMHDR)lParam);
    TOOLTIPTEXTW *pTTT=(TOOLTIPTEXTW *)pNMHDR;
    UINT t = TTN_NEEDTEXTW;
    if(pNMHDR->code == t) 
    {  
        UINT nID=pNMHDR->idFrom;
        if(!(pTTT->uFlags & TTF_IDISHWND))
        {
            if ((nID >= ID_MYTOOLBAR_BUTTON1) || 
                (nID <= ID_MYTOOLBAR_BUTTONLAST))
            {
                USES_CONVERSION;

                UINT btn1 = ID_MYTOOLBAR_BUTTON1;

                pTTT->lpszText = T2W(m_aCString[nID - btn1]);
                pTTT->hinst = NULL;

                return TRUE;
            }
        }
    }
    return CToolBar::OnNotify(wParam, lParam, pResult);
}

Press Ctrl+Shift+Enter to change back from full screen view. Add the following to the end of the stdafx.h file:

C++
#define ID_MYTOOLBAR_BUTTON1 WM_USER + 1
#define ID_MYTOOLBAR_BUTTONLAST WM_USER + 100

Now we will add some methods for setting and retrieving the tooltips. For simplicity's sake, we will assume that all insertions are consecutive, in the same order for button IDs.

  1. Right click the CMyDynamicToolBar class in Class view -> Add -> Add Function...
  2. Set the Return type to void and set the Function name to InsertToolTip.
  3. Also set the parameter type to CString and the parameter name to sToolTip.
  4. Next, click the Add button.
  5. Set the comment to Inserts tooltip text consecutively.
  6. Click Finish.
  7. Edit CMyDynamicToolBar::InsertToolTip into this:
C++
// Inserts tool tips text consecutively
void CMyDynamicToolBar::InsertToolTip(CString sToolTip)
{
    m_asToolTips.push_back(sToolTip);
}

Repeat the above steps to create the following CMyDynamicToolBar::GetToolTip function.

C++
// Get tool tips text
CString CMyDynamicToolBar::GetToolTip(UINT nID)
{
    UINT btn1 = ID_MYTOOLBAR_BUTTON1;
          return m_asToolTips[nID - btn1];
}

Now modify CMyDynamicToolBar::~CMyDynamicToolBar() to clear the ToolTips arrays on destruction.

C++
// Clear tool tips text array
CMyDynamicToolBar::~CMyDynamicToolBar()
{
   m_asToolTips.clear();
}

Now add code for handling the creation of CMyDynamicToolBar on the main frame window:

  1. Right click CMainFrame class. -> Add Variable...
  2. Type CMyDynamicToolBar into the Variable Type edit box.
  3. Type m_wndMyToolBar into the Variable Name edit box.
  4. Click Finish.
  5. Double click CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)

Then add the following code:

C++
// begin create dynamic tool bar
if (!m_wndMyToolBar.CreateEx(this, 
    TBSTYLE_FLAT /*| TBSTYLE_TRANSPARENT*/, WS_CHILD | WS_VISIBLE | CBRS_TOP
    | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC))
{
    TRACE0("Failed to create dynamic toolbar\n");
    return -1;      // fail to create
}

CDC *dc = this->GetDC();
CDC dcMem; 
dcMem.CreateCompatibleDC(dc);
HBITMAP hbitmap = CreateCompatibleBitmap(dc->m_hDC, 50*16, 16);
CBitmap *bmp = CBitmap::FromHandle(hbitmap);
CBitmap* pOldBitmap = dcMem.SelectObject( bmp );

//draw a filled rectangle with the background (window) color 
CRect rect(0, 0, 5*16, 16);
FillRect(dcMem.m_hDC, &rect, (HBRUSH) (COLOR_WINDOW));
dcMem.SetMapMode(MM_TEXT);
CString tmp = "Button ";

for (int i=0;i < 5;i++) 
{
    HICON icon = LoadIcon(NULL,MAKEINTRESOURCE(32512+i));
    dcMem.DrawIcon( i*16, 0, icon );
    char buffer[2];
    m_wndMyToolBar.InsertToolTip( tmp + itoa(i,buffer,10));
}

dcMem.SelectObject(pOldBitmap); 
dcMem.DeleteDC();
pOldBitmap->DeleteObject();

m_wndMyToolBar.SetBitmap(hbitmap);
m_wndMyToolBar.SetButtons(NULL,i);

for(int j=0; j < i; j++) 
{
    m_wndMyToolBar.SetButtonInfo(j, ID_MYTOOLBAR_BUTTON1 + j, 
        TBBS_BUTTON | TBBS_AUTOSIZE, j);
}

// end    create dynamic tool bar

So far, we have the toolbar created and it shows tooltip text, but we also want to have the same text on the status bar. We therefore need to override the CMainFrame::GetMessageString method that is responsible for providing status bar text according to control IDs:

  1. Right click the CMainFrame class in Class view -> Properties.
  2. On Properties, click the Override button.
  3. Scroll down to the GetMessageString method.
  4. Click on the cell right next to it and select <Add> GetMessageString from the drop down list.
  5. Replace MainFrame::GetMessageString with:
C++
void CMainFrame::GetMessageString(UINT nID, CString& rMessage) const
{
    if ((nID >= ID_MYTOOLBAR_BUTTON1) && (nID <= ID_MYTOOLBAR_BUTTONLAST))
        rMessage = (const_cast <CMyDynamicToolBar*> (&m_wndMyToolBar))
        ->GetToolTip(nID);
    else
        CMDIFrameWnd::GetMessageString(nID, rMessage);
    return;
}

Build and run the application. (F7) (F5)

Points of interest

MSDN documents state that we need to process the TBN_GETINFOTIP message for specifying the tooltip text. A simpler way to get tooltip texts is to set the TBSTYLE_LIST style and the TBSTYLE_EX_MIXEDBUTTONS toolbar extended style. For buttons that do not have the BTNS_SHOWTEXT style, the toolbar control will automatically display the button text as a tooltip. This works for Version 5.81 of Common Controls.

History

  • 18 July, 2007 -- Original version posted
  • 24 July, 2007 -- Updated

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

rmortega77
Software Developer CIMEX S.A.
Cuba Cuba
Rodolfo Ortega is a Cuban Computer Scientist. He works as IT Auditor for the CIMEX S.A. subsidiary in Holguin, Cuba. He lives and works in Holguin, in the eastern part of the island of Cuba.

You can contact him at rodolfom[]cimex.com.cu for any personal message: Ideas on new articles, bibliography about new APIs, questions, are wellcome.

Submit questions related with current article to the article forum.

Comments and Discussions

 
Question(HBRUSH)COLOR_WINDOW Pin
Bob H23-Mar-16 10:51
MemberBob H23-Mar-16 10:51 
Generalnice Pin
alipour200720-Jul-07 2:00
Memberalipour200720-Jul-07 2:00 
GeneralIt is nice Pin
Tarik Guney19-Jul-07 6:26
MemberTarik Guney19-Jul-07 6:26 
GeneralThis isnt C# Pin
leppie18-Jul-07 22:23
Memberleppie18-Jul-07 22:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.