1

I wanna make a large toolbar with support of icons with more colors depth than default in Visual Studio. I am using Visual Studio 2005 and the Toolbar is on a CDialog.

I used the Code found : here but did not work.

int CSalariesForm::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CDialog::OnCreate(lpCreateStruct) == -1)
        return -1;

    HBITMAP hBitmap = (HBITMAP) ::LoadImage(AfxGetInstanceHandle(),
        MAKEINTRESOURCE(IDR_MAINFRAME1), IMAGE_BITMAP,
        0,0, LR_CREATEDIBSECTION | LR_LOADMAP3DCOLORS);

    CBitmap bm;
    bm.Attach(hBitmap);


    CImageList m_imagelist;
    m_imagelist.Create(20, 20, ILC_COLOR8, 4, 4);
    m_imagelist.Add(&bm, (CBitmap*) NULL);

    cToolBar.Create(this);
    cToolBar.GetToolBarCtrl().SetImageList(&m_imagelist);

    cToolBar.ShowWindow(SW_SHOW);
    cToolBar.SetBarStyle(CBRS_TOOLTIPS | CBRS_FLOATING | CBRS_ALIGN_TOP | CBRS_FLYBY);
    RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);
    return 0;
}

And when I call the dialog there is no Toolbar shown. What is wong with the code?

Thank You

Community
  • 1
  • 1
Blood-HaZaRd
  • 2,049
  • 2
  • 20
  • 43

1 Answers1

1

Assigning an image list to a toolbar does not create any toolbar buttons. Also your image list should be a class member, not a local variable.

// header file
private:
    CImageList m_imagelist;
    CToolBar   m_toolbar;
// source file
enum { width = 20, height = 20 }; // width and height of one button image

m_toolbar.Create(this);

// create the image list
m_imagelist.Attach(
    ImageList_LoadImage(
        AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_BITMAP1), width, 4, 
        CLR_DEFAULT, IMAGE_BITMAP, LR_CREATEDIBSECTION | LR_LOADTRANSPARENT )
);

// set button and image sizes (copied from CToolBar::LoadToolBar)
m_toolbar.SetSizes(CSize(width+7,height+7), CSize(width,height));

// set image list
m_toolbar.GetToolBarCtrl().SetImageList(&m_imagelist);

// define command ids for each button
const UINT cmdIds[] = { 
    IDOK,
    0,        // separator
    IDCANCEL,
};

// assign ids to the toolbar
m_toolbar.SetButtons(cmdIds, sizeof(cmdIds)/sizeof(UINT));

RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);

IDB_BITMAP1 is a 40x20 24bit color bitmap (two 20x20 buttons). If you need more control over the creation of the buttons, you can use CToolBarCtrl::SetButtons() instead. Refer to ImageList_LoadImage for more details on loading the image list.

Anonymous Coward
  • 6,186
  • 1
  • 23
  • 29
  • Thank you, that works Great. One little issue, how could I add a tooltip to the toolbar when the cursor is upon the button of the toolbar? thank you – Blood-HaZaRd Dec 26 '12 at 08:36
  • Ok I found it, Overrinding OnNotify() ;) – Blood-HaZaRd Dec 26 '12 at 08:51
  • @Blood-HaZaRd or you could handle [TTN_NEEDTEXT](http://msdn.microsoft.com/en-us/library/windows/desktop/bb760293%28v=vs.85%29.aspx) which is specifically for tooltips. And if you add string table entries matching your command IDs, it's as simple as `CString::LoadString` to get the text. – Anonymous Coward Dec 26 '12 at 09:47