【问题标题】:MFC ribbon home button closes app with double clickMFC 功能区主页按钮通过双击关闭应用程序
【发布时间】:2017-08-04 15:02:28
【问题描述】:
我遇到了主页功能区按钮的奇怪行为。
我在 Visual Studio 2010 中使用具有功能区控件的 Office 模板创建了标准 MFC 应用程序。但是,如果我双击上方位置的主页功能区按钮,应用程序将关闭。
您能否告诉我这是否是标准的 MFC 应用程序处理程序行为以及如何更改它?
我查看了Prevent double click on MFC-Dialog button,但无法将其应用于我的案例(更清楚的是 - 我不知道如何将双击处理程序添加到功能区主页按钮)。
【问题讨论】:
标签:
c++
visual-studio
mfc
【解决方案1】:
CMFCRibbonApplicationButton 不是从 CWnd 派生的,因此无法处理 WM_LBUTTONDBLCLK 消息。
一种解决方案是从 CMFCRibbonBar 派生。
class CCustomRibbonBar : public CMFCRibbonBar
{
// ...
protected:
DECLARE_MESSAGE_MAP()
afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);
};
BEGIN_MESSAGE_MAP(CCustomRibbonBar, CMFCRibbonBar)
ON_WM_LBUTTONDBLCLK()
END_MESSAGE_MAP()
void CCustomRibbonBar::OnLButtonDblClk(UINT nFlags, CPoint point)
{
CMFCRibbonBaseElement* pHit = HitTest(point);
if (pHit->IsKindOf(RUNTIME_CLASS(CMFCRibbonApplicationButton)))
{
// the user double-clicked in the application button
// do what you want here but do not call CMFCRibbonBar::OnLButtonDblClk
return;
}
CMFCRibbonBar::OnLButtonDblClk(nFlags, point);
}
另一种解决方案:在 CMainFrame 类中覆盖 PreTranslateMessage;
BOOL CMainFrame::PreTranslateMessage(MSG* pMsg)
{
if ((WM_LBUTTONDBLCLK == pMsg->message) && (pMsg->hwnd == m_wndRibbonBar))
{
CPoint point(pMsg->pt);
m_wndRibbonBar.ScreenToClient(&point);
CMFCRibbonBaseElement* pHit = m_wndRibbonBar.HitTest(point);
if (pHit && pHit->IsKindOf(RUNTIME_CLASS(CMFCRibbonApplicationButton)))
{
// do what you want but do not call CMDIFrameWndEx::PreTranslateMessage
return TRUE; // no further dispatch
}
}
return CMDIFrameWndEx::PreTranslateMessage(pMsg);
}
【解决方案2】:
- 派生您自己的 CMFCRibbonApplicationButton 派生类。
- 为 CMFCRibbonApplicationButton::OnLButtonDblClk 创建消息处理程序
- 在双击时提供您自己想要执行的操作。如果什么都没有发生,就让身体空着。
- 在 CMainFrame 中,您可以找到 CMFCRibbonApplicationButton m_MainButton 的定义。将类名替换为您的实现。