My windows c++ program creates EMFs (Enhanced Metafile Format) to export to clipboard, word and excel.
The following example code generates an EMF rectangle (width=height=25) that is only 12x12 while the canvas is 25x25 (note: screen resolution of my laptop is3600x1800). At other screen resolutions, similar scaling anomalies occur (too big/too small). It would appear the scaling of the graphics-drawing needs to be set as a function of the resolution. I clearly have a gap in my knowledge here...any help is appreciated.
HDC ref_dc = GetDC(NULL);
Rect r(0, 0, 25, 25);
Metafile* emf = new Metafile(ref_dc, r, MetafileFrameUnitPixel, EmfTypeEmfPlusDual, L"Drawing");//to HDC
Graphics* g = new Graphics(emf);
//draw a simple box
Gdiplus::Pen* pen = new Pen(Color(0, 255, 0), 1.0f);
pen->SetDashStyle(DashStyleSolid);
pen->SetLineCap(LineCapRound, LineCapRound, DashCapFlat);
g->DrawRectangle(pen, r); // DrawMyObject(g);
// code here to put on clipboard
Although unrelated to your actual problem, I feel very compelled to point out that your programming style of creating all the objects on the heap is quite odd. There is no reason to use
newlike this. Just create temporary objects on the stack. This keeps you from leaking memory and other resources like a sieve. By way of illustration, all you need is this:As for your actual question, metafiles don't have a fixed size. They basically just encapsulate a series of GDI drawing instructions that can be recapitulated at will.
The normal way to put an enhanced metafile on the clipboard would be to call
SetClipboardDatafunction, using theCF_ENHMETAFILEformat. The handle type would obviously then beHENHMETAFILE. You'll need to get GDI+ to give you one of those, probably by using theMetafile::GetHENHMETAFILEmethod after you've finished loading/creating your metafile object.The scaling/sizing is the responsibility of the client code, the one that receives your metafile from the clipboard and tries to display it. The metafile header contains an entry that specifies its horizontal and vertical resolution. That can then be scaled in terms of the display DPI. In GDI+, something like: