YOU CAN CODE!

 

With The Case Of UCanCode.net  Release The Power OF  Visual C++ !   HomeProducts | PurchaseSupport | Downloads  
Download Evaluation
Pricing & Purchase?
E-XD++Visual C++/ MFC Products
Overview
Features Tour 
Electronic Form Solution
Visualization & HMI Solution
Power system HMI Solution
CAD Drawing and Printing Solution

Bar code labeling Solution
Workflow Solution

Coal industry HMI Solution
Instrumentation Gauge Solution

Report Printing Solution
Graphical modeling Solution
GIS mapping solution

Visio graphics solution
Industrial control SCADA &HMI Solution
BPM business process Solution

Industrial monitoring Solution
Flowchart and diagramming Solution
Organization Diagram Solution

Graphic editor Source Code
UML drawing editor Source Code
Map Diagramming Solution

Architectural Graphic Drawing Solution
Request Evaluation
Purchase
ActiveX COM Products
Overview
Download
Purchase
Technical Support
  General Q & A
Discussion Board
Contact Us

Links

Get Ready to Unleash the Power of UCanCode .NET


UCanCode Software focuses on general application software development. We provide complete solution for developers. No matter you want to develop a simple database workflow application, or an large flow/diagram based system, our product will provide a complete solution for you. Our product had been used by hundreds of top companies around the world!

"100% source code provided! Free you from not daring to use components because of unable to master the key technology of components!"


MFC Article: Create toolbar with SetButtons and SetButtonInfo and OnNotify

 
 

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:

Collapse Copy Code
#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:
Collapse Copy Code
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:

Collapse Copy Code
#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:
Collapse Copy Code
// 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.

Collapse Copy Code
// 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.

Collapse Copy Code
// 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:

Collapse Copy Code
// 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:
Collapse Copy Code
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.

 

 

 

 

Copyright ?1998-2022 UCanCode.Net Software , all rights reserved.
Other product and company names herein may be the trademarks of their respective owners.

Please direct your questions or comments to webmaster@ucancode.net