Learn how to a build a cloud-first strategyRegister Now


Copy and Paste in MFC

Posted on 1998-07-07
Medium Priority
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 graph....is 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 200 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 called....you 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 document...so the llists become empty....so 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)....so 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 document...so 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 clipboard...now 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 list...it needs to be the very first function called

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

long answer...


Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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…
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.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month20 days, 20 hours left to enroll

810 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