Click here to Skip to main content
15,036,737 members
Articles / Programming Languages / C++
Posted 25 Nov 1999


112 bookmarked

Clipboard Copy in a Nutshell

Rate me:
Please Sign up or sign in to vote.
4.91/5 (15 votes)
25 Nov 1999CPOL1 min read
Clipboard copy in a nutshell.

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;

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

    // Fill in memDC
    memDC.FillSolidRect(rect, dc.GetBkColor()); 

    // Copy contents of memDC to clipboard
    SetClipboardData(CF_BITMAP, bitmap.GetSafeHandle());

    // Clean up

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

    // Place clipboard data in the shared memory file

    if (sf.GetLength() > 0) {
        // Put the data on the clipboard
        SetClipboardData(CF_TEXT, sf.Detach());

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}}


#include <afxpriv.h>

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

    // Place clipboard data in the shared memory file

    if (sf.GetLength() > 0) {
        // Put the data on the clipboard
        SetClipboardData(::RegisterClipboardFormat(CF_RTF), sf.Detach());

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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Keith Rule
Software Developer (Senior)
United States United States
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 It has saved me an incredible amount of time. I only hope my small contributions have given back some of what I've taken.

Comments and Discussions

GeneralClipboard Data Store Pin
Anubhava Dimri5-May-10 21:55
MemberAnubhava Dimri5-May-10 21:55 
Questioncopy tabular data (text and images) to clipboard? Pin
Nachtalb15-May-08 23:13
MemberNachtalb15-May-08 23:13 
Questionplease can you help me to copy file? Pin
yummy_yummy19-Aug-06 0:45
Memberyummy_yummy19-Aug-06 0:45 
GeneralClipboard Query. Pin
HakunaMatada3-Aug-06 1:44
MemberHakunaMatada3-Aug-06 1:44 
GeneralA little additon Pin
AlexSr7-Dec-04 1:13
MemberAlexSr7-Dec-04 1:13 
GeneralThanx, Good Work Pin
Aamir Butt13-Sep-04 0:09
MemberAamir Butt13-Sep-04 0:09 
Generalquestion Pin
Member 332153121-Dec-02 19:19
MemberMember 332153121-Dec-02 19:19 
Generalcatching the CTRL+V Pin
allia18-Dec-02 10:12
Memberallia18-Dec-02 10:12 
Generalconvert clipboard data to bytes Pin
Anonymous31-Jul-02 7:19
MemberAnonymous31-Jul-02 7:19 
GeneralWord file Pin
27-Jan-02 21:42
suss27-Jan-02 21:42 
QuestionHow can I disable copy to clipboard Pin
22-Aug-01 0:17
suss22-Aug-01 0:17 
AnswerRe: How can I disable copy to clipboard Pin
Anonymous22-Apr-04 0:56
MemberAnonymous22-Apr-04 0:56 
GeneralMultiple Pin
Konstantin S. Diguine6-Apr-01 4:02
MemberKonstantin S. Diguine6-Apr-01 4:02 
GeneralCXStitchView Pin
AlexMarbus2-Mar-01 10:11
MemberAlexMarbus2-Mar-01 10:11 
GeneralRe: CXStitchView Pin
5-Mar-01 12:42
suss5-Mar-01 12:42 
GeneralRe: CXStitchView Pin
AlexMarbus5-Mar-01 12:45
MemberAlexMarbus5-Mar-01 12:45 
QuestionHow can I... Pin
1-Dec-00 0:10
suss1-Dec-00 0:10 
AnswerRe: How can I... Pin
14-Dec-00 5:41
suss14-Dec-00 5:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.