Article ID: 113254
Article Last Modified on 3/7/2005
typedef struct tagMETAFILEPICT
{
int mm;
int xExt;
int yExt;
HMETAFILE hMF;
} METAFILEPICT;
The mm field in the METAFILEPICT structure specifies the mapping mode in which the metafile is to be played. In order for your metafile to be scalable, you should specify MM_ANISOTROPIC as the mapping mode for the mm field. The members xExt and yExt specify the width and height of the rectangle within which the metafile is played.
pt.x = nWidth; pt.y = nHeight; SaveDC(hDC); SetMapMode(hDC, MM_HIMETRIC); DPtoLP(hDC, &pt, 1); RestoreDC(hDC, -1);Because the y-axis is inverted in the MM_HIMETRIC mapping mode, DPtoLP returns negative values for the y coordinate, and therefore you should specify yExt as a positive value. One way to do this is to use the C run- time library function abs to get the absolute value of pt.y.
pt.x = nWidth; pt.y = nHeight; // assumes the hDC is in the appropriate mapping mode LPtoDP(hDC, &pt, 1); SaveDC(hDC); SetMapMode(hDC, MM_HIMETRIC); DPtoLP(hDC, &pt, 1); RestoreDC(hDC, -1);The hMF field in the METAFILEPICT structure should contain the handle to a Windows memory metafile. Because the memory allocated for any object that is placed on the clipboard is owned by the clipboard, this handle should be a copy of the metafile that you created. To fill in the hMF member, call the function CopyMetaFile.
//*************************************************************
//
// CreateClipboardMetaFile()
//
// Purpose: Allocates and fills out a METAFILEPICT struct and
// copies the metafile to the clipboard.
//
//
// Parameters:
//
// HWND hWnd For OpenClipboard().
// HDC hDC For LPtoDP & DPtoLP. The mapping mode
// must match the logical units that
// nWidth and nHeight are specified in.
// int nWidth Width of the metafile in logical units.
// int nHeight Height of the metafile in logical units.
//
//
// Return: (void)
//
//*************************************************************
void CreateClipboardMetaFile(HWND hWnd,
HDC hDC,
HANDLE hMF,
int nWidth,
int nHeight)
{
HANDLE hMem;
LPMETAFILEPICT lpMFP;
POINT pt;
// Allocate memory for the METAFILEPICT struct
hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, sizeof(METAFILEPICT));
lpMFP = (LPMETAFILEPICT)GlobalLock(hMem);
// MM_ANISOTROPIC ensures that it is scalable on playback
lpMFP->mm = MM_ANISOTROPIC;
// Convert the object width and height into MM_HIMETRIC units.
pt.x = nWidth;
pt.y = nHeight;
// First get the width and height into device units.
// This assumes that the hDC is in the mapping mode that
// matches the logical units that nWidth and nHeight are
// specified in. If nWidth and nHeight are already in device
// units, then this step isn't necessary.
LPtoDP(hDC, &pt, 1);
// Temporarily set the mapping mode to MM_HIMETRIC
SaveDC(hDC);
SetMapMode(hDC, MM_HIMETRIC);
DPtoLP(hDC, &pt, 1);
RestoreDC(hDC, -1);
// Because the y-axis is inverted in the MM_HIMETRIC mapping mode,
// DPtoLP returns negative values for the y coordinate.
// Because of this, we should specify yExt as a positive value.
// Also guard against the obscure case that the logical units
// that nWidth was specified in would cause pt.x to be negative.
lpMFP->xExt = abs(pt.x);
lpMFP->yExt = abs(pt.y);
// Fill in the handle to the metafile.
// Make a copy of the metafile because the memory block allocated
// for it will be owned by the clipboard. The memory allocated for the
// METAFILEPICT struct will also be owned by the clipboard.
lpMFP->hMF = CopyMetaFile(hMF, NULL);
GlobalUnlock(hMem);
// Copy the metafile to the clipboard.
OpenClipboard(hWnd);
EmptyClipboard();
SetClipboardData(CF_METAFILEPICT, hMem);
CloseClipboard();
}
Additional query words: 3.10 metafile MM_HIMETRIC METAFILEPICT
Keywords: kbinfo KB113254