Question

I have some class around this code, let call it ToolbarButton

TBBUTTON tbbutton;
ZeroMemory(&tbbutton, sizeof(tbbutton));  
tbbutton.idCommand = index;
tbbutton.dwData = 0;
tbbutton.fsState = TBSTATE_ENABLED | BSTYLE_BUTTON | BTNS_SHOWTEXT;
tbbutton.iBitmap = I_IMAGENONE;
tbbutton.iString = toolbar->AddStrings(text);

toolbar->InsertButton(index, &tbbutton);

where toolbar is something CToolBarCtrl*

How can I create message loop for ToolbarButton class?

something like

class ToolbarButton : public CMessageMap{
  ..
  BEGIN_MSG_MAP(ToolbarButton )
    MESSAGE_HANDLER(WM_COMMAND, OnClick)
  END_MSG_MAP()

  ..
}

OnClick didn't called, what else should I do?

Update: I also think about the variant from answer - toolbar handles click message, find button by idCommand and call OnClick of founded button. .. But I have a code that I am refactoring and see the button class (to be correct ~ about 4 interfaces and 15 classes around button) that do such syntax sugar what I need, but also they contains obsolete code and code that I want to eliminate and currently I can't slice it

Was it helpful?

Solution

It is supposed to work in a somewhat different way.

  • you don't inherit from message map class, normally windowed classes have message maps
  • toolbar button is not a window; toolbar is the windowed control and button is its internal part without separate handle, without message map; you handle button clicks as notifications from toolbar with specific button identifiers
  • if you want to put a custom button onto toolbar, it should be either (a) customized button, such as owner-drawn, up to extent supported by toolbar control itself, or (b) a fully featured windowed control

I suggest that you check Using Toolbar Controls as for what your options really are.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top