Windows 10 Creators update eats metafile graphics in MFC RichTextEdit

488 Views Asked by At

I jumped into an old MFC application which has a problem when run under Windows 10 1703/Creators update. It works fine for XP to Windows 10/1607. After some investigation, it seems that in Windows 10/1703 the app cannot paste metafiles (wmf and emf) from the clipboard into an CRichTextView and save it. The graphics data is not embedded in the rtf file. Here is a stripped down example:

static void testFn(CRichEditView* View)
{
    // Minimal Example

    // Init MetaFileDC
    CMetaFileDC MetaFileDC;
    CClientDC DC(NULL);
    MetaFileDC.CreateEnhanced(NULL, NULL, NULL, NULL);
    CRect Recht(0, 0, 400, 300);
    MetaFileDC.SetAttribDC(DC.m_hDC);
    MetaFileDC.SetWindowOrg(0, 0);
    MetaFileDC.SetWindowExt(Recht.Size());

    // draw : "ABC" and a line
    MetaFileDC.TextOutA(0, 0, "ABC");
    MetaFileDC.MoveTo(0, 0);
    MetaFileDC.LineTo(Recht.right, Recht.bottom);

    // to clipboard
    View->OpenClipboard();
    EmptyClipboard();
    SetClipboardData(CF_ENHMETAFILE, MetaFileDC.CloseEnhanced());
    CloseClipboard();

    // paste from clipboard
    View->GetRichEditCtrl().Paste();

    // save rtf file
    View->GetDocument()->OnSaveDocument("abc.rtf");
}

This example pastes a enhanced metafile mit "ABC" and a line into the CRichTextView and saves the document as "abc.rtf"

  • From Windows XP to Windows 10/1607 this works fine
  • with the latest Creators update the file is smaller and the data is not saved

It is probably related to RichEditBox: picture and content after the picture disappear (Windows 10 1703 Creators Update)

Any ideas? Is there a way to get the metafile graphics in the document without the clipboard? Bitmaps still work.

1

There are 1 best solutions below

0
On

We had the same problem in our software after creators update. We also get our graphic via the clipboard. After some research on Google and SO I came up with this:

HENHMETAFILE hMetafile = nullptr;

if(OpenClipboard(AfxGetMainWnd()->m_hWnd))
{
    if(EnumClipboardFormats(0) == CF_ENHMETAFILE)
        hMetafile = (HENHMETAFILE) GetClipboardData(CF_ENHMETAFILE);

    CloseClipboard();
}

Gdiplus::MetafileHeader header;
Gdiplus::Metafile::GetMetafileHeader(hMetafile,&header);

HDC hdc = AfxGetMainWnd()->GetDC()->GetSafeHdc();
UINT bufsize = GetWinMetaFileBits(hMetafile,0,0,MM_ANISOTROPIC,hdc);
BYTE* buffer = new BYTE[bufsize];
GetWinMetaFileBits(hMetafile,bufsize,buffer,MM_ANISOTROPIC,hdc);

std::stringstream ss;
ss << "{\\rtf1{\\pict\\wmetafile8";
ss << "\\picw" << (UINT)((header.Width / header.DpiX) * 2540) << "\\pich" << (UINT)((header.Height / header.DpiY) * 2540);
ss << "\\picwgoal" << (UINT)((header.Width / header.DpiX) * 1440) << "\\pichgoal" << (UINT)((header.Height / header.DpiY) * 1440);
ss << " " << std::endl;
ss << std::hex << std::setfill('0');

for(UINT i = 0;i < bufsize;++i)
    ss << std::setw(2) << static_cast<UINT>(buffer[i]);

delete[] buffer;
ss << "}}" << std::endl;
return ss.str().c_str();

We use this now to insert graphics into our document. I have not tried it as a standalone document.