Article ID: 167960
Article Last Modified on 1/5/2006
BOOL CMyXxx::PreTranslateMessage(MSG* pMsg)
{
// If edit control is visible in tree view control, when you send a
// WM_KEYDOWN message to the edit control it will dismiss the edit
// control. When the ENTER key was sent to the edit control, the
// parent window of the tree view control is responsible for updating
// the item's label in TVN_ENDLABELEDIT notification code.
if (pMsg->message == WM_KEYDOWN &&
pMsg->wParam == VK_RETURN || pMsg->wParam == VK_ESCAPE)
{
CEdit* edit = m_TreeCtrl.GetEditControl();
if (edit)
{
edit->SendMessage(WM_KEYDOWN, pMsg->wParam, pMsg->lParam);
return TRUE;
}
}
// CXxxx can be a CFormView, Cdialog, or CPropertyPage class.
return CXxxx::PreTranslateMessage(pMsg);
}
For Visual C++ 2.xx, since PreTranslateMessage() is not called for
modal CDialog-derived classes, the sample code above does not apply to the
modal dialog and property page in a modal CPropertySheet derived class. In this
situation, those keystroke messages can be trapped in the overridden OnOK()
(for Enter key) and OnCancel() (for Esc key) functions in either CDialog or
CPropertySheet derived-class. The sample code works fine even in the absence of
both the OK and Cancel buttons from the dialog resource template.
// Set MODAL_PROPERTYSHEET to 1 for modal CPropertySheet-derived class
// and 0 for CDialog-derived class.
#define MODAL_PROPERTYSHEET 1
BOOL CMyDxxx::IsTreeCtrlEditMessage(WPARAM KeyCode)
{
BOOL rvalue = FALSE;
// pWnd is a pointer to either an active CPropertyPage of the modal
// CPropertySheet or a modal CDialog object.
CWnd* pWnd = this;
#if MODAL_PROPERTYSHEET
pWnd = GetActivePage();
#endif
// IDC_TREECTRL is the ID of the tree view control.
CTreeCtrl *treectrl = (CTreeCtrl *) pWnd->GetDlgItem(IDC_TREECTRL);
if (!treectrl)
return rvalue;
// If the edit control of the tree view control has the input focus,
// sending a WM_KEYDOWN message to the edit control will dismiss the
// edit control. When ENTER key was sent to the edit control, the
// parentwindow of the tree view control is responsible for updating
// the item's label in TVN_ENDLABELEDIT notification code.
CWnd* focus = GetFocus();
CEdit* edit = treectrl->GetEditControl();
if ((CEdit *) focus == edit)
{
edit->SendMessage(WM_KEYDOWN, KeyCode);
rvalue = TRUE;
}
return rvalue;
}
void CMyDxxx::OnOK()
{
// Do not dismiss the dialog object if ENTER key was sent to the tree
// view's edit control. You may call the CDialog::OnOK() function if
// this is for CDialog.
if (!IsTreeCtrlEditMessage(VK_RETURN))
EndDialog(IDOK);
}
void CMyDxxx::OnCancel()
{
// Do not dismiss the dialog object if ESC key was sent to the tree
// view's edit control. You might call the CDialog::Cancel()
// function if this is for CDialog.
if (!IsTreeCtrlEditMessage(VK_ESCAPE))
EndDialog(IDCANCEL);
}
130691 ESC and ENTER keys do not work when you edit labels in TreeView
Keywords: kbtshoot kbbug kbmfc kbtreeview kbuidesign kbcode KB167960