You are here:

C++/ToolBar Problem

Advertisement


Question
Hello
I have a problem with creating my own toolbar.
The next code creates only 4 gray buttons and doesn't show the pictures. what is the problem?

CreateToolBar(HWND hwnd)
{
  HWND hToolWnd;
  TBADDBITMAP tbab;
  HINSTANCE hInst;
  TBBUTTON tbb[4];

  INITCOMMONCONTROLSEX icex;
   icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
   icex.dwICC  = ICC_LISTVIEW_CLASSES;
   InitCommonControlsEx(&icex);

  hInst = (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE);
  
  hToolWnd = CreateWindowEx(0,TOOLBARCLASSNAME,NULL,WS_CHILD | WS_VISIBLE,
       0, 0,0, 0,hwnd,(HMENU)ID_TOOLBAR,hInst,NULL);

  SendMessage(hToolWnd, TB_BUTTONSTRUCTSIZE,
        (WPARAM)sizeof(TBBUTTON), 0);

  tbab.hInst = hInst;
   tbab.nID =(int) LoadBitmap(hInst, "ToolBar");

  SendMessage(hToolWnd, TB_ADDBITMAP, 4, (LPARAM)&tbab);

  
  ZeroMemory(tbb, sizeof(tbb));

  tbb[0].iBitmap = 0;
   tbb[0].fsState = TBSTATE_ENABLED;
   tbb[0].fsStyle = TBSTYLE_BUTTON;
   tbb[0].idCommand = A;

  tbb[1].iBitmap = 1;
   tbb[1].fsState = TBSTATE_ENABLED;
   tbb[1].fsStyle = TBSTYLE_BUTTON;
   tbb[1].idCommand = B;


  tbb[2].iBitmap = 2;
   tbb[2].fsState = TBSTATE_ENABLED;
   tbb[2].fsStyle = TBSTYLE_BUTTON;
   tbb[2].idCommand = C;

  tbb[3].iBitmap = 3;
   tbb[3].fsState = TBSTATE_ENABLED;
   tbb[3].fsStyle = TBSTYLE_BUTTON;
   tbb[3].idCommand = D;

  SendMessage(hToolWnd, TB_ADDBUTTONS, (WPARAM)4,
       (LPARAM)&tbb);
  ShowWindow(hToolWnd,SW_SHOW);

}

Answer
Gil, Thank you for your question.

The error is in the statements

tbab.hInst = hInst;
tbab.nID =(int) LoadBitmap(hInst, "ToolBar");

If you want to use a 16x15 bitmap image, you must specify

tbab.hInst = NULL;

Otherwise, you should supply a Resource ID in tbab.nID.

Also, you have to send the TB_ADDBITMAP message once for each toolbar control, not once for all four controls.

David Spector

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


David Spector

Expertise

Highly knowledgeable in the C++ language, Visual C++ (MSVC), Windows API, documentation and other quality-assurance techniques, and debugging. Knowledgeable in MFC, COM, GUI design, and object-oriented design.

Experience

I have been a software engineer since 1965. I have been published. My specializations have been: biomedical programming, compiler implementation, and many kinds of Windows programming. I don't do Databases or other business-oriented stuff.

Publications
Windows?/DOS Developer's Journal, ACM SIGPLAN Notices, and Computer Science Press.

Education/Credentials
ICCP Systems Programming Certification
Master's degree equivalent in Computer Science

©2016 About.com. All rights reserved.