2017-08-04 98 views
1

我遇到了家庭絲帶按鈕的奇怪行爲。
我已經在帶有帶控件的Office模板的Visual Studio 2010中創建了標準MFC應用程序。但是,如果我雙擊位於上部位置的Home Ribbon按鈕,應用程序將關閉。
你能告訴我,如果它是標準的MFC應用程序處理程序的行爲,我可以如何改變它?
我看過Prevent double click on MFC-Dialog button,但不能應用到我的情況(更清楚地 - 我不知道如何將雙擊處理程序添加到功能區主頁按鈕)。MFC絲帶首頁按鈕關閉雙擊的應用程序

回答

0

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); 
} 

另一種解決方案:覆蓋的PreTranslateMessage在CMainFrame類;

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); 
} 
0
  1. 派生自己的派生類CMFCRibbonApplicationButton。
  2. 爲CMFCRibbonApplicationButton創建消息處理程序:: OnLButtonDblClk
  3. 提供您自己的雙擊實現。如果什麼都不應該發生,就把身體留空。
  4. 在您的CMainFrame中,您可以找到CMFCRibbonApplicationButton m_MainButton的定義。用你的實現替換類名稱。
相關問題