Copy and Paste in MFC

Posted on 1998-07-07
Last Modified: 2013-11-19
I used the clipboard assistant in the projects/contols menu. This generated some code using the open
    clipboard(),Setclipboard() etc functions. I need to copy the entire client space which happens to be
    graph into clipboard. This copies the stuff onto the clipboard but I am not able to paste it elsewhere like
    windows paint or elsewhere. Actually I am not able to access the clipboard data so I am not sure if this
    even copies it to the clipboard. What do I do???
Question by:sram
  • 2
  • 2

Expert Comment

ID: 1318818
what kind of data is your it a bit map or just data in the document?

Author Comment

ID: 1318819
It is data coming from a data aquisition board which is plotted on the client space using OnDraw(), by updating the client space using the OnTimer() function.

Author Comment

ID: 1318820
It is data coming from a data aquisition board which is plotted on the client space using OnDraw(), by updating the client space using the OnTimer() function.

Accepted Solution

bfwizard earned 50 total points
ID: 1318821
well...this is a long answer and if you need more detailed info leave a comment here.  The basic thing to do is make sure you selected the custom clipboard format when you created it with components.  

.first the a little backround.  The clip board acts like a file in which you can serilize to and from.  A good example I will use is say selecting numbers from the screen...this should translate fine to your problem. So in the client area we have the numbers on the screen:
3 4 5
we know where they are exist on the screen and we know what they look like.
in the document our data might be where the number is and its value.  Maybe two lists
llist1={3,4,5} //the values
llist2={{2,3},{4,3},{6,3}}//for the locations

now say I select the number 4 on the screen. and I cut it.
first the function IsSelected is have to decide if it is selcted or not
the next thing that happens is OnEditCut is called (note you wont have to change this at all)  this calls DoCutCopyPaste which does the work.  What you have to do is cut the the numbers 3 and 5 and there locations off the lists in the document.  The numbers 3 and 5 should be placed on some hold lists.  so you have got:

llist1={4} //the values in the document
llist2={{4,3}}//for the locations

Templist1={3,5} //the values in a temp list
Templist2={{2,3},{6,3}}//for the locations in a temp list

this should be done in some function that you call before any other functions are called in
DoCutCopyPaste.  The rest of the code in this function is fine...don't change it...just add a  function that breaks things up into lists before all the other things are called.
what happens in the rest of the code in this function is the clipboard serelizes anything in the document...which in our case in the two llists with just the 3.  The hold list is not touched.  then the function deletecontents is called which wipes all the data out of the the llists become the result is 3 is saved on the clipboard, but wiped from memory.  Note copy calls this function as well and if everthing works for cut it should work for copy as well (you shouldn't have to change a thing) when you copy your function gets called just like in when you cut...they break up the two lists to four list saveing only the 3 in the document and the other number on the hold list...the document gets serilized so the number 3 is saved...again the hold list is not touched.  But deletecontents is not called!!!  so the result is the 3 is serlized on the clip board and not errased from the document...and you still have the other two hold lists.

After both the cut and copy operations...the OnUpdate function is called and must be overriden.  What you should do here is get your data from the document...on a cut there will be none...on a copy there will be the 3.
and merge the holdlists into the lists in the document:

after a cut
llist2={{}} //both empty lists


you need to have the result be


///makes sure your hold lists are empty

now you have all the data minus the 3 in your your ondraw or whatever can do its thing

with copy its the same only the llist will have the 3 in it and the hold list must be merged onto the documents list...


now on a paste operation your function in the DoCutCopyPaste just needs to put everthing
on the hold list...that way when OnUpdate gets called the number 3 gets serilized back...and your document will have the list from off the you just merge the hold lists into the document data....and you've got it.  Another thing to keep in mind is where you paste...maybe where the user last clicked in the window...with that info you just need to change the 3 location.  then ondraw does its thing when all the data is back into the document

You should end up with a bare bones that looks like these functions:

void CCutterView::OnUpdateEditCopyCut(CCmdUI* pCmdUI)
 ...........this you wont have to touch

void CCutterView::OnUpdateEditPaste(CCmdUI* pCmdUI)
.....this you wont have to touch

void CCutterView::OnEditCopy()
..this you wont have to touch

void CCutterView::OnEditCut()
.this you wont have to touch

void CCutterView::OnEditPaste()
.this you wont have to touch

BOOL CCutterView::IsSelected()
..this you will have to add to
somehow you must decide if your graph is selected or not..just return TRUE if it is FALSE otherwise.

void CCutterView::DoCutCopyPaste(CArchive &ar, BOOL bCut)
..add your function here to chop up the needs to be the very first function called

then finally you need to override OnUpdate which gets called after everything.

long answer...


Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ASCII Non-Printable characters/codes and their HTML equivalents 6 113
if loop in java 3 139
fizzArray  challenge 1 81
modThree challenge 4 89
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now