如何更改 CMFCToolBar 中的按钮位置?

How to change button position in CMFCToolBar?

我可以更改 CMFCToolBar 中某些按钮的位置吗?例如在工具栏的开头设置第三个按钮。

把旧的按钮拿下来,插入新的位置,删除旧的。

// Get button at position 3 and move it to position 1
auto *pButton = bar.Getbutton(2);
bar.InsertButton(*pButton,0);
bar.RemoveButton(2);

因为 InsertButton 创建了一个副本,所以取消引用指针是省事的。

我的几个按钮版本:

// pIDs - array of buttons IDs
// nIDCounts - size of array
void ArrangeButtons(CMFCToolBar& wnd, const UINT* pIDs, UINT nIDCounts) 
{

    std::vector<std::unique_ptr<CMFCToolBarButton>> aBtns;
    std::transform(pIDs, std::next(pIDs, nIDCounts), std::inserter(aBtns, std::end(aBtns)), [&](UINT nID)-> std::unique_ptr < CMFCToolBarButton > {
            std::unique_ptr<CMFCToolBarButton> res;
            CMFCToolBarButton* pBtn = wnd.GetButton(wnd.CommandToIndex(nID));
            if ((pBtn != nullptr) && pBtn->IsKindOf(RUNTIME_CLASS(CMFCToolBarButton))) {
                    res.reset(STATIC_DOWNCAST(CMFCToolBarButton, pBtn->GetRuntimeClass()->CreateObject()));
                    res->CopyFrom(*pBtn);
            }
            return std::move(res);
    });

    wnd.RemoveAllButtons();
    std::for_each(std::begin(aBtns), std::end(aBtns), [&](std::unique_ptr<CMFCToolBarButton>& btn) {
            if (btn) {
                    wnd.InsertButton(*btn);
            }
    });
}