Toolbar help: How to make buttons dynamic using images

Discussion in 'Win32' started by Echelo, Aug 1, 2008.

  1. Echelo

    Echelo New Member

    Joined:
    Aug 1, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I am trying to make a toolbar with dynamic buttons which change the image of the button when the user clicks on it. More specifically to change the index of the iBitmap in TBButton. So far I've been trying to use TB_SETBUTTONINFO but without much luck, so far the message fails with...here's the code so far:

    I have the toolbar setup and buttons setup as follows:
    Code:
    //Globals:
    	TBBUTTON tbb[2];
    	HWND hTool = NULL;
    	HIMAGELIST imagelist;
    
    //WM_CREATE:
    
    			// 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, TEXT("Could not create tool bar."), TEXT("Error"), MB_OK | MB_ICONERROR);
    
    			// Send the TB_BUTTONSTRUCTSIZE message, which is required for
    			// backward compatibility.
    			SendMessage(hTool, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
    imagelist = ImageList_Create(32, 32, ILC_COLOR32, 3, 3);
    			HBITMAP hToolIcons = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_TOOLBAR));
    			ImageList_Add(imagelist, hToolIcons, NULL);
    
    			SendMessage(hTool, TB_SETIMAGELIST, 0, (LPARAM)(HIMAGELIST)imagelist);
    
    			ZeroMemory(tbb, sizeof(tbb));
    			tbb[0].iBitmap = 0; 
    			tbb[0].fsState = TBSTATE_ENABLED;
    			tbb[0].fsStyle = TBSTYLE_BUTTON;
    			tbb[0].idCommand = ID_FILE_CONNECT;
    
    			tbb[1].iBitmap = 2;
    			tbb[1].fsState = TBSTATE_ENABLED;
    			tbb[1].fsStyle = TBSTYLE_BUTTON;
    			tbb[1].idCommand = ID_FILE_SELECTSERVER;
    
    			SendMessage(hTool, TB_ADDBUTTONS, sizeof(tbb)/sizeof(TBBUTTON), (LPARAM)&tbb);
    
    
    //and when the user clicks on the button I have the following code:
    		case ID_FILE_CONNECT:
    			{
    				if(!g_CONNECTED)
    				{	
    					CreateNewMDIChild(g_hMDIClient);
    					TBBUTTONINFO tbInfo;
    					tbInfo.cbSize = sizeof(tbInfo);
    					tbInfo.dwMask = TBIF_IMAGE;
    					tbInfo.iImage = 1;
    					//tbb[0].iBitmap = 1; 
    					int i = SendMessage(hwnd, TB_SETBUTTONINFO,(WPARAM)ID_FILE_CONNECT, (LPARAM)&tbInfo);
    					if(i == NULL)
    						MessageBox(hwnd, TEXT("Could not update tool bar."), TEXT("Error"), MB_OK | MB_ICONERROR);
    					
    				}
    			}
    			break;
    		}
    		break;
    Can anyone help? maybe Im doing something wrong.
     
  2. Spogliani

    Spogliani New Member

    Joined:
    Jun 24, 2008
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Milano, Italy
    Hi, I've never worked with toolbars but I had similar problems with other controls.
    I think the problem is that you do not any value for "idCommand" in the tbInfo structure, while managing the message.

    Try to add something like
    Code:
    tbInfo.idCommand = ID_FILE_CONNECT;
    
    I think this may be the solution for your problem becouse of the info found at http://msdn.microsoft.com/en-us/library/bb760476(VS.85).aspx

    I hope this can help you
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice