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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sum67 challenge 35 116
unix example issues 18 106
Problem to open Excel file 15 227
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

696 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