Search code examples
c++winapitoolbar

Not loading custom bitmaps for toolbar in C++


I'm working on a WINAPI application, and I created a toolbar using CreateWindowEx().

And as you know, to load custom icons for the toolbar's buttons, you should specify the bitmap which contains the a list of icons in the resource file, and join it to the toolbar
(By using tbab.nID = IDB_LIST;). BUT IT DOESN'T WORK!

resource.h:

#define IDB_LIST 101


resource.rc:

#include "resource.h"
IDB_LIST BITMAP "list.bmp"


main.cpp:

            TBBUTTON tbb[1];
            TBADDBITMAP tbab;

            tbab.hInst = g_hInst;
            tbab.nID = IDB_LIST;

            hToolBar = CreateWindowEx(TBSTYLE_EX_MIXEDBUTTONS, TOOLBARCLASSNAME, NULL, WS_CHILD | WS_VISIBLE | TBSTYLE_TOOLTIPS, 0, 0, 0, 0,
                hWnd, (HMENU)IDC_MAIN_TOOL, g_hInst, NULL);

            SendMessage(hToolBar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
            SendMessage(hToolBar, TB_ADDBITMAP, (WPARAM)1, (LPARAM)&tbab);
            SendMessage(hToolBar, TB_SETMAXTEXTROWS, 0, 0);

            ZeroMemory(&tbb, sizeof(tbb));
            tbb[0].iBitmap = 0;
            tbb[0].fsState = TBSTATE_ENABLED;
            tbb[0].fsStyle = TBSTYLE_BUTTON;
            tbb[0].idCommand = ID_FILE_NEW;
            tbb[0].iString = (INT_PTR)"Create a new file";

            SendMessage(hToolBar, TB_ADDBUTTONS, sizeof(tbb)/sizeof(TBBUTTON), (LPARAM)&tbb);

Solution

  • I used another way to load bitmap.

    Modified code (only the custom bitmap section is provided):

    HWND CreateSimpleToolbar(HWND hWnd)
    {
        TBBUTTON tbb[1];
        TBADDBITMAP tbab;
    
        const int bitmapSize = 32;
        const int ImageListID = 0;
    
        hToolBar = CreateWindowEx(TBSTYLE_EX_MIXEDBUTTONS, TOOLBARCLASSNAME, NULL, WS_CHILD | WS_VISIBLE | TBSTYLE_TOOLTIPS | TBSTYLE_WRAPABLE, 0, 0, 0, 0,
            hWnd, NULL, g_hInst, NULL);
        HBITMAP bitmap = (HBITMAP)LoadImage((HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE), MAKEINTRESOURCE(IDB_LIST), IMAGE_BITMAP, 32, 32, NULL);
    
        SendMessage(hToolBar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
    
        ZeroMemory(&tbb, sizeof(tbb));
        tbb[0].iBitmap = 0;
        tbb[0].fsState = TBSTATE_ENABLED;
        tbb[0].fsStyle = TBSTYLE_BUTTON;
        tbb[0].idCommand = ID_FILE_NEW;
        tbb[0].iString = (INT_PTR)L"Create a new file";
    
        g_hImageList = ImageList_Create(bitmapSize, bitmapSize,   // Dimensions of individual bitmaps.
            ILC_COLOR24 | ILC_MASK,   // Ensures transparent background.
            1, 0);
        ImageList_Add(g_hImageList, bitmap, NULL);
    
        SendMessage(hToolBar, TB_SETIMAGELIST,
            (WPARAM)ImageListID,
            (LPARAM)g_hImageList);
    
        SendMessage(hToolBar, TB_ADDBUTTONS, sizeof(tbb) / sizeof(TBBUTTON), (LPARAM)&tbb);
    
        SendMessage(hToolBar, TB_AUTOSIZE, 0, 0);
        return hToolBar;
    }
    

    Debug:

    2

    I mainly use ImageList_Add.

    Adds an image or images to an image list.

    Refer :How to use custom icons for toolbars in winapi programming

    Create an image list using ImageList_Create(), add your BMP image to it using ImageList_Add() or ImageList_ReplaceIcon(), associate it with the toolbar using TB_SETIMAGELIST, and then you can set tbb[0].iBitmap to the BMP's index within the image list.

    But surely, the picture ID is no problem. I load the BMP image into the resource file, and then load the image successfully.