template <class T> class CEditCommands { public: BEGIN_MSG_MAP(CEditCommands< T >) ALT_MSG_MAP(1) COMMAND_ID_HANDLER(ID_EDIT_CLEAR, OnEditClear) COMMAND_ID_HANDLER(ID_EDIT_CLEAR_ALL, OnEditClearAll) COMMAND_ID_HANDLER(ID_EDIT_COPY, OnEditCopy) //… END_MSG_MAP() //… LRESULT OnEditCopy(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) { T* pT = static_cast<T*>(this); pT->Copy(); return 0; } //…可见平时看到的快捷键最终会转换成一个WM_COMMAND.
class CMyEdit : public CWindowImpl< CMyEdit,CEdit> , CEditCommands < CMyEdit> { BEGIN_MSG_MAP(CMyEdit) //… CHAIN_MSG_MAP_ALT(CEditCommands< CMyEdit>, 1) //… END_MSG_MAP() };理论上这样消息传递就应OK了,但问题是,使用快捷键时没有收到Edit的WM_COMMAND消息。
if (::GetFocus() == m_view1.m_hWnd) { CHAIN_MSG_MAP_MEMBER(m_view1) } if (::GetFocus() == m_view2.m_hWnd) { CHAIN_MSG_MAP_MEMBER(m_view2) }这段代码有点恶心,但不管怎么样,问题能解决了。 一开始的时候,由于急功近利,代码就是这样草草写完,不知其所以。
BOOL CMainFrame::PreTranslateMessage(MSG* pMsg) { if(CFrameWindowImpl<CMainFrame>::PreTranslateMessage(pMsg)) return TRUE; return m_view.PreTranslateMessage(pMsg); }
BOOL PreTranslateMessage(MSG* pMsg) { if(m_hAccel != NULL && ::TranslateAccelerator(m_hWnd, m_hAccel,pMsg)) return TRUE; return FALSE; }