Click here to Skip to main content
15,881,248 members
Articles / Desktop Programming / MFC
Article

Toolbar with image background

Rate me:
Please Sign up or sign in to vote.
3.23/5 (37 votes)
30 May 2003 189.6K   3.6K   35   21
Shows how to display a bitmap in a toolbar as background.

Sample Image - imagetoolbar.jpg

Introduction

The proposed class is based on Daniel Godson's: Generating inactive/disabled images for toolbar. I have fixed some errors in the original code. But there are still some shortcomings left:

  1. the function to load bitmap from the file and display it is not implemented
  2. the size of buttons on the toolbar is 24×24, I haven't give an interface to change it.

Using the code

Just 2 places you need to edit for your own use.

First is,

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
  if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
    return -1;
 
  if (!m_wndToolBar.CreateEx(this, 
    TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
    | CBRS_GRIPPER | CBRS_TOOLTIPS | 
    CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
    !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
  {
    TRACE0("Failed to create toolbar\n");
    return -1;      // fail to create
  }
 
  int *num,i,j=m_wndToolBar.GetToolBarCtrl().GetButtonCount();
  num=new int[j+1];
  memset(num,0,(j+1)*sizeof(int));
  num[0]=j;//button numbers,include the separators.
  for(i=0;i<j;i++)
  {
     if(ID_SEPARATOR == m_wndToolBar.GetItemID( i ))
        num[i+1]=1;
  }//get the button info.zero represent the 
  //separators.the info is used to calc 
  //the position of the buttons.

  m_wndToolBar.SetFullColorImage(IDR_MAINFRAME_24, 
    RGB(255, 0, 255),num); //the first Parameter 
    //specify the resource id of button bitmap , 
    //second is the backgroud color of  the toolbar.
    
  if (!m_wndStatusBar.Create(this) ||
    !m_wndStatusBar.SetIndicators(indicators,
    sizeof(indicators)/sizeof(UINT)))
  {
    TRACE0("Failed to create status bar\n");
    return -1;      // fail to create
  }
  
  // TODO: Delete these three lines if
  // you don't want the toolbar to
  //  be dockable
  m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
  EnableDocking(CBRS_ALIGN_ANY);
  DockControlBar(&m_wndToolBar);
  delete []num;
  return 0;
}

Second is, change the background bitmap here, by resource ID. So you must add the bitmap to your workplace first.

void CMainFrame::OnChangebk() 
{
  // TODO: Add your command handler code here
  if(whichbk)
    m_wndToolBar.UpdateBk(IDR_MAINFRAME_24, 
           RGB(255, 0, 255) , IDB_BITMAP1);
  else
    m_wndToolBar.UpdateBk(IDR_MAINFRAME_24 ,
       RGB(255, 0, 255) , IDB_BITMAP2);
  whichbk=!whichbk;
}

Acknowledgements

  • Thanks for xxxyyyzzz's translation!
  • Thanks for DavidRipple's suggestion!

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


Written By
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalnot working in VS 2005 Pin
Member 423561322-Nov-09 23:38
Member 423561322-Nov-09 23:38 
QuestionWhy this program only support multi-byte character set? Pin
benjamin_tang27-Sep-08 20:56
benjamin_tang27-Sep-08 20:56 
Generalif use this in rebar , the bitmap can extend to rightmost Pin
Cryingwind19-Dec-04 19:49
Cryingwind19-Dec-04 19:49 
GeneralDamn!!! Pin
Schniddel14-Sep-04 8:38
Schniddel14-Sep-04 8:38 
Generalmemory leak!!!!!!! Pin
huangashen20-Jul-04 16:13
huangashen20-Jul-04 16:13 
Generaldo as the following , ok! Pin
ccart77720-Aug-04 16:27
ccart77720-Aug-04 16:27 
GeneralRe: do as the following , ok! Pin
yaodebo25-Jan-07 3:04
yaodebo25-Jan-07 3:04 
GeneralRe: WRONG Pin
yaodebo25-Jan-07 16:07
yaodebo25-Jan-07 16:07 
Generalvery nice Pin
krskumar26-Sep-03 3:14
krskumar26-Sep-03 3:14 
Generalhoe to very nice... but how ! incorporate in dialog base application Pin
Rajesh match24-Sep-03 2:26
Rajesh match24-Sep-03 2:26 
GeneralWorking under Windows XP Pin
nilaysoft28-May-03 0:33
nilaysoft28-May-03 0:33 
GeneralRe: Working under Windows XP Pin
Cryingwind29-May-03 18:21
Cryingwind29-May-03 18:21 
GeneralBeautiful Pin
*love*24-May-03 18:56
*love*24-May-03 18:56 
GeneralTHANKS VERY MUCH!! I had changed the project Pin
Cryingwind24-May-03 18:04
Cryingwind24-May-03 18:04 
GeneralInternational forum Pin
Simon.W23-May-03 1:24
Simon.W23-May-03 1:24 
GeneralA suggestion to Chinese authors Pin
DavidRipple22-May-03 17:36
DavidRipple22-May-03 17:36 
GeneralRe: A suggestion to Chinese authors Pin
jjqiao28-Dec-03 22:47
jjqiao28-Dec-03 22:47 
GeneralRe: A suggestion to Chinese authors Pin
Ventruing26-Apr-04 4:04
Ventruing26-Apr-04 4:04 
GeneralRe: A suggestion to Chinese authors Pin
mace5-May-04 2:26
mace5-May-04 2:26 
Has our nationality the place to have the Chinese character ? Smile | :)
GeneralLet me try to translate it, hehe Pin
TeleStar22-May-03 9:28
TeleStar22-May-03 9:28 
Generalwtf Pin
Steve McLenithan22-May-03 2:27
Steve McLenithan22-May-03 2:27 

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.