2007年1月17日星期三

Copy Enhance Metafile to Clipboard


// Below is an example to copy a enhance metafile to clipboard by using win32 API
RECT rc;

// Calculate the metafile width and height
// Convert from pixel dimensions to MM dimensions since
// metafile use MM dimensions(in .01-millimeter units)
HDC hDC = ::GetDC(hWnd);
int iWidthMM = GetDeviceCaps(hDC, HORZSIZE);
int iHeightMM = GetDeviceCaps(hDC, VERTSIZE);
int iWidthPels = GetDeviceCaps(hDC, HORZRES);
int iHeightPels = GetDeviceCaps(hDC, VERTRES);
::ReleaseDC(hWnd, hDC);
int iMMPerPelX = (iWidthMM * 100)/iWidthPels;
int iMMPerPelY = (iHeightMM * 100)/iHeightPels;

rc.left = 0 * iMMPerPelX;
rc.top = 0 * iMMPerPelY;
rc.right = 100 * iMMPerPelX;
rc.bottom = 100 * iMMPerPelY;

// Create the metafile and draw a rectangle
HDC hMetaDC = CreateEnhMetaFile((HDC)NULL, NULL, &rc, "ddd");
Rectangle(hMetaDC, 0, 0, 100, 100);
HENHMETAFILE hMetaFile = CloseEnhMetaFile(hMetaDC);

// Open Clipboard
if (OpenClipboard())
{
    // Remove the current Clipboard contents
    if (EmptyClipboard())
    {
        if (!::SetClipboardData(CF_ENHMETAFILE, hMetaFile))
        {
            AfxMessageBox("Unable to set Clipboard data");
        }
    } else {
        AfxMessageBox("Copy Data to clipboard Error: Cannot empty the Clipboard");
    }
    CloseClipboard();
} else {
    AfxMessageBox("Copy Data to clipboard Error: Cannot open the Clipboard");
}
DeleteEnhMetaFile(hMetaFile);

没有评论: