How to create a True Color bitmap buttons on the toolbar?

How to create a True Color bitmap buttons on the toolbar?

Post by S2V2aW4gQ2 » Fri, 02 Jul 2004 14:09:01


Jeff,

You can try using an ImageList. I believe that's the only way to use 32-bit bitmaps in a toolbar, so I would imagine it would do fine for 24-bit bitmaps.

Here's a sample:

/************************/

#define NUM_BUTTONS 2

HIMAGELIST ilist = NULL;
HWND hwndToolbar = CreateWindow(TOOLBARCLASSNAME, NULL, WS_CHILD | TBSTYLE_TOOLTIPS, 0, 0, 0, 0, hWnd, (HMENU)IDC_TOOLBAR, hInst, NULL);

ilist = ImageList_Create(48, 48, ILC_COLOR32, NUM_BUTTONS, 0);
HBITMAP bmp = NULL;

bmp = (HBITMAP)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(BITMAP1), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_DEFAULTSIZE);
ImageList_Add(ilist, bmp, NULL);
DeleteObject(bmp);

bmp = (HBITMAP)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(BITMAP2), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_DEFAULTSIZE);
ImageList_Add(ilist, bmp, NULL);
DeleteObject(bmp);

TBBUTTON tbb[NUM_BUTTONS];
char szBuffer[16];
int imgstr[NUM_BUTTONS];
int cmdid[NUM_BUTTONS] = { MENU_COMMAND_1, MENU_COMMAND_2 };

LoadString(hInst, IDS_BUTTONTEXT1, (LPSTR) &szBuffer, 16);
imgstr[0] = SendMessage(hwndToolbar, TB_ADDSTRING, 0, (LPARAM) (LPSTR) szBuffer);
LoadString(hInst, IDS_BUTTONTEXT2, (LPSTR) &szBuffer, 16);
imgstr[1] = SendMessage(hwndToolbar, TB_ADDSTRING, 0, (LPARAM) (LPSTR) szBuffer);

SendMessage(hwndToolbar, TB_SETIMAGELIST, 0, (LPARAM) ilist);

for (i=0;i<NUM_BUTTONS;i++)
{
tbb[i].iBitmap = i;
tbb[i].iString = imgstr[i];
tbb[i].idCommand = cmdid[i];
tbb[i].fsState = TBSTATE_ENABLED;
tbb[i].fsStyle = BTNS_BUTTON;
}

SendMessage(hwndToolbar, TB_ADDBUTTONS, NUM_BUTTONS, (LPARAM)(&tbb));

/****************************/

Hope that helps!

Kevin