Click here to Skip to main content
13,090,299 members (75,407 online)
Click here to Skip to main content
Add your own
alternative version


145 bookmarked
Posted 22 Nov 1999

All you ever wanted to know about the Clipboard...

, 22 Nov 1999
Rate this:
Please Sign up or sign in to vote.
<!-- STEP 2. Include download and sample image information -->
  • Download clipboard demo project - 12 Kb
  • Download notification demo project - 12 Kb
  • Adding clipboard support to a VC++ / MFC application is extremely simple. This article covers the basic steps involved in getting your applictions talking to the clipboard. In it are examples of the following:

    Reading and writing text

    The following source code demonstrates how to place text (contained in the CString "source") onto the clipboard.

    CString source; 
    //put your text in source
    	HGLOBAL clipbuffer;
    	char * buffer;
    	clipbuffer = GlobalAlloc(GMEM_DDESHARE, source.GetLength()+1);
    	buffer = (char*)GlobalLock(clipbuffer);
    	strcpy(buffer, LPCSTR(source));

    The source code below demonstrates the converse, how to retrieve text from the clipboard.

    char * buffer = NULL;
    //open the clipboard
    CString fromClipboard;
    if ( OpenClipboard() ) 
    	HANDLE hData = GetClipboardData( CF_TEXT );
    	char * buffer = (char*)GlobalLock( hData );
    	fromClipboard = buffer;
    	GlobalUnlock( hData );

    Reading and writing WMF (enhanced) data

    Writing and reading images to and from the clipboard can be very useful, and it is really very easy! The following example writes an enhanced metafile to the clipboard.

    if ( OpenClipboard() )
    	//create the metafile DC
    	CMetaFileDC * cDC = new CMetaFileDC();
    	//call draw routine here that makes GDI calls int cDC
    	//close meta CMetafileDC and get its handle
    	HENHMETAFILE handle = cDC->CloseEnhanced();
    	//place it on the clipboard
    	//delete the dc
    	delete cDC;

    Here is the converse. We get the metafile from the clipboard and draw it into our own client DC (just as a test, really you would want to make a copy).

    if ( OpenClipboard() )
    	//Get the clipboard data
    	//play it into a DC (our own DC in this example)
    	CClientDC dc(this);
    	CRect client(0,0,200,200);
    	//close the clipboard

    Reading and writing a bitmap

    Reading and writing a bitmap is only marginally trickier. The basic idea remains the same. Here is an example of saving a bitmap to the clipboard.

    if ( OpenClipboard() )
    	//create some data
    	CBitmap * junk = new CBitmap();
    	CClientDC cdc(this);
    	CDC dc;
    	CRect client(0,0,200,200);
    	//call draw routine here that makes GDI calls
    	//put the data on the clipboard
    	//copy has been made on clipboard so we can delete
    	delete junk;

    As with the other examples, here is an example of getting a bitmap from the clipboard. In this simple example we will just Blt it to the cleint DC.

    if ( OpenClipboard() )
    	//Get the clipboard data
    	HBITMAP handle = (HBITMAP)GetClipboardData(CF_BITMAP);
    	CBitmap * bm = CBitmap::FromHandle(handle);
    	CClientDC cdc(this);
    	CDC dc;

    Setting up and using your own custom format

    By using the RegisterClipboardFormat() API you can copy and paste any type of data you want. This can be very useful in moving data between your own applications. Let's say we have a structure:

    struct MyFormatData
    	long val1;
    	int val2;

    that we want to move on the clipboard. We can copy as follows:

    UINT format = RegisterClipboardFormat("MY_CUSTOM_FORMAT");
    	//make some dummy data
    	MyFormatData data;
    	data.val1 = 100;
    	data.val2 = 200;
    	//allocate some global memory
    	HGLOBAL clipbuffer;
    	clipbuffer = GlobalAlloc(GMEM_DDESHARE, sizeof(MyFormatData));
    	MyFormatData * buffer = (MyFormatData*)GlobalLock(clipbuffer);
    	//put the data into that memory
    	*buffer = data;
    	//Put it on the clipboard

    To read it back off we do the inverse:

    //second call with just get format already registered
    UINT format = RegisterClipboardFormat("MY_CUSTOM_FORMAT");
    MyFormatData data;
    if ( OpenClipboard() ) 
    	//get the buffer
    	HANDLE hData = GetClipboardData(format);
    	MyFormatData * buffer = (MyFormatData *)GlobalLock( hData );
    	//make a local copy
    	data = *buffer;
    	GlobalUnlock( hData );

    Getting notified of clipboard changes

    It is very useful to be notified (via a windows message) whenever the clipboard has changed. To do this you use SetClipboardViewer() and then catch WM_DRAWCLIPBOARD

    In your initialization code call:
    	SetClipboardViewer();  //add us to clipboard change notification chain
    In your message map add:
    	ON_MESSAGE(WM_DRAWCLIPBOARD, OnClipChange)  //clipboard change notification
    Which is declared as:
    	afx_msg void OnClipChange();  //clipboard change notification
    Finally implement:
    void CDetectClipboardChangeDlg::OnClipChange() 
    	//do something here, for example
    	CTime time = CTime::GetCurrentTime();
    	SetDlgItemText(IDC_CHANGED_DATE,time.Format("%a, %b %d, %Y -- %H:%M:%S"));

    Pasting data to another app's window

    One thing that I have found useful is to copy text to the clipboard (see above) and then to "paste" it to ANOTHER application!. I wrote a nice localization app that used a third party language translation package using this technique.

    Simply get the handle to the target window and send a "PASTE" to it.

    SendMessage(m_hTextWnd, WM_PASTE, 0, 0);


    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

    Randy More
    United States United States
    No Biography provided

    You may also be interested in...


    Comments and Discussions

    QuestionPasting data of clipboard to Other Window by WINDOW TITLE and doing String manupulation like <=,== Text Pin
    AdminSam18-Jan-12 20:30
    memberAdminSam18-Jan-12 20:30 
    GeneralMy vote of 4 Pin
    AdminSam18-Jan-12 20:21
    memberAdminSam18-Jan-12 20:21 
    GeneralMy vote of 2 Pin
    gordon885-Dec-11 17:36
    membergordon885-Dec-11 17:36 
    GeneralNEVER do this!!! Pin
    John Crenshaw12-Jun-09 13:05
    memberJohn Crenshaw12-Jun-09 13:05 
    Questioncompile error _UNICODE Pin
    winchnet200516-Nov-08 14:33
    memberwinchnet200516-Nov-08 14:33 
    AnswerRe: compile error _UNICODE Pin
    VEMS28-Jan-09 4:26
    memberVEMS28-Jan-09 4:26 
    AnswerRe: compile error _UNICODE Pin
    AdminSam18-Jan-12 20:24
    memberAdminSam18-Jan-12 20:24 
    QuestionHow can manage all of copy and paste in system in C++ -&gt;please help me Pin
    crackgns13-Aug-08 2:08
    membercrackgns13-Aug-08 2:08 
    AnswerRe: How can manage all of copy and paste in system in C++ -&gt;please help me Pin
    r1co17-Jan-09 19:21
    memberr1co17-Jan-09 19:21 
    QuestionHow to paste to a WORD app's window? Pin
    test_15509-Sep-07 2:34
    membertest_15509-Sep-07 2:34 
    QuestionHow to get the XML data present in the Clipboard??? Pin
    gloriousgopi15-Feb-07 3:17
    membergloriousgopi15-Feb-07 3:17 
    Questionhow to capture a big window? Pin
    Rockone8-Jan-07 2:42
    memberRockone8-Jan-07 2:42 
    GeneralClipboard Query. Pin
    Hakuna-Matada3-Aug-06 1:40
    memberHakuna-Matada3-Aug-06 1:40 
    GeneralRe: Clipboard Query. Pin
    Ashutosh Bhawasinka12-Dec-06 8:40
    memberAshutosh Bhawasinka12-Dec-06 8:40 
    GeneralRe: Clipboard Query. Pin
    Naadia27-Sep-07 20:14
    memberNaadia27-Sep-07 20:14 
    Generalcrash with release edition Pin
    chinabcb16330-Jul-06 0:40
    memberchinabcb16330-Jul-06 0:40 
    GeneralRe: crash with release edition Pin
    xox_c0bra_xox7-Sep-09 13:31
    memberxox_c0bra_xox7-Sep-09 13:31 
    GeneralMemory Leak [modified] Pin
    Little.Ben26-Jul-06 1:31
    memberLittle.Ben26-Jul-06 1:31 
    QuestionRe: Memory Leak Pin
    PistolPeet9-May-07 7:01
    memberPistolPeet9-May-07 7:01 
    AnswerRe: Memory Leak (it's not a leak) Pin
    dagronf27-Jun-07 17:11
    memberdagronf27-Jun-07 17:11 
    GeneralRe: Memory Leak (it's not a leak) Pin
    PistolPeet27-Jun-07 23:19
    memberPistolPeet27-Jun-07 23:19 
    QuestionHow OnPaintClipboard() do ? Pin
    trauconnguyen16-Apr-05 15:43
    membertrauconnguyen16-Apr-05 15:43 
    GeneralGetting a bitmap Pin
    tunerica22-Jul-04 18:45
    membertunerica22-Jul-04 18:45 
    GeneralPlease REJECT this article Pin
    Anonymous22-May-03 9:48
    sussAnonymous22-May-03 9:48 
    GeneralRe: Please REJECT this article Pin
    mathboy26-Jan-04 10:26
    membermathboy26-Jan-04 10:26 
    GeneralRe: Please REJECT this article Pin
    henky_arektc9711-May-06 19:00
    memberhenky_arektc9711-May-06 19:00 
    GeneralRe: Please REJECT this article Pin
    xox_c0bra_xox7-Sep-09 13:16
    memberxox_c0bra_xox7-Sep-09 13:16 
    GeneralRe: Please REJECT this article Pin
    JPhelps13-Aug-16 11:58
    memberJPhelps13-Aug-16 11:58 
    GeneralRe: Please REJECT this article Pin
    xox_c0bra_xox7-Sep-09 13:24
    memberxox_c0bra_xox7-Sep-09 13:24 
    GeneralI want to Understand METAFILEPICT format Pin
    anirudhas1-Mar-03 20:57
    memberanirudhas1-Mar-03 20:57 
    QuestionWhy you doesn't use standard technique? Pin
    Alexander Kourov19-Nov-02 17:01
    memberAlexander Kourov19-Nov-02 17:01 
    Generalget clipboard data as bytes Pin
    Anonymous31-Jul-02 7:31
    sussAnonymous31-Jul-02 7:31 
    GeneralI don't know what is this error Pin
    Angu17-Dec-01 11:49
    memberAngu17-Dec-01 11:49 
    GeneralRe: I don't know what is this error Pin
    xox_c0bra_xox7-Sep-09 13:10
    memberxox_c0bra_xox7-Sep-09 13:10 
    QuestionFile Transfer? Pin
    Anonymous19-Sep-01 8:17
    memberAnonymous19-Sep-01 8:17 
    QuestionWhy does SetClipboardViewer() crash my application? Pin
    Anonymous4-Jun-01 22:54
    memberAnonymous4-Jun-01 22:54 
    GeneralPlease give me more Information of MetaFile Please. Pin
    Monchai22-Apr-01 21:46
    memberMonchai22-Apr-01 21:46 
    QuestionHow do I compile this? Pin
    sleepy24-Feb-01 4:55
    membersleepy24-Feb-01 4:55 
    GeneralClipboard arrays Pin
    alan29-Dec-00 4:56
    memberalan29-Dec-00 4:56 
    GeneralRe: Clipboard arrays Pin
    Anonymous20-Feb-01 16:50
    memberAnonymous20-Feb-01 16:50 
    GeneralGetting notified of clipboard changes is wong and dangerous Pin
    JW8-Jun-00 3:58
    sussJW8-Jun-00 3:58 
    GeneralRe: Getting notified of clipboard changes is wong and dangerous Pin
    Tom Archer6-Jul-01 1:00
    memberTom Archer6-Jul-01 1:00 
    GeneralRe: Getting notified of clipboard changes is wong and dangerous Pin
    Anonymous10-Apr-03 11:58
    sussAnonymous10-Apr-03 11:58 
    Questionole to msaccess? Pin
    Thomas Gruber25-May-00 5:41
    sussThomas Gruber25-May-00 5:41 
    GeneralEverything about storing(loading) Ole Objects Pin
    Mihai Filimon9-Mar-00 2:37
    sussMihai Filimon9-Mar-00 2:37 
    GeneralA couple comments Pin
    William Kempf27-Jan-00 4:19
    sussWilliam Kempf27-Jan-00 4:19 
    GeneralRe: A couple comments Pin
    george9-Feb-00 23:44
    sussgeorge9-Feb-00 23:44 

    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.

    Permalink | Advertise | Privacy | Terms of Use | Mobile
    Web03 | 2.8.170813.1 | Last Updated 23 Nov 1999
    Article Copyright 1999 by Randy More
    Everything else Copyright © CodeProject, 1999-2017
    Layout: fixed | fluid