Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 379
  • Last Modified:

Dynamic Toolbars

I need to know how to load a toolbar.bmp file into the CToolbar class at run time.  The bmp file is not a resource therefore I will not have a resource id.  Also it can not be a resource as toolbars will be user defineable and creatable at runtime through a basic maintenance program.  The user can have as many toolbars as they want but can only load one at a time.  Since I don't know what buttons will be in the toolbar I cannot create a resource.  Also the bitmap file created may contain any number of buttons. It's not just one button.
0
msherw
Asked:
msherw
  • 2
  • 2
1 Solution
 
Tommy HuiCommented:
You can create a CToolBar without any bitmaps or buttons loaded. Then at runtime, you can use CToolBar::GetToolBarCtrl() to retrieve the underlying CToolBarCtrl. You can then create a CBitmap at runtime with which you use the CToolBarCtrl::AddBitmap() function. After loading the bitmap, be sure to also use CToolBarCtrl::AddButton to specify the button information.

0
 
msherwAuthor Commented:
Best I can tell CBitmap wants a resource identifier also. I don't have a resource Identifier. The bitmaps in question are not part of the runtime executable either. It is possible that they will be created months after the executable is made. I'm working with Loadimage to get a handle and using the SetBitmap member of CToolbar but all the buttons seem to be disabled with the exception of the first button.  Again since I DON'T HAVE A RESOURCE IDENTIFIER for the bitmap or the buttons I'm using SetButton(NULL, nobuttons) and SetButtonInfo in a for loop just arbitrarily assigning the counter value to the button and image id.  
0
 
gelbertCommented:
Advice about using CToolBatCtrl::AddBitmap()  was right. MFC provides two implementations of this function one of them takes resource ID as you sad but another takes pointer to CBitmap object which you can create at run-time.
0
 
msherwAuthor Commented:
Ok, so I've tried the CBitmap object, the buttons are still disabled even though I have set the state to enabled. If I use       m_wndToolBarCtl.AddButtons( ButtonCount,ptbButton);
it adds the buttons all right and they are enabled but they don't work like buttons. When you press them they do look pressed or anything. So I tried it a different way.  I use m_wndToolBarCtl to add the Bitmap then send a message to the m_wndToolBar which is the CToolBar class to add the buttons
      m_wndToolBar.SendMessage(TB_ADDBUTTONS,(WPARAM)(UINT)ButtonCount,(LPARAM)ptbButton);
It adds them and they act like buttons, the only problem is that everyone except the first is disabled.
Here is my TBButton array
      for (i=0; i < ButtonCount; i++)
      {
            ptbButton[i].iBitmap = i;
            ptbButton[i].idCommand =  i;
              ptbButton[i].fsState = TBSTATE_ENABLED;
            ptbButton[i].fsStyle =  TBSTYLE_BUTTON ;
            ptbButton[i].dwData = 0L;
            ptbButton[i].iString = NULL;

      }

as you can see I have set the state to enabled for all the buttons.  So what's the deal?

      
0
 
gelbertCommented:
I think your problem is wrong value of command IDs for your buttons. Try to use values in range of 32777-33000. And make sure that you have message handlers for these IDS
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now