Click to See Complete Forum and Search --> : Implementing Copy/Paste in a CWnd derived class
April 17th, 1999, 11:52 PM
Hello everyone,
I've derived my own class from CWnd and using it to display coloured text using standard procedures (TextOut and so on). Now, I'd like to implement some copy/paste (mainly copy, in fact) capability, but I really don't know how to do that ... Could anybody give me a clue, so I can investigate further myself ?
Thanks,
Raphael Fleury
Roger Osborn
April 18th, 1999, 10:26 AM
Here's an example of how to put simple text on the clipboard that might help:
CString sCopyData=_T("Hello World\nAlright!");
HANDLE hMem=GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,(sCopyData.GetLength()+1)*sizeof(TCHAR));
if(hMem!=NULL)
{
TCHAR *pTxt=static_cast<TCHAR*>(GlobalLock(hMem));
_tcscpy(pTxt,sCopyData);
GlobalUnlock(hMem);
// we should now have a string ready to give to the clipboard
BOOL bWorked=FALSE;
if (OpenClipboard() )
{
// Remove the current Clipboard contents
if( ::EmptyClipboard() )
{
if ( ::SetClipboardData( CF_TEXT, hMem ) != NULL )
{
bWorked=TRUE;
}
else
{
AfxMessageBox( "Unable to set Clipboard data" );
}
}
else AfxMessageBox( "Cannot empty the Clipboard" );
CloseClipboard();
}
else AfxMessageBox( "Cannot open the Clipboard" );
// if went wrong then the memory should be freed
if(!bWorked)::GlobalFree(hMem);
}
else AfxMessageBox( "Insufficient virtual memory" );
// check didn't damage memory
ASSERT(AfxCheckMemory());
In real life I tend to use MessageBoxEx instead of AfxMessageBox (cos it can be stopped from disappearing behind other windows), but you get the idea.
Cheers,
Roger
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.