tags:

views:

45

answers:

2

I can't find a simple CToolBar example of all things..

I created a toolbar in the resource editor, and loaded the toolbar in my code like this:

toolbar = new CToolBar;
toolbar->CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_ALIGN_TOP, CRect(0, 0, 0, 0), AFX_IDW_TOOLBAR);
toolbar->LoadToolBar(IDR_TOOLBAR);

I have also handled WM_LBUTTONDOWN in the message map.

Now, all the buttons are grayed out.. am I missing a step somewhere?

Thanks for any help/links =)

+1  A: 

Have you supplied an ON_COMMAND handler in the message map? Should look something like ON_COMMAND(SOME_BUTTON_ID, OnSomeButton) where SOME_BUTTON_ID corresponds to one of the buttons on your toolbar.

PSU
+2  A: 

The buttons in a toolbar are disabled in MFC by default. For each button, which should be associated with a command ID, you need to have an ON_UPDATE_COMMAND_UI macro in your message map. The handler function you use in this macro will be called whenever the app is idle. This function can call the member functions of the CCmdUI pointer that is passed to the function, such as the Enable method.

Mark Ransom
Thanks. That did it. I think it's a little dumb to have the buttons disabled by default though =/
Nick
@Nick, the buttons are disabled by default because MFC assumes you will add command handlers through the built in wizard. Until a command handler is defined it doesn't make sense to have the button enabled, does it? I've noticed that Microsoft does this often, optimizing a common case at the expense of alternates.
Mark Ransom
If I wanted to implement a command handler, it would be run for all controls, on every update, including the one on creation. What difference would it make if it initially disabled some buttons, or initially enabled them? BUT if I simply wanted my project ready to go, I would be stuck adding extra code.. I don't see how this is an optimization =/
Nick