简体   繁体   English

MFC功能区主页按钮通过双击关闭应用程序

[英]MFC ribbon home button closes app with double click

I've faced with strange behaviour of a home ribbon button. 我遇到了首页功能区按钮的奇怪行为。
I've created standard MFC application in Visual Studio 2010 with Office template that has a ribbon control. 我已经在Visual Studio 2010中使用具有功能区控件的Office模板创建了标准的MFC应用程序。 But if I double click on the Home ribbon button at the upper position the application is closed. 但是,如果我双击上方的“首页”功能区按钮,则该应用程序将关闭。
Could you please tell me if it is standard MFC application handlers behaviour and how I can change it? 您能否告诉我这是否是MFC应用程序处理程序的标准行为,以及如何进行更改?
I've looked at Prevent double click on MFC-Dialog button but couldn't apply it to my case (more clearly - I don't know how to add double click handler to a ribbon home button). 我看过防止在MFC-Dialog按钮上双击,但是无法将其应用于我的情况(更清楚地讲-我不知道如何将双击处理程序添加到功能区主页按钮)。

  1. Derive your own derived class of CMFCRibbonApplicationButton. 派生您自己的CMFCRibbonApplicationButton的派生类。
  2. Create a message handler for CMFCRibbonApplicationButton::OnLButtonDblClk 为CMFCRibbonApplicationButton :: OnLButtonDblClk创建消息处理程序
  3. Provide your own implementation of what you want to do on the double click. 提供您想要双击执行的操作。 If nothing should happen, just leave the body empty. 如果什么也没发生,那就让身体空着。
  4. In your CMainFrame you find a definition of CMFCRibbonApplicationButton m_MainButton. 在您的CMainFrame中,您可以找到CMFCRibbonApplicationButton m_MainButton的定义。 Replace the class name with your implementation. 用您的实现替换类名。

CMFCRibbonApplicationButton is not derived from CWnd so cannot handle WM_LBUTTONDBLCLK message. CMFCRibbonApplicationButton不是从CWnd派生的,因此无法处理WM_LBUTTONDBLCLK消息。 One solution is to derive from CMFCRibbonBar . 一种解决方案是从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);
}

Another solution: override PreTranslateMessage in CMainFrame class; 另一种解决方案:在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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM