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

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

By Randy More

VC6, C++Windows, NT4VS6, Visual Studio, Dev

Posted: 22 Nov 1999
Updated: 22 Nov 1999
Views: 193,687
Bookmarked: 77 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
69 votes for this Article.
Popularity: 7.64 Rating: 4.15 out of 5
3 votes, 9.7%
1
2 votes, 6.5%
2
5 votes, 16.1%
3
5 votes, 16.1%
4
16 votes, 51.6%
5
  • 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
    
    if(OpenClipboard())
    {
    	HGLOBAL clipbuffer;
    	char * buffer;
    	EmptyClipboard();
    	clipbuffer = GlobalAlloc(GMEM_DDESHARE, source.GetLength()+1);
    	buffer = (char*)GlobalLock(clipbuffer);
    	strcpy(buffer, LPCSTR(source));
    	GlobalUnlock(clipbuffer);
    	SetClipboardData(CF_TEXT,clipbuffer);
    	CloseClipboard();
    }
    

    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 );
    	CloseClipboard();
    }
    

    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() )
    {
    	EmptyClipboard();
    
    	//create the metafile DC
    
    	CMetaFileDC * cDC = new CMetaFileDC();
    	cDC->CreateEnhanced(GetDC(),NULL,NULL,"the_name");
    
    	//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
    
    	SetClipboardData(CF_ENHMETAFILE,handle);
    	CloseClipboard();
    
    	//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
    
    	HENHMETAFILE handle = (HENHMETAFILE)GetClipboardData(CF_ENHMETAFILE);
    
    	//play it into a DC (our own DC in this example)
    
    	CClientDC dc(this);
    	CRect client(0,0,200,200);
    	dc.PlayMetaFile(handle,client);		
    
    	//close the clipboard
    
    	CloseClipboard();
    }
    

    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() )
    {
    	EmptyClipboard();
    	//create some data
    
    	CBitmap * junk = new CBitmap();
    	CClientDC cdc(this);
    	CDC dc;
    	dc.CreateCompatibleDC(&cdc);
    	CRect client(0,0,200,200);
    	junk->CreateCompatibleBitmap(&cdc,client.Width(),client.Height());
    	dc.SelectObject(junk);
    
    	//call draw routine here that makes GDI calls
    
    	DrawImage(&dc,CString("Bitmap"));
    
    	//put the data on the clipboard
    
    	SetClipboardData(CF_BITMAP,junk->m_hObject);
    	CloseClipboard();
    
    	//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;
    	dc.CreateCompatibleDC(&cdc);
    	dc.SelectObject(bm);
    	cdc.BitBlt(0,0,200,200,&dc,0,0,SRCCOPY);
    
    	CloseClipboard();
    
    }
    

    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");
    if(OpenClipboard())
    {
    	//make some dummy data
    
    	MyFormatData data;
    	data.val1 = 100;
    	data.val2 = 200;
    
    	//allocate some global memory
    
    	HGLOBAL clipbuffer;
    	EmptyClipboard();
    	clipbuffer = GlobalAlloc(GMEM_DDESHARE, sizeof(MyFormatData));
    	MyFormatData * buffer = (MyFormatData*)GlobalLock(clipbuffer);
    
    	//put the data into that memory
    
    	*buffer = data;
    
    	//Put it on the clipboard
    
    	GlobalUnlock(clipbuffer);
    	SetClipboardData(format,clipbuffer);
    	CloseClipboard();
    }
    

    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 );
    	CloseClipboard();
    }
    


    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"));
    
    	DisplayClipboardText();
    }
    

    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);
    

    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

    Randy More



    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 25 of 35 (Total in Forum: 35) (Refresh)FirstPrevNext
    Questioncompile error _UNICODEmemberwinchnet200515:33 16 Nov '08  
    QuestionHow can manage all of copy and paste in system in C++ ->please help memembercrackgns3:08 13 Aug '08  
    QuestionHow to paste to a WORD app's window?membertest_15503:34 9 Sep '07  
    QuestionHow to get the XML data present in the Clipboard???membergloriousgopi4:17 15 Feb '07  
    Generalhow to capture a big window?memberRockone3:42 8 Jan '07  
    GeneralClipboard Query.memberHakuna-Matada2:40 3 Aug '06  
    GeneralRe: Clipboard Query.memberAshutosh Bhawasinka9:40 12 Dec '06  
    GeneralRe: Clipboard Query.memberNaadia21:14 27 Sep '07  
    Generalcrash with release editionmemberchinabcb1631:40 30 Jul '06  
    GeneralMemory Leak [modified]memberLittle.Ben2:31 26 Jul '06  
    QuestionRe: Memory LeakmemberPistolPeet8:01 9 May '07  
    AnswerRe: Memory Leak (it's not a leak)memberdagronf18:11 27 Jun '07  
    GeneralRe: Memory Leak (it's not a leak)memberPistolPeet0:19 28 Jun '07  
    GeneralHow OnPaintClipboard() do ?membertrauconnguyen16:43 16 Apr '05  
    GeneralGetting a bitmapmembertunerica19:45 22 Jul '04  
    GeneralPlease REJECT this articlesussAnonymous10:48 22 May '03  
    GeneralRe: Please REJECT this articlemembermathboy11:26 26 Jan '04  
    GeneralRe: Please REJECT this articlememberhenky_arektc9720:00 11 May '06  
    GeneralI want to Understand METAFILEPICT formatmemberanirudhas21:57 1 Mar '03  
    GeneralWhy you doesn't use standard technique?memberAlexander Kourov18:01 19 Nov '02  
    Generalget clipboard data as bytessussAnonymous8:31 31 Jul '02  
    GeneralI don't know what is this errormemberAngu12:49 17 Dec '01  
    GeneralFile Transfer?memberAnonymous9:17 19 Sep '01  
    GeneralWhy does SetClipboardViewer() crash my application?memberAnonymous23:54 4 Jun '01  
    GeneralPlease give me more Information of MetaFile Please.memberMonchai22:46 22 Apr '01  

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

    PermaLink | Privacy | Terms of Use
    Last Updated: 22 Nov 1999
    Editor: Chris Maunder
    Copyright 1999 by Randy More
    Everything else Copyright © CodeProject, 1999-2009
    Web11 | Advertise on the Code Project