Thread: Adding user drawn bitmaps to an existing toolbar

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    54

    Question Adding user drawn bitmaps to an existing toolbar

    i wonder if anyone can help me with this small problem

    i have a toolbar, that displays 3 standard toolbal buttons, NEW, OPEN, SAVE. This displays and works okay, i can add other system buttons using this method, but i cant create my own buttons or at least add my bitmaps to the buttons. The code i used came from the site that everyone seems to mention on this board, but i forgot the address, but i will show the code for the area i need to add the buttons below.

    Code:
    case WM_CREATE:
    		{
    			HWND hTool;
    			TBBUTTON tbb[3];//define array of toolbar buttons
    			TBADDBITMAP tbab;
    
    			HWND hStatus;
    			int statwidths[] = {100, -1};
    
    			CLIENTCREATESTRUCT ccs;
    
    			// Create MDI Client
    
    			// Find window menu where children will be listed
    			ccs.hWindowMenu  = GetSubMenu(GetMenu(hwnd), 2);
    			ccs.idFirstChild = ID_MDI_FIRSTCHILD;
    
    			g_hMDIClient = CreateWindowEx(WS_EX_CLIENTEDGE, "mdiclient", NULL,
    				WS_CHILD | WS_CLIPCHILDREN | WS_VSCROLL | WS_HSCROLL | WS_VISIBLE,
    				CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
    				hwnd, (HMENU)IDC_MAIN_MDI, GetModuleHandle(NULL), (LPVOID)&ccs);
    
    			if(g_hMDIClient == NULL)
    				MessageBox(hwnd, "Could not create MDI client.", "Error", MB_OK | MB_ICONERROR);
    
    			// Create Toolbar
    
    			hTool = CreateWindowEx(0, TOOLBARCLASSNAME, NULL, WS_CHILD | WS_VISIBLE, 0, 0, 0, 0,
    				hwnd, (HMENU)IDC_MAIN_TOOL, GetModuleHandle(NULL), NULL);
    			if(hTool == NULL)
    				MessageBox(hwnd, "Could not create tool bar.", "Error", MB_OK | MB_ICONERROR);
    
    			// Send the TB_BUTTONSTRUCTSIZE message, which is required for
    			// backward compatibility.
    			SendMessage(hTool, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
    			
    			tbab.hInst = HINST_COMMCTRL;
    			tbab.nID = IDB_STD_LARGE_COLOR;
    			SendMessage(hTool, TB_ADDBITMAP, 0, (LPARAM)&tbab);
    
    			ZeroMemory(tbb, sizeof(tbb));
    			tbb[0].iBitmap		= STD_FILENEW;		tbb[0].fsState		= TBSTATE_ENABLED;
    			tbb[0].fsStyle		= TBSTYLE_BUTTON;	tbb[0].idCommand	= IDM_FILE_NEW;
    
    			tbb[1].iBitmap		= STD_FILEOPEN;		tbb[1].fsState		= TBSTATE_ENABLED;
    			tbb[1].fsStyle		= TBSTYLE_BUTTON;	tbb[1].idCommand	= IDM_FILE_OPEN;
    
    			tbb[2].iBitmap		= STD_FILESAVE;		tbb[2].fsState		= TBSTATE_ENABLED;
    			tbb[2].fsStyle		= TBSTYLE_BUTTON;	tbb[2].idCommand	= IDM_FILE_SAVE_AS;
    			
    
    			SendMessage(hTool, TB_ADDBUTTONS, sizeof(tbb)/sizeof(TBBUTTON), (LPARAM)&tbb);
    
    			// Create Status bar
    
    			hStatus = CreateWindowEx(0, STATUSCLASSNAME, NULL,
    				WS_CHILD | WS_VISIBLE | SBARS_SIZEGRIP, 0, 0, 0, 0,
    				hwnd, (HMENU)IDC_MAIN_STATUS, GetModuleHandle(NULL), NULL);
    
    			SendMessage(hStatus, SB_SETPARTS, sizeof(statwidths)/sizeof(int), (LPARAM)statwidths);
    			SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Hi there :)");
    		}
    		break;
    does anyone have any ideas??

    i am using msvc6.0 and using win32 and c

    thanx in advance

    korbitz

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You can have a look at a complete sample here(see dll_toolbar.c):
    http://www.exhedra.com/vb/scripts/Sh...=7011&lngWId=3

    But here are the steps:
    1. Add a bitmap containing your toolbar icons as a resource. It should contain a series of 16x16 icons.
      Code:
      // Bitmap containing toolbar icons...
      TOOLBAR_ICONS BITMAP "dll_tbicons.bmp"
    2. Add the bitmap to the toolbar control and return the index of the first image.
      Code:
      // Add custom images...
      tbAddBitmap.hInst = g_hInstance; /* App's hInstance. */
      tbAddBitmap.nID = TOOLBAR_ICONS; /* Resource id of bitmap */
      iImageOffset = SendMessage(g_hwndToolbar, TB_ADDBITMAP, 5, (LPARAM) &tbAddBitmap); /* Bitmap contains 5 icons */
    3. Use the images with your buttons.
      Code:
      tbb[3].iBitmap = iImageOffset + 0; // Use first icon
      tbb[4].iBitmap = iImageOffset + 1; // Use second button


    P.S I see you are using large buttons so icons should be 24x24(or possibly 26x26, docs are unclear) rather that 16x16.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    54
    okay i added the code from that link but the images are not displaying

    the compiler dont like this line

    Code:
    // Create a toolbar that has ToolTips associated with it...
    	g_hwndToolbar = CreateWindowEx(0, TOOLBARCLASSNAME, NULL,
    			             WS_CHILD | TBSTYLE_TOOLTIPS | WS_CLIPSIBLINGS, 0, 0, 0, 0,
    				     hwnd, (HMENU) 0, (HINSTANCE) GetWindowLongPtr(hwnd, GWL_HINSTANCE), NULL);
    although i am not too sure if this is the problem, i made the following modifications
    Code:
    // Create a toolbar that has ToolTips associated with it...
    			g_hToolbar = CreateWindowEx(0, TOOLBARCLASSNAME, NULL,
    						 WS_CHILD | TBSTYLE_TOOLTIPS | WS_CLIPSIBLINGS, 0, 0, 0, 0,
    						 hwnd, (HMENU) IDC_MAIN_TOOL, (HINSTANCE)GetModuleHandle(0), NULL);
    here is the complete CASE statement again, with the modifications
    Code:
    case WM_CREATE:
    		{
    			CLIENTCREATESTRUCT ccs;
    			UINT iButton, iImageOffset;
    			TBADDBITMAP tbAddBitmap;
    
    			INT idCommands[COUNT_BUTTONS];
    
    			HWND hStatus;
    			int statwidths[] = {100, -1};
    			// Toolbar buttons...
    			TBBUTTON tbb [COUNT_BUTTONS] = {
    				{STD_FILENEW,  0, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, 0},
    				{STD_FILEOPEN, 0, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, 0},
    				{STD_FILESAVE, 0, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, 0},
    
    				{STD_CUT,   0, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, 0},
    				{STD_COPY,  0, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, 0},
    				{STD_PASTE, 0, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, 0},
    				{STD_UNDO,  0, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, 0},
    
    				{5, 0, TBSTATE_ENABLED, TBSTYLE_SEP, 0, 0, 0},
    
    				{TBICON_FIND,  0,  TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, 0 },  // Find
    				{HIST_FORWARD, 0,  TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, 0 },  // Goto
    
    				{5, 0, TBSTATE_ENABLED, TBSTYLE_SEP, 0, 0, 0},
    
    				{TBICON_WORD_WRAP,        0, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, 0 },   // Wordwrap
    				{TBICON_CONV_LINE_BREAKS, 0, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, 0 },   // Convert line breaks
    
    				{5, 0, TBSTATE_ENABLED, TBSTYLE_SEP, 0, 0, 0},
    
    				{TBICON_CALCULATOR, 0, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, 0 }, // Calculator
    			};
    
    			// Toolbar captions. Supposedly strings should be double null terminated...
    			const LPTSTR szToolbarCaptions[] = {
    				TEXT("New\0"), TEXT("Open\0"), TEXT("Save\0"), NULL,
    				TEXT("Cut\0"), TEXT("Copy\0"), TEXT("Paste\0"), TEXT("Undo\0"), NULL,
    				TEXT("Find\0"), TEXT("Goto\0"), NULL,
    				TEXT("Toggle Word Wrap\0"), TEXT("Convert Unix Style Line Breaks\0"), NULL,
    				TEXT("Calculator\0"),
    			};
    
    
    			ccs.hWindowMenu  = GetSubMenu(GetMenu(hwnd), 2);
    			ccs.idFirstChild = ID_MDI_FIRSTCHILD;
    
    			g_hMDIClient = CreateWindowEx(WS_EX_CLIENTEDGE, "mdiclient", NULL,
    						   WS_CHILD | WS_CLIPCHILDREN | WS_VSCROLL | WS_HSCROLL | WS_VISIBLE,
    						   CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
    						   hwnd, (HMENU)IDC_MAIN_MDI, GetModuleHandle(NULL), (LPVOID)&ccs);
    
    			if(g_hMDIClient == NULL)
    				MessageBox(hwnd, "Could not create MDI client.", "Error", MB_OK | MB_ICONERROR);
    
    			// Create a toolbar that has ToolTips associated with it...
    			g_hToolbar = CreateWindowEx(0, TOOLBARCLASSNAME, NULL,
    						 WS_CHILD | TBSTYLE_TOOLTIPS | WS_CLIPSIBLINGS, 0, 0, 0, 0,
    						 hwnd, (HMENU) IDC_MAIN_TOOL, (HINSTANCE)GetModuleHandle(0), NULL);
    
    			if (!g_hToolbar) return;
    
    			// Send the TB_BUTTONSTRUCTSIZE message, which is required for backward compatibility...
    			SendMessage(g_hToolbar, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
    
    			// Prevent text from displaying under buttons...
    			SendMessage(g_hToolbar, TB_SETMAXTEXTROWS, 0, 0);
    
    			// Get the correct menu ids for our OS...
    			//GetCommandIds(idCommands);
    
    			for (iButton = 0;iButton < COUNT_BUTTONS;iButton++) {
    
    				// Set correct menu(command) id for each button...
    				tbb[iButton].idCommand = idCommands[iButton];
    
    				// Add button captions to toolbar string pool...
    				if (szToolbarCaptions[iButton] != NULL)
    					tbb[iButton].iString = SendMessage(g_hToolbar, TB_ADDSTRING, 0, 
    										   (LPARAM) szToolbarCaptions[iButton]);
    				else
    					tbb[iButton].iString = 0;
    			}
    
    			// Disable goto button on NT 4.0 as it does not exist...
    			if (tbb[10].idCommand == 0) tbb[10].fsState = 0; // Turn off TBSTATE_ENABLED
    
    			// Add standard image list...
    			tbAddBitmap.hInst = HINST_COMMCTRL;
    			tbAddBitmap.nID = IDB_STD_SMALL_COLOR;
    			SendMessage(g_hToolbar, TB_ADDBITMAP, 0, (LPARAM) &tbAddBitmap);
    
    			// Add history image list...
    			tbAddBitmap.hInst = HINST_COMMCTRL;
    			tbAddBitmap.nID = IDB_HIST_SMALL_COLOR;
    			iImageOffset = SendMessage(g_hToolbar, TB_ADDBITMAP, 0, (LPARAM) &tbAddBitmap);
    			tbb[10].iBitmap += iImageOffset; // Goto button
    
    			// Add custom images...
    			tbAddBitmap.hInst = hInstance;
    			tbAddBitmap.nID = TOOLBAR_ICONS;
    			iImageOffset = SendMessage(g_hToolbar, TB_ADDBITMAP, 5, (LPARAM) &tbAddBitmap);
    						
    			tbb[9].iBitmap += iImageOffset;  // Find button
    			tbb[12].iBitmap += iImageOffset; // Toggle Word Wrap button
    			tbb[13].iBitmap += iImageOffset; // Convert line breaks button
    			tbb[15].iBitmap += iImageOffset; // Calculator button
    
    			// Add buttons to toolbar...
    			SendMessage(g_hToolbar, TB_ADDBUTTONS, COUNT_BUTTONS, (LPARAM) &tbb);
    
    			// Send a WM_SIZE message to layout controls...
    			SendWMSizeMessage(hwnd);
    
    			// Show toolbar...
    			ShowWindow(g_hToolbar, SW_SHOWNORMAL);
    
    			hStatus = CreateWindowEx(0, STATUSCLASSNAME, NULL,
    					  WS_CHILD | WS_VISIBLE | SBARS_SIZEGRIP, 0, 0, 0, 0,
    					  hwnd, (HMENU)IDC_MAIN_STATUS, GetModuleHandle(NULL), NULL);
    
    			SendMessage(hStatus, SB_SETPARTS, sizeof(statwidths)/sizeof(int), (LPARAM)statwidths);
    			SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Hi there :)");	
    						
    		}
    thanx again

    korbitz

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    54
    okay, i ran an error checker through the code and get an error on this section of code

    Code:
    SendMessage(g_hToolbar, TB_ADDBITMAP, 0, (LPARAM) &tbAddBitmap);
    the error checker reports the following error:

    Not enough storage is available to process this command
    does anyone know why i get this error as i have no idea

    thanx again

    korbitz

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    54
    okay, no matter, got the images displayed, still getting that error with the error checker, but the code is doing what is is meant to be doing so far, so i'm happy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM
  2. Problems adding a user in linux
    By axr0284 in forum Tech Board
    Replies: 1
    Last Post: 12-10-2004, 09:13 AM
  3. using standard bitmaps in the toolbar editor
    By Kibble in forum Windows Programming
    Replies: 0
    Last Post: 12-23-2002, 08:43 PM
  4. Replies: 3
    Last Post: 07-24-2002, 08:46 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM