Click here to Skip to main content
5,790,650 members and growing! (17,570 online)
Email Password   helpLost your password?
Desktop Development » Clipboard » General     Beginner

Clipboard Copy in a Nutshell

By Keith Rule

Clipboard copy in a nutshell.
VC6, C++, Windows, Visual Studio, Dev

Posted: 25 Nov 1999
Updated: 25 Nov 1999
Views: 127,123
Bookmarked: 69 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
34 votes for this Article.
Popularity: 7.16 Rating: 4.67 out of 5
1 vote, 8.3%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
1 vote, 8.3%
4
10 votes, 83.3%
5

Supporting copy is usually straightforward for most applications. It usually requires 10-20 lines of code. But like many features of MFC, the implementation is simple. However, finding the information needed to create the implementation is tedious and time consuming. To save you many hours of reading documentation, I have included several common methods for placing data on the clipboard.

Copying a Bitmap of the Client Area

The following bit of code renders the client area using OnDraw() into a bitmap. The bitmap is placed on the clipboard as a CF_BITMAP that is recognizable by most applications that accept bitmaps. This code will work will all mapping modes provided that mapping mode is set in the OnPrepareDC() function.

void CXStitchView::OnEditCopy() 
{
    CRect       rect;
    CClientDC   dc(this);
    CDC         memDC;
    CBitmap     bitmap;
    
    GetClientRect(&rect); 

    // Create memDC

    memDC.CreateCompatibleDC(&dc);
    bitmap.CreateCompatibleBitmap(&dc, rect.Width(), rect.Height());
    CBitmap* pOldBitmap = memDC.SelectObject(&bitmap);

    // Fill in memDC

    memDC.FillSolidRect(rect, dc.GetBkColor()); 
    OnPrepareDC(&memDC);
    OnDraw(&memDC);

    // Copy contents of memDC to clipboard

    OpenClipboard();
    EmptyClipboard();
    SetClipboardData(CF_BITMAP, bitmap.GetSafeHandle());
    CloseClipboard();

    // Clean up

    memDC.SelectObject(pOldBitmap);
    bitmap.Detach();
}

Copying a Table of Data to the Clipboard

Placing a table of data on the clipboard is easy. It is simply a string of text. Tabs separate columns, new lines separate rows. Here's some example source.

#include <afxpriv.h>


void CClipExamView::OnEditCopy() 
{
    // Create a shared memory file

    CSharedFile sf(GMEM_MOVEABLE | GMEM_SHARE | GMEM_ZEROINIT);

    // Place clipboard data in the shared memory file

    RenderTableData(sf);

    if (sf.GetLength() > 0) {
        // Put the data on the clipboard

        OpenClipboard();
        EmptyClipboard();
        SetClipboardData(CF_TEXT, sf.Detach());
        CloseClipboard();
    }
}

void CClipExamView::RenderTableData(CFile &file)
{
    // Columns are separated by tabs ('\t')

    // Rows are separated by new lines ('\n')

    CString buffer = "1\t2\t3\n4\t5\t6\n";
    file.Write(buffer, buffer.GetLength()); 
}

Copying Formatted Data to the Clipboard

Formatted text can support bold, italic or any other formatting that can be done in a word processor. Formatted text is usually placed on the clipboard as RTF (Rich Text Format).

The Rich Text Format is intended as an interchange format for Word-processing applications. Because of that, it is a rather large and feature rich file format. Fortunately, it is possible to describe a minimal RTF command set for creating simple formatted documents.

Basic RTF commands:

\par - Starts a new paragraph. 
\tab - A tab. 
\b - Enable Bold (scoped within a group) 
\i - Enable Italics (scoped within a group) 
\ul - Enable Underline (scoped within a group)

For example, the RTF string:

{\rtf1 {1 \tab 2 \tab 3 \par 4 \tab {\b\i 5} \tab 6}}

Source

#include <afxpriv.h>


void CClipExamView::OnEditCopy() 
{
    // Create a shared memory file

    CSharedFile sf(GMEM_MOVEABLE | GMEM_SHARE | GMEM_ZEROINIT);

    // Place clipboard data in the shared memory file

    RenderFormattedData(sf);

    if (sf.GetLength() > 0) {
        // Put the data on the clipboard

        OpenClipboard();
        EmptyClipboard();
        SetClipboardData(::RegisterClipboardFormat(CF_RTF), sf.Detach());
        CloseClipboard();
    }
}

void CClipExamView::RenderFormattedData(CFile &file)
{
    // RTF (Rich Text Format) - Don't forget to escape

    // the \ character in your C strings!

    CString buffer = "{\\rtf1 {1 \\tab 2 \\tab 3" 
               " \\par 4 \\tab {\\b\\i 5} \\tab 6}}";
    file.Write(buffer, buffer.GetLength()); 
}

Getting More Info on RTF

Most books on file format address RTF in some form. You can find the RTF specification here.

Virtually all word processors can save RTF files. This is usually a good place to start when building an RTF template.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Keith Rule


I work at Tektronix in Beaverton OR. I've been programming for fun since 1975 (I started while in a Computer Explorer Scout group in Spokane WA). I've been programming in C since 1979 and I've been working professionally since 1983.

I really enjoy www.codeproject.com. It has saved me an incredible amount of time. I only hope my small contributions have given back some of what I've taken.

Occupation: Web Developer
Location: United States United States

Other popular Clipboard articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 17 of 17 (Total in Forum: 17) (Refresh)FirstPrevNext
Generalcopy tabular data (text and images) to clipboard?memberNachtalb0:13 16 May '08  
Generalplease can you help me to copy file?memberyummy_yummy1:45 19 Aug '06  
GeneralClipboard Query.memberHakuna-Matada2:44 3 Aug '06  
GeneralA little additonmemberAlexSr2:13 7 Dec '04  
GeneralThanx, Good WorkmemberBugSearcher1:09 13 Sep '04  
GeneralquestionsussAnonymous20:19 21 Dec '02  
Generalcatching the CTRL+VsussM.A.11:12 18 Dec '02  
Generalconvert clipboard data to bytessussAnonymous8:19 31 Jul '02  
GeneralWord filememberLai The Thoi22:42 27 Jan '02  
GeneralHow can I disable copy to clipboardmemberAnonymous1:17 22 Aug '01  
GeneralRe: How can I disable copy to clipboardsussAnonymous1:56 22 Apr '04  
GeneralMultiplememberKonstantin S. Diguine5:02 6 Apr '01  
GeneralCXStitchViewmemberAlexMarbus11:11 2 Mar '01  
GeneralRe: CXStitchViewmemberKeith13:42 5 Mar '01  
GeneralRe: CXStitchViewmemberAlexMarbus13:45 5 Mar '01  
GeneralHow can I...memberBernd Giesen1:10 1 Dec '00  
GeneralRe: How can I...memberCraig Henderson6:41 14 Dec '00  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 25 Nov 1999
Editor: Smitha Vijayan
Copyright 1999 by Keith Rule
Everything else Copyright © CodeProject, 1999-2009
Web16 | Advertise on the Code Project