Handling ESC and Return Keys While Editing a Label
When I first implemented the tree view control and tried editing a label, the only way I could finish editing was to click elsewhere. MFC has been designed in such a way that the parent window gets a chance at the messages using the PreTranslateMessage() virtual function. If the parent window is a CDialog or a CFormView, the ESC and the return key are handled by the parent and does not reach the control. To allow our control to process some important messages we need to override the PreTranslateMessage() function. In our code below, we also also allow the control key combinations to go through to the control. This allows copy, cut and paste using the control key combinations.
BOOL CTreeCtrlX::PreTranslateMessage(MSG* pMsg)
{
if( pMsg->message == WM_KEYDOWN )
{
// When an item is being edited make sure the edit control
// receives certain important key strokes
if( GetEditControl()
(pMsg->wParam == VK_RETURN
|| pMsg->wParam == VK_DELETE
|| pMsg->wParam == VK_ESCAPE
|| GetKeyState( VK_CONTROL)
)
)
{
::TranslateMessage(pMsg);
::DispatchMessage(pMsg);
return TRUE; // DO NOT process further
}
}
return CTreeCtrl::PreTranslateMessage(pMsg);

Comments
THANKS
Posted by sam1982129 on 07/26/2006 05:15amOK
ReplyIt works
Posted by ambhagat on 04/13/2005 10:43amThanks Zafir - you saved me loads of time
ReplyIt also works for CDialogBar
Posted by Legacy on 01/30/2004 12:00amOriginally posted by: HB
I was having the same problem with my derived CTreeCtrl on a CDialogBar. I knew that I had to override pretranslate but I was doing this instead.
{
WPARAM wparam=IDC_TREE_MAIN;
LPARAM lparam=(LPARAM)&nmhdr
nmhdr.idFrom=IDC_TREE_MAIN;
nmhdr.hwndFrom=GetSafeHwnd();
nmhdr.code=TVN_ENDLABELEDIT;
SendMessage(WM_NOTIFY, wparam, lparam);
return TRUE;
}
so it was generating the TVN_ENDLABELEDIT message, but it wasn't actually ending the edit.
Thanks for the solution!
PS: A bunch of these MFC controls need to be redone.
Reply
This is cool!! and so simple
Posted by Legacy on 02/14/2003 12:00amOriginally posted by: tudul
.
ReplyAn easier way...
Posted by Legacy on 04/05/2000 12:00amOriginally posted by: Byron Montgomerie
ReplyEasier way to cancel label editing
Posted by Legacy on 08/25/1999 12:00amOriginally posted by: Lisa Goldsmith
ReplyOverride PreTranslateMessage() of dialog; don't use TVN_KEYDOWN
Posted by Legacy on 07/05/1999 12:00amOriginally posted by: Damian Biollo
You can put the code in the dialog class or the tree control.
ReplyON_NOTIFY(TVN_KEYDOWN ...) will not work because when you edit labels, because an Edit control gets created on the fly ... so the tree control is not receiving the key down messages.
But if you want to handle key presses in the tree (eg. the Delete key) when NOT editing a label use TVN_KEYDOWN.
Is the code correct
Posted by Legacy on 05/26/1999 12:00amOriginally posted by: P�l K. T�nder
I tried the above code, and it only worked if I overrode
Replythe PreTranslateMessage method of the dialog, not the
control
ON_NOTIFY(TVN_KEYDOWN vs PreTranslateMessage
Posted by Legacy on 04/19/1999 12:00amOriginally posted by: Alexander Pavlovic
Hmm, I thought MFC CTreeCtrl provides ON_NOTIFY(TVN_KEYDOWN
macro for setting handler that catches any keypress within
tree control. Overrides of PreTranslateMessage for any particular reason?
Reply