wxRibbonButtonBar :在运行时更改按钮的图像

wxRibbonButtonBar : Changing a Button's Image at Runtime

我一直在寻找一种更好 的方法来在运行时更改 wxRibbonButtonBar 上按钮的图像。在 MS Excel 中,当一个或多个单元格的背景发生变化时,功能区按钮会反映最后选择的颜色。因此,我的目标是实现类似的目标。我想到了两种可能的方法:

第一种方法:有两个功能:

1) AddButton (int button_id,..,const wxBitmap &bitmap,...)

2) DeleteButton (int button_id)

由于按钮的 ID 是已知的,所以我想调用 DeleteButton,然后在需要更改位图时调用 AddButton。虽然这可行,但我怀疑这是一个好方法。

另一种可能的方法:因为AddButton函数return是一个指向wxRibbonButtonBarButtonBase的指针并且有下面的函数

void SetItemClientData (wxRibbonButtonBarButtonBase *item, void *data)

return 值 wxRibbonButtonBarButtonBase 可以作为参数传递给它以指向特定按钮。然而,在这里,我不确定 data 到底指的是什么参数(因为按钮可以有标题、位图等...)以及如何将 wxBitmap 作为 data 传递给这个函数。

以下代码有效,它混合了第一种和第二种方法,更多的是第一种;但是,我怀疑这是最佳方式。

wxColourDialog dlg(this);

wxColour color;
if (dlg.ShowModal() == wxID_OK) color = dlg.GetColourData().GetColour(); else return;

wxMemoryDC dc;
wxBitmap bmp(bucket_xpm); //32 by 32
dc.SelectObject(bmp);
dc.SetBrush(color);
dc.DrawRectangle(0, 28, 32, 32);

int itemID=m_ribbonButtonBarFormat->GetItemId(m_BtnFillColor);
m_ribbonButtonBarFormat->DeleteButton(itemID);
m_BtnFillColor=m_ribbonButtonBarFormat->AddButton(itemID, wxT("Fill Color"), bmp, wxEmptyString);
m_ribbonButtonBarFormat->Realize();

如有任何想法,我们将不胜感激,如果这有利于第二种方法,那么代码片段将大有帮助。

我的目标是“我一直在寻找一种更好的方法来在运行时更改 wxRibbonButtonBar 上按钮的图像。在 MS Excel 当一个或多个单元格的背景发生变化时,功能区按钮会反映最后选择的颜色。"

我的方法的缺点是我的目标是更改 wxRibbonButtonBar 的位图。事实上,Excel 通过 混合按钮工具 实现了这一点,当点击下拉工具时,它会显示调色板和然后一旦你点击按钮,它就会应用选定的颜色(虽然我仍然不知道如何显示浮动调色板)。

因此,我将方法从 wxRibbonButtonBar 更改为 wxRibbonToolBar 并使用以下代码添加了一个混合按钮:

m_ribbonToolBarFormat->AddHybridTool(ID_FORMATFILLCOLOR, bmp, wxT("Fill Color"));

混合工具可以生成两个事件:1) OnRibbonToolClicked 2) OnRibbonToolDropdownClicked

OnRibbonToolDropdownFillColorClicked(wxRibbonToolBarEvent& event)
{
    wxColourDialog dlg(this);

    if (dlg.ShowModal() == wxID_OK) m_LastChosenFillColor = dlg.GetColourData().GetColour(); else return;

    wxMemoryDC dc;
    wxBitmap bmp(bucket_xpm);
    dc.SelectObject(bmp);
    dc.SetBrush(m_LastChosenFillColor);
    dc.DrawRectangle(0, 28, 32, 32);

    m_ribbonToolBarFormat->SetToolNormalBitmap(ID_FORMATFILLCOLOR, bmp);
    m_ribbonToolBarFormat->Refresh();

有了SetToolNormalBitmap,我可以不删除工具可以设置工具的图像在run-time.