0

我有一些关于这段代码的类,我们称之为 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);

其中工具栏是 CToolBarCtrl*

如何为 ToolbarButton 类创建消息循环?

就像是

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

  ..
}

OnClick 没有调用,我该怎么办?

更新:我还考虑了答案的变体-工具栏处理单击消息,通过 idCommand 查找按钮并调用已创建按钮的 OnClick。.. 但是我有一个代码,我正在重构并看到按钮类(正确的~大约 4 个接口和按钮周围的 15 个类)执行我需要的这种语法糖,但它们也包含过时的代码和我想要的代码消除,目前我无法切片

4

1 回答 1

1

它应该以某种不同的方式工作。

  • 您不是从消息映射类继承的,通常窗口类具有消息映射
  • 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.

于 2011-10-10T18:46:22.980 回答