Re: How to make child control handle accelerator command of parent CView?
On Dec 31 2010, 1:17 pm, yshi <sy8...@gmail.com> wrote:
Hi, all:
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)
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, &ThisClass::OnEditCopy)
But it doesn't work.
Try this: in your view class, override OnCmdMsg, like this:
BOOL CMyView::OnCmdMsg(
UINT nID,
int nCode,
void* pExtra,
AFX_CMDHANDLERINFO* pHandlerInfo
)
{
if (__super::OnCmdMsg(nID etc))
return TRUE; // View handled the message
CWnd* p = GetFocus();
if (!p) return FALSE; // No window with focus.
if (p->GetParent() != this) return FALSE; // Window with focus not
our child.
return GetFocus()->OnCmdMsg(nID etc));
}
Then, for commands that you want to pass to CListCtrl, don't create
ON_COMMAND handler in CMyView, but rather, let them go through to the
child with focus.
Idea here is that you use MFC command routing mechanics, allowing you
to place your ON_COMMAND and ON_COMMAND_UPDATE macros where you want
them to be handled.
Goran.