views:

57

answers:

3

I am trying to get my toolbar to work with an ImageList. The images are individual PNGs, so I was adding them in the ImageList in succession, Bbt it wasn't working. Here is the code to add the Image to the ImageList:

HIMAGELIST CreateToolBarImages(HINSTANCE hInst)
{
    HIMAGELIST v_ImageList = NULL;
    // IMAGE_LIST v_Img;
    HICON hIcon;
    HBITMAP hBit;
    COLORMAP cMap;
    COLORREF fromColor = RGB( 0,0,0 );

    InitCommonControls();

    v_ImageList = ImageList_Create(32, 32, ILC_MASK, 1, 1);

    cMap.from = fromColor;
    cMap.to = ::GetSysColor(COLOR_BTNFACE);
    hBit = CreateMappedBitmap(hInst, IDB_CONSOLE, 0, &cMap, 1);

    // hBit = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_CONSOLE));
    consoleImg = ImageList_Add(v_ImageList, hBit, 0);
    if (consoleImg == -1)
        return NULL;

    DeleteObject(hBit);

    // [edit Franci Penov]
    return v_ImageList;
}

Then I create The ToolBar, but it fails at the Image function.

HWND CreateToolBarButton(HWND hWndParent)
{
    const int ImageID = 0;
    const int numB = 1;
    COLORREF iColor;

    HWND hToolBar = CreateWindowEx(0,
        TOOLBARCLASSNAME,
        NULL,
        WS_CHILD |TBSTYLE_LIST |TBSTYLE_FLAT | WS_VISIBLE,
        0,0,0,0,
        hWndParent, 
        NULL,
        g_hInst, 
        NULL);

    if (hToolBar == NULL)
        return NULL;

    HIMAGELIST ImgList = CreateToolBarImages(g_hInst);
    if (ImgList == NULL)
        MessageBox( hWndParent, L"No Tool Images", L"BOB", MB_OK );

    // [edit Franci Penov]
    return hToolBar;
}

Is there something I am missing or not doing?

+1  A: 

I've made some changes around returning values in your example code above which I marked with comments.

For detailed explanation on how to use the Win32 Toolbar control, you can read this MSDN article.

In particular, what your code seems to be missing is sending the TB_SETIMAGELIST message to the Toolbar control to load the ImageList you just created:

// Set the image list.
SendMessage(hToolBar, TB_SETIMAGELIST, 0, (LPARAM)ImgList);

Of course, there might be other things you are missing in there, but that would be a good starting point.

You also have some GDI objects memory leaks around the couple of places you do error checks and exit your functions early. You might want to clean those up as well.

Franci Penov
A: 

Whats "not working". CreateMappedBitmap is used for loading images that have a color table - which means they have <= 256 colors.

PNGs imply you are using 32bpp images with an alpha channel which means you cannot use the *MappedBitmap functions.

Chris Becke
A: 

Hi and thank you fellas i have resolved my issue, Chris youre right, after i posted i made that observation as well, I would need to use GDI+ in order to use PNG's, i've opted to go with using Icons instead and it works perfectly now. Franci, thank you i have cleaned up the code as well.

regards,

blaquee