我是用matlab程序画图并将图形发送到剪贴板上,然后在MFC对话框中加一个“保存”按钮,用下面的消息响应函数将图形从剪贴板读取并保存为bmp文件。现在的问题是程序运行后第一次点“保存”时是可以的,但当我第二次再“保存”就报错,分步调试发现是程序中的SetViewportExt和SetViewportOrg出错,在wingdi.cpp文件中查看两个函数的代码:VERIFY(::SetViewportExtEx(m_hAttribDC, x, y, &size)),错误的原因好像是size的值超出了范围,但是两个函数的参数值x,y的值是正确的,所以怀疑可能是DoPasteFigure(CDC* pDC, CRect& metafile)中pDC的问题,我却不知道该怎么该,那位高人指点一下啊,万分感激!
CRect controlBounds;
GetClientRect(&controlBounds);
int bmpWidth = controlBounds.right - controlBounds.left;
int bmpHeight = controlBounds.bottom - controlBounds.top; // Create the bitmap with the same dimensions of the control.
CImage bmp;
bmp.Create(bmpWidth, bmpHeight, 24); // Play the  metafile from the clipboard to the bitmap.
HDC bmpDC = bmp.GetDC();
CDC pDC;
pDC.Attach(bmpDC);  DoPasteFigure(&pDC, controlBounds); bmp.Save(_T(filename));
bmp.ReleaseDC(); void CCopiedFigure::DoPasteFigure(CDC* pDC, CRect& metafile)
        {
GLOBALHANDLE    hGMem;
LPMETAFILEPICT   lpMFP;
OpenClipboard();
hGMem = GetClipboardData(CF_METAFILEPICT);
lpMFP = (LPMETAFILEPICT)GlobalLock(hGMem);
pDC->SaveDC();
pDC->SetMapMode(lpMFP->mm);   //设置显示模式
pDC->SetViewportExt(metafile.Width(), metafile.Height());
pDC->SetViewportOrg(metafile.left, metafile.top);   //设置显示位置
pDC->PlayMetaFile(lpMFP->hMF);     //放置图像
VERIFY(pDC->RestoreDC(-1));
GlobalUnlock(hGMem);
CloseClipboard();
         }