Article ID: 145616
Article Last Modified on 11/21/2006
// 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: kbctrl kbdocview kbeditctrl kbhowto kbuidesign KB145616