本文介绍了如何使子控件处理父CView的加速器命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CFormView和一个子CListCtrl控件.我能应付加速器事件,例如CFormView中的Ctrl + C,Ctrl + V ...问题,方法是定义以下消息处理程序:

I have a CFormView, and a child CListCtrl control. I can handle accelerator events, like Ctrl+C, Ctrl+V ... in CFormView without problem, by defining below message handler:

    ON_COMMAND(ID_EDIT_COPY, &CMyFormView::OnEditCopy) 

现在,我希望CListCtrl对这些命令的处理方式有所不同.我想要在CListCtrl类中实现OnEditCopy,而不是实现逻辑在视图类中.如何将加速器事件从CView传递到子控件,当CListCtrl处于焦点时?我尝试过:

Now I want my CListCtrl handle these commands differently. I want to implement OnEditCopy in CListCtrl class, rather than implement logic in the view class. How can I pass the accelerator events from CView to child control, when CListCtrl is on focus? I tried like:

    ON_CONTROL_REFLECT(ID_EDIT_COPY, &CMyListCtrl::OnEditCopy) 

但是它不起作用.

推荐答案

在CMyListCtrl中使用相同的ON_COMMAND宏.

Use same ON_COMMAND macro in CMyListCtrl.

  ON_COMMAND(ID_EDIT_COPY, &CMyListCtrl::OnEditCopy)  

您要做的就是覆盖OnCmdMsg方法.

All you have to do is overriding OnCmdMsg method.

BOOL CMyFormView::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
{
    if (GetFocus() == m_myListCtrl
        && m_myListCtrl->OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
        return TRUE;
    return CMyFormView::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}

(m_myListCtrl是CMyListCtrl实例指针.)

(m_myListCtrl is the CMyListCtrl instance pointer.)

这将使所有WM_COMMAND消息首先在m_myListCtrl(如果它是焦点窗口)中处理.

This make all WM_COMMAND message first handled in m_myListCtrl if its the focus window.

这篇关于如何使子控件处理父CView的加速器命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 00:44