Paste Picture to CRichEditCtrl
-
Sunday, August 05, 2012 8:21 PM
I have a dialog with a CRichEditCtrl to which I would like to paste objects from the Clipboard, including text and pictures. I have no problem pasting text and tables cut from MS Word, but pictures disappear. Using PasteSpecial(UINT nClipFormat, DWORD dvAspect, HMETAFILE hMF) with nClipFormat set to CF_TEXT pastes text okay (dvAspect and hMF left at their defaults, 0), but other formats ((CF_BITMAP, CF_DIB, CF_DSPBITMAP, CF_METAFILEPICT, ...) don't work for me. Nothing happens, even when CanPaste() returns TRUE.
So, what is PasteSpecial() supposed to do, beyond pasting text only? Paste() doesn't work for pictures either, just text.
How does one find out which format is associated with the object in the Clipboard? I can paste a picture from Word into WordPad using Edit >> Paste Special >> Picture(Metafile). Can the same be done through PasteSpecial()? How? I found a suggestion on codeguru (http://www.codeguru.com/cpp/controls/richedit/article.php/c5383/Insert-any-HBITMAP-Bitmap-in-your-RichEdit-Control.htm), but it seems terribly complicated. Is there an easier way?
Peter
All Replies
-
Sunday, August 05, 2012 11:49 PM
>How does one find out which format is associated with the object in the Clipboard?
EnumClipboardFormats
You are supposed to list the available formats and let the user to choose one when the paste special menu is invoked.
The following is signature, not part of post
Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
Visual C++ MVP -
Monday, August 06, 2012 5:01 PM
Thank you, but I cannot get your suggestion to work. I presume that it's possible to insert both text and pictures into a CRichEditCtrl, and stream the data into and out of a file, as suggested by its class members and CF_RTF and CF_RETEXTOBJ Clipboard formats, but I cannot paste a picture from the Clipboard using the suggested procedure: call CanPaste(), if TRUE, then call Paste(). When I copy a graphic from MS Word to the Clipboard, CanPaste() returns TRUE, but Paste() does nothing. Your suggestion to call EnumClipboardFormats() cycles through many formats, including 49304, CF_RTF. Still, calling PasteSpecial(CF_RTF) gives nothing and PasteSpecial() applying all the other formats gives nothing also. PasteSpecial() sets parameter HMETAFILE = 0 at default, so maybe it needs to be set to something else. But where do I find the HMETAFILE? GetClipBoardData() returns a HANDLE, but that doesn't work when cast to HMETAFILE.
I looked into the WordPad code sample packaged with Visual Studio to see how it does the job, but that code is extremely dense.
Peter
-
Monday, August 06, 2012 6:05 PM
You don't need to fill HMETAFILE... use COlePasteSpecialDialog::GetIconicMetafile() to get it.
The following is signature, not part of post
Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
Visual C++ MVP -
Tuesday, August 07, 2012 9:00 PM
Thank you again. Your hints have helped me educate myself, but it still doesn't work. I don't want to ask the user to select a paste format; I want to do it programmatically by selecting the format for the user, when the user clicks a "Paste Special" button that I placed on the dialog. Nonetheless, I proceeded see what would happen if I used COlePasteSpecialDialog. The code follows. Stepping through the code with Debug compilation, I get an exception at dlg.AddStandardFormats(FALSE). The prompt that appears says "A required dialog resource was unavailable."
Any further suggestions?
-------------------------
void CDlgPaper::OnClickedButtonPasteSpecial()
{
// TODO: Add your control notification handler code hereUINT nFormat;
int nIndex;
DVASPECT das;
HGLOBAL hg;
COlePasteSpecialDialog dlg(PSF_SELECTPASTE, NULL, this);dlg.AddStandardFormats(FALSE);
if (dlg.DoModal() == IDOK) {
nIndex = dlg.GetPasteIndex();
nFormat = dlg.m_ps.arrPasteEntries[nIndex].fmtetc.cfFormat;
das = dlg.GetDrawAspect();
hg = dlg.GetIconicMetafile();
m_editPaperText.PasteSpecial(nFormat, das, (HMETAFILE) hg);
}}
--------------------
Peter
-
Wednesday, August 08, 2012 1:33 AMCheck if your resource ids conflict with MFC's
The following is signature, not part of post
Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
Visual C++ MVP -
Friday, August 10, 2012 1:54 PMI checked my resource ids. They don't seem to be in conflict with MFCs. In any case, I am thinking that this issue of pasting pictures into CRichEditCtrl is too complicated to tackle, so I am just going to set it aside for now. Thank you very much.
Peter

