Thread: [C] Problem with icons in a toolbar

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    49
    salutations,

    we are beginners. we are trying to create a toolbar with icons both from the Common Controls standard icons (STD_FILENEW, etc.) and from an imagelist that contains self-made icons. we are using this code (based on MSDN and www.winprog.org/tutorial/).
    NOTE: we are including the rest of the WinMain for clarity as to what is b.

    Code:
    _stdcall WinMain(HINSTANCE i, HINSTANCE j, char *k, int l) 
    { 
       InitCommonControls();
       LoadLibrary("Riched32.dll");
       b=CreateDialog(i,(LPCTSTR)"DIALOGO",NULL,(DLGPROC)zzz);
    
       HWND hTool = CreateWindowEx(0, TOOLBARCLASSNAME, NULL, WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, b, (HMENU)45/*ID*/, GetModuleHandle(NULL), NULL);
       SendMessage(hTool, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
       TBBUTTON tbb[3];
    
        HIMAGELIST hImageList = ImageList_Create(
            16, 16,                   // Dimensions of individual bitmaps.  
            ILC_COLOR16 | ILC_MASK,   // Ensures transparent background.
            sizeof(tbb)/sizeof(TBBUTTON), 0);
    	int custom_icon = ImageList_AddIcon(hImageList, LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDR_ICO_MAIN)));
    	SendMessage(hTool, TB_SETIMAGELIST, 0, (LPARAM)hImageList);
    
    	SendMessage(hTool, TB_LOADIMAGES, (WPARAM)IDB_STD_SMALL_COLOR, (LPARAM)HINST_COMMCTRL);
    
    	ZeroMemory(tbb, sizeof(tbb));
       tbb[0].iBitmap = STD_FILENEW;
       tbb[0].fsState = TBSTATE_ENABLED;
       tbb[0].fsStyle = TBSTYLE_BUTTON;
       tbb[0].idCommand = 6002;
    
       tbb[1].iBitmap = STD_FILEOPEN;
       tbb[1].fsState = TBSTATE_ENABLED;
       tbb[1].fsStyle = TBSTYLE_BUTTON;
       tbb[1].idCommand = 6001;
    
       tbb[2].iBitmap = custom_icon;
       tbb[2].fsState = TBSTATE_ENABLED;
       tbb[2].fsStyle = TBSTYLE_BUTTON | TBSTYLE_DROPDOWN;
       tbb[2].idCommand = 453;
    
       SendMessage(b, WM_SIZE, 0, 0);
       ShowWindow(b,SW_SHOW);
    
       while (GetMessage(&c,0,0,0)==TRUE)
       {
    	  TranslateMessage(&c); 
          DispatchMessage(&c);
       }
    }
    notice that there is both the loading of an icon in an imagelist and its loading into the toolbar, as well as SendMessage(hTool, TB_LOADIMAGES, (WPARAM)IDB_STD_SMALL_COLOR, (LPARAM)HINST_COMMCTRL); for the loading of the common control icons. the strange thing is that the button that was supposed to display STD_FILENEW displays STD_DELETE instead. when only the common control icons - and not the icons from the imagelist - are loaded into the toolbar, STD_FILENEW is displayed correctly.
    what is wrong with this code? is there a more proper way?

    thank you in advance.
    Last edited by pc2-brazil; 12-21-2008 at 05:47 AM.

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Shouldn't

    ZeroMemory(tbb, sizeof(tbb));

    be

    ZeroMemory(&tbb, sizeof(tbb));
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    49
    Quote Originally Posted by novacain View Post
    Shouldn't

    ZeroMemory(tbb, sizeof(tbb));

    be

    ZeroMemory(&tbb, sizeof(tbb));
    thank you for the response. we just tried it, but it made no difference in the output.
    but we forgot to mention one detail: when both the imagelist with IDR_ICO_MAIN and the standard icons from Common Controls are set in the toolbar, what should be STD_FILENEW is shown as STD_DELETE and what should be STD_FILEOPEN is shown as STD_FILENEW.
    strange, isn't it? is it that both the imagelist and the bitmaps from Common Controls shouldn't be loaded together?
    update:
    another interesting point that we noticed: in that conditions, when we write:
    Code:
    	ZeroMemory(tbb, sizeof(tbb));
       tbb[0].iBitmap = STD_FILENEW+1;
       tbb[0].fsState = TBSTATE_ENABLED;
       tbb[0].fsStyle = TBSTYLE_BUTTON;
       tbb[0].idCommand = 6002;
    
       tbb[1].iBitmap = STD_FILEOPEN+1;
       tbb[1].fsState = TBSTATE_ENABLED;
       tbb[1].fsStyle = TBSTYLE_BUTTON;
       tbb[1].idCommand = 6001;
    
       tbb[2].iBitmap = custom_icon;
       tbb[2].fsState = TBSTATE_ENABLED;
       tbb[2].fsStyle = TBSTYLE_BUTTON | TBSTYLE_DROPDOWN;
       tbb[2].idCommand = 453;
    summing 1 to the icon indices, they are shown correctly. anyway, if we add one more icon to the imagelist:
    Code:
    	int custom_icon = ImageList_AddIcon(hImageList, LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDR_ICO_MAIN)));
    	int custom_icon1 = ImageList_AddIcon(hImageList, LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDR_ICO_MAIN)));
    	SendMessage(hTool, TB_SETIMAGELIST, 0, (LPARAM)hImageList);
    we have to add 2 (i. e. STD_FILENEW+2, STD_FILEOPEN+2, etc) for the icons to display correctly.
    any ideas? thank you in advance.
    Last edited by pc2-brazil; 12-22-2008 at 05:39 AM.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Add some error testing.

    Check the WIN 32 functions return success, if not use GetLastError()

    I would look at the return from

    int custom_icon = ImageList_AddIcon(hImageList, LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDR_ICO_MAIN)));

    it is not an ID, it is an index.

    You are therefore mixing IDs and indexes in the button structs.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Toolbar problem
    By mrafcho001 in forum Windows Programming
    Replies: 2
    Last Post: 12-27-2005, 11:38 AM
  2. Toolbar problem
    By Ward in forum Windows Programming
    Replies: 0
    Last Post: 12-21-2005, 03:08 PM
  3. Dockable Toolbar (Like taskbar or office toolbar)
    By om3ga in forum Windows Programming
    Replies: 2
    Last Post: 11-20-2005, 03:09 AM
  4. Troubles with ListView and Toolbar
    By cornholio in forum Windows Programming
    Replies: 8
    Last Post: 11-14-2005, 01:26 AM
  5. Replies: 5
    Last Post: 11-07-2005, 11:34 PM