CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Guest

    Implementing Copy/Paste in a CWnd derived class

    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




  2. #2
    Join Date
    Apr 1999
    Posts
    48

    Re: Implementing Copy/Paste in a CWnd derived class

    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




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured