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.

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.

Share

About the Author

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.

}
Sometimes result is :
1. There are 4 files in clipboard:
D:\my application.xls
i
j
k (-> incorrect)
2.there are 1 file in clipboard:
D:\my application.xls (->correct)
Any ways? Can you help me? Thanks a lot.

In Office 2003, the inbuilt clipboard viewer shows all the data that has been copied to the clipboard. My question is, Is the Clipboard capable of handling multiple "CTRL+C"s? I mean if I copy some text then I copy another set of text, will the previous one be overwritten or will it remain in the clipboard at a different index( like in an array )?

My situation is such that I want to use the clipboard but not lose the previous data which was there. i.e. After my work with the clipboard is done, it should be set with the previous data which was there, irrespective of the type of data. How do I achieve it?

Thanx a Lot mate. You have helped me save a lot of my time. I wanted to get Client Area on to the clipboard and you have done it fantastically. Thanx Again.

Regards,
Aamir

Imagine that you are creating a fabric of human destiny with the object of making men happy in the end, giving them peace and rest at last, but that it was essential and inevitable to torture to death only one tiny creature..and to found that edifice on its unavenged tears, would you consent to be the architect on those conditions? Tell me, and tell me the truth!

I wrote a little clipboard application and I want that every time
a CTRL+V is pressed to perform some action in my application that will
affect the data in the clipboard, repost it to the clipboard and display
it on the screen.

lets say, that I did CTRL+C on "hello" and now pressed CTRL+V.
my application will convert to upercase "HELLO" and I want it pasted
on screen (of course without the smallercase "hello")

I have talked to people who said maybe catching an interrupt of CTRL+V
and creating one after manipulation - but, can I make sure my CTRL+V
interrupt is done before it is pasted by the OS on the screen ?

i have a process that needs to give its clipboard information (contents, size of contents) to another process on another machine. These 2 processes communicate via sockets. If the clipboard has data in text format, i am able to convert that to characters and send it across to receiving process. However, the problem comes when clipboard data is in CF_BITMAP form. How can i convert this into raw bytes and send it across using sockets?

How can I place the same data in the multiple formats on the clipboard? It would be best of all if someone knows how to do this using pure WinAPI ot at least without "COleDataSource" (without OLE at all). And why Corel(9) does not "understend" CF_ENHMETAFILE???

You're right. I was teaching an MFC class when I wrote up this example for CodeProject. The group project was to create a cross stitch editor. The theory being that this was a program that was feasible for a 400 level group project that had the following properies:

1) None of the students probably knew anything about cross stitching.
2) I could readily explain to them what cross stitching was in a few minutes.
3) All of the student's probably knew someone who did cross stitch.
4) The knowledge of MFC required for this project could be taught in just a few weeks.

The result was, shall we say, interesting. I think it was a learning experence for everyone (including my self). For the most part a cross stitch editor was the right level of project an met my goals. I'm not sure that the students found it terribly interesting though. They seemed to like the screen saver individual project best.

LOL Well, at least your goal was reached.
I will be teaching students in a few months from now, do you have any suggestions for me? (Besides not being nervous ) Or maybe even a link where I can find interesting material on this topic, or even examples? Tnx!

However, this seems to work only successfully if the window (pointed to by pWnd2) is derived from CEdit or CRichEditCtrl (e.g. while using Notepad or a MS-Office document). It does NOT work with marked/selected text for instance in an Internet Explorer or Outlook window. How could I retrieve such text data?

WM_COPY is specific to Edit and Combo controls. You will need to send a WM_COMMAND with the ID of the Copy option. Umm... not that simple. MFC defines ID_EDIT_COPY as 0xE122, so this will work for any MFC application using the defaults, but it is extremely hit-and-miss. If you are targeting only specific applications, then this method could work, but it is not at all generic.

Another alternative could be to simulate the target app's Copy key-stroke. Generally Ctrl+Ins and/or Ctrl+C, but again it can be application specific.