1

In C++ with GDI+ I would like to draw a metafile, but modifying it in such a way that the brushes and pens are replaced by the ones I created in my program. I would like this brush and this pen to be used instead of any others specified in the metafile itself.

How could I achieve this?

I tried to use EnumerateMetafile and PlayRecord, but I don't see how to get control on the pen and brush being used. I tried the code below to select the current Pen and Brush of the underlying HDC, but as I was expecting it does not work...

Any suggestion please? (I would like to avoid rewriting a full handling of all EMF record types)

Thanks in advance.

Tentative code (does not do anything, as expected):

// callback function called by EnumerateMetafile:
BOOL CALLBACK metaCallback(
   EmfPlusRecordType recordType, 
   unsigned int flags, unsigned int dataSize, const unsigned char* pStr, void* metafile)
{ 
   HDC hdc = myGraphics->GetHDC ();
   SelectObject(hdc, GetStockObject(WHITE_PEN));
   myGraphics->ReleaseHDC(hdc);

   static_cast <Metafile*>(metafile)->PlayRecord(recordType, flags, dataSize, pStr);
   return TRUE; 
}
bfredo123
  • 462
  • 1
  • 8
  • 20

1 Answers1

1

Is this an EMF or EMF+? If it's a regular EMF (or WMF), I'd suggest using Enum[Enh]MetaFile instead of GDI+ as it has a better interface for this sort of thing. You'll just need to implement your own handling of SelectObject. If the handle selected is a brush or pen (as determined by GetObjectType), skip that record. Then if you select the pen/brush you want before enumerating the metafile, it should stay there the whole time.

For an EMF+, I would suggest looking for records of type EmfPlusRecordTypeObject and, if they are for a brush or pen object, playing a record that loads the brush or pen you want instead. That's not a complete solution, as some record types embed solid color brushes instead of using a separate object record, but it's a start.

To process an EMF+ you'll need some understanding of the format (http://msdn.microsoft.com/en-us/library/cc230724.aspx). Unfortunately, Microsoft made this difficult by not providing structures for the records and by not providing access to the handle/object table from GDI+, or the HDC it uses to play non-EMF+ records.

Esme Povirk
  • 3,004
  • 16
  • 24