How to Enable Edit Menu Commands for Edit ControlsID: Q145616
|
On a CDialog or other window that has no edit menu options for cutting,
copying, and pasting, CEdit controls handle these functions through the
normal Windows accelerator processing (CTRL+X,V,C, or the SHIFT-DELETE,
SHIFT-INSERT, CTRL-INSERT, CTRL-DELETE, and so on keys). However, in a
typical MDI or SDI application, these options are disabled.
This is due to the default implementation for menu handlers and accelerator
keys that AppWizard adds to your application to handle these functions.
These menu handlers get the accelerator keystrokes instead of your edit
control. This article shows you how to implement the edit control's
clipboard functions on a CFormView-derived form.
If you completely remove the menu options (not just enable or disable) and
any accelerator definitions for the standard edit accelerator keys, then
there will be no conflict and your CEdit controls will take care of their
clipboard functions as usual. However, if you want to implement the common
Edit menu clipboard functions (and toolbar Edit buttons), you will have to
add some additional handlers to your program.
// IsEdit: a helper function to determine if a given CWnd pointer
// points to a CEDit control.
// Use the SDK ::GetClassName() function because MFC IsKindOf
// fails if no CEdit variable has been created for the control you're
// trying to test.
BOOL CMyFormView::IsEdit( CWnd* pWnd )
{
ASSERT( pWnd != NULL );
HWND hWnd = pWnd->GetSafeHwnd();
if (hWnd == NULL)
return FALSE;
TCHAR szClassName[6];
return ::GetClassName(hWnd, szClassName, 6) &&
_tcsicmp(szClassName, _T("Edit")) == 0;
}
// UPDATE_COMMAND_UI handler for Edit Copy and Edit Cut which both
// require that the current focus is on an edit control that has
// text selected.
void CMyFormView::OnUpdateNeedSel(CCmdUI* pCmdUI)
{
// get the current focus & determine if its on a CEdit control
CWnd* pWnd = GetFocus();
if (NULL == pWnd || !IsEdit( pWnd ))
{
pCmdUI->Enable( FALSE );
}
else
{
CEdit* pEdit = (CEdit*)pWnd;
int nBeg, nEnd;
pEdit->GetSel( nBeg, nEnd );
pCmdUI->Enable( nBeg != nEnd );
}
}
// UPDATE_COMMAND_UI handlers for Edit Paste requires that focus be
// on an edit control and that the clipboard contains text to be
// pasted into the control.
void CMyFormView::OnUpdateNeedClip(CCmdUI* pCmdUI)
{
// get the current focus & determine if its on a CEdit control
// also check to see if the control is read-only.
CWnd* pWnd = GetFocus();
if ( NULL == pWnd ||
!IsEdit( pWnd ) ||
(pWnd->GetStyle() & ES_READONLY) != 0 )
{
pCmdUI->Enable( FALSE );
}
else
pCmdUI->Enable(::IsClipboardFormatAvailable(CF_TEXT));
}
void CMyFormView::OnEditCopy()
{
CEdit* pEdit = (CEdit*)GetFocus();
ASSERT( IsEdit( pEdit) );
pEdit->Copy();
}
void CMyFormView::OnEditCut()
{
CEdit* pEdit = (CEdit*)GetFocus();
ASSERT( IsEdit( pEdit) );
pEdit->Cut();
}
void CMyFormView::OnEditPaste()
{
CEdit* pEdit = (CEdit*)GetFocus();
ASSERT( IsEdit( pEdit) );
ASSERT(::IsClipboardFormatAvailable(CF_TEXT));
pEdit->Paste();
}
Additional query words: kbinf 1.00 1.50 2.50 2.51 2.52 2.00 2.10 2.20 3.00 3.10 3.20 4.00
Keywords : kbcode kbMFC KbUIDesign kbVC
Version : 1.00 1.50 1.51 1.52 | 2.00 2.10
Platform : NT WINDOWS
Issue type :
Last Reviewed: July 28, 1999