VC++ 元文件保存矢量图形

1、内存元文件

1)创建元文件 CMetaFileDC m_dcMetaFile;

2)初始化 m_dcMetaFile.Create();

3)在OnLButtonUp中添加代码

CClientDC dc(this);


CPen pen(0,1,RGB(255,0,0));


dc.SelectObject(&pen);
m_dcMetaFile.SelectObject(&pen);
CBrush *pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));


dc.SelectObject(pBrush);
m_dcMetaFile.SelectObject(pBrush);


switch(m_nDrawType)
{
case 1:
dc.SetPixel(point,RGB(255,0,0));
m_dcMetaFile.SetPixel(point,RGB(255,0,0));
break;
case 2:
dc.MoveTo(m_ptOrigin);
dc.LineTo(point);
m_dcMetaFile.MoveTo(m_ptOrigin);
m_dcMetaFile.LineTo(point);
break;
case 3:
dc.Rectangle(CRect(m_ptOrigin,point));
m_dcMetaFile.Rectangle(CRect(m_ptOrigin,point));
break;
case 4:
dc.Ellipse(CRect(m_ptOrigin,point));
m_dcMetaFile.Ellipse(CRect(m_ptOrigin,point));
break;
default:
break;

}

4)、回放

HMETAFILE hmetafile;
hmetafile=m_dcMetaFile.Close();

pDC->PlayMetaFile(hmetafile);
m_dcMetaFile.Create();

m_dcMetaFile.PlayMetaFile(hmetafile);

DeleteMetaFile(hmetafile);

2、本地元文件的保存与打开

1)在文件/保存中添加代码

HMETAFILE hmetafile;
hmetafile=GetMetaFile("meta.wmf");
m_dcMetaFile.PlayMetaFile(hmetafile);
DeleteMetaFile(hmetafile);
Invalidate();



2)在文件/打开中添加代码

HMETAFILE hmetafile;
hmetafile=m_dcMetaFile.Close();
CopyMetaFile(hmetafile,"meta.wmf");
m_dcMetaFile.Create();
DeleteMetaFile(hmetafile);

3、兼容设备描述表

CDC m_dcCompatible;

OnLButtonUp中

if(!m_dcCompatible.m_hDC)
{
m_dcCompatible.CreateCompatibleDC(&dc);
CRect rect;
GetClientRect(&rect);
CBitmap bitmap;
bitmap.CreateCompatibleBitmap(&dc,rect.Width(),rect.Height());
m_dcCompatible.SelectObject(&bitmap);
m_dcCompatible.SelectObject(pBrush);

m_dcCompatible.BitBlt(0,0,rect.Width(),rect.Height(),&dc,0,0,SRCCOPY);
}



switch(m_nDrawType)
{
case 1:
dc.SetPixel(point,RGB(255,0,0));

m_dcCompatible.SetPixel(point,RGB(255,0,0));
break;
case 2:
dc.MoveTo(m_ptOrigin);
dc.LineTo(point);

m_dcCompatible.MoveTo(m_ptOrigin);
m_dcCompatible.LineTo(point);
break;
case 3:
dc.Rectangle(CRect(m_ptOrigin,point));

m_dcCompatible.Rectangle(CRect(m_ptOrigin,point));
break;
case 4:
dc.Ellipse(CRect(m_ptOrigin,point));

m_dcCompatible.Ellipse(CRect(m_ptOrigin,point));
break;
default:
break;

}

OnDraw中

CRect rect;
GetClientRect(&rect);
pDC->BitBlt(0,0,rect.Width(),rect.Height(),&m_dcCompatible,0,0,SRCCOPY);



你可能感兴趣的:(vc++)