gpt4 book ai didi

c++ - MFC Ribbon - 从命令中获取基本元素

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:36:30 24 4
gpt4 key购买 nike

我在 MFC 应用程序的功能区上有一个 CMFCRibbonUndoButton。单击其 ID 时,我有一个处理程序 (ON_COMMAND(ID_EDIT_UNDO, ...))。但是,当按钮也在快速访问工具栏 (QAT) 中时,显然有两个 CMFCRubbonUndoButtons,每个都保持自己的状态。在命令处理程序中,我不知道如何判断点击了哪个,如果您调用了错误的 GetActionNumber(),您将返回错误的撤消操作数。

在我的 ON_COMMAND 处理程序中有没有办法获取触发事件的 CMFCRibbonBaseElement*

编辑:答案对我很重要,问题有点晦涩,但我悬赏了!

编辑:这是它添加到 QAT 的方式:

CList<UINT, UINT> lstQATCmds;
lstQATCmds.AddTail(ID_EDIT_UNDO);
m_RibbonBar.SetQuickAccessCommands(lstQATCmds);

最佳答案

Is there a way in my ON_COMMAND handler to get the CMFCRibbonBaseElement* that fired the event?

不直接,不。 WM_COMMAND 消息是从 CMFCRibbonBaseElement::NotifyCommand 发送的,该消息的参数中不包含指针。

为了能够知道从 ON_COMMAND 处理程序中单击了哪个撤消按钮,我编写了此类,它继承了 CMFCRibbonUndoButton。这段代码的作用是在每次单击其中一个按钮或激活弹出菜单时存储指向最后激活的“撤消”按钮的指针。

// CMyMFCRibbonUndoButton.h

class CMyMFCRibbonUndoButton : public CMFCRibbonUndoButton
{
DECLARE_DYNCREATE(CMyMFCRibbonUndoButton)

public:
CMyMFCRibbonUndoButton();
CMyMFCRibbonUndoButton(UINT nID, LPCTSTR lpszText,
int nSmallImageIndex = -1, int nLargeImageIndex = -1);

virtual void OnClick(CPoint point);
virtual void OnShowPopupMenu();

static CMyMFCRibbonUndoButton* GetLastActivated();

private:
static CMyMFCRibbonUndoButton* s_pLastActivated;
};

// CMyMFCRibbonUndoButton.cpp

IMPLEMENT_DYNCREATE(CMyMFCRibbonUndoButton, CMFCRibbonUndoButton)

CMyMFCRibbonUndoButton* CMyMFCRibbonUndoButton::s_pLastActivated = NULL;

CMyMFCRibbonUndoButton::CMyMFCRibbonUndoButton()
{
}

CMyMFCRibbonUndoButton::CMyMFCRibbonUndoButton(UINT nID, LPCTSTR lpszText,
int nSmallImageIndex, int nLargeImageIndex) :
CMFCRibbonUndoButton(nID, lpszText, nSmallImageIndex, nLargeImageIndex)
{
}

void CMyMFCRibbonUndoButton::OnClick(CPoint point)
{
s_pLastActivated = this;
CMFCRibbonUndoButton::OnClick(point);
}

void CMyMFCRibbonUndoButton::OnShowPopupMenu()
{
s_pLastActivated = this;
CMFCRibbonUndoButton::OnShowPopupMenu();
}

CMyMFCRibbonUndoButton* CMyMFCRibbonUndoButton::GetLastActivated()
{
return s_pLastActivated;
}

在初始化功能区栏时,使用此类代替 CMFCRibbonUndoButton。在您的处理程序函数中,调用 GetLastActivated() 来检索此指针,例如:

void CMyTestDoc::OnEditUndo()
{
CMyMFCRibbonUndoButton* pUndoButton =
CMyMFCRibbonUndoButton::GetLastActivated();

ASSERT_VALID(pUndoButton);

if (pUndoButton != NULL)
{
int ActionNumber = pUndoButton->GetActionNumber();
// etc.
}
}

当然,这有点 hack,但这是我能找到的解决问题的唯一方法。

无论如何,我希望这对您有所帮助,

克里斯

关于c++ - MFC Ribbon - 从命令中获取基本元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3353769/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com