MFC Library: CMetaFileDC |
|
A metafile is a series of instructions used to draw one or a group of graphics on a device context. Unlike drawing on a regular device context, a metafile includes its instructions in a normal windows file. The instructions are then used to draw what is necessary. One of the strengths of a metafile is that, because it is a Windows file, it can be accessed by any application that can read it. This means that you can create a metafile in your Visual C++ application and be able to open it in a graphics application like Paint Shop Pro.
In Visual C++, a metafile can be created using the CMetaFileDC class. This class is based on CDC. For this reason, it inherits any type of drawing you can perform on a device context. This significantly reduces the amount of learning. To start, you can declare a CMetaFileDC variable using its default constructor. Using this variable, call the Create() method to formally initiate the file. The syntax of the CMetaFileDC::Create() method is: BOOL Create(LPCTSTR lpszFilename = NULL); This methods takes an optional argument as the name of the file that will hold the instructions. If you don't supply the argument, a default file would be created. After calling the Create() method, you can perform any necessary drawing on the CMetaFileDC variable as you would proceed with a CDC object. After using the CMetaFileDC variable, you should close it. This is done by calling the Close() method. Its syntax is: HMETAFILE Close(); Here is an example: void CMFCMetaFile1View::OnDraw(CDC* /*pDC*/) { CMFCMetaFile1Doc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (!pDoc) return; // TODO: add draw code for native data here CMetaFileDC mfDC; mfDC.Create("Meta1.wmf"); CBrush BrushGreen(RGB(0, 125, 5)); CBrush BrushRed(RGB(255, 2, 5)); CBrush BrushYellow(RGB(250, 255, 5)); CBrush BrushBlue(RGB(0, 2, 255)); CBrush *pBrush; CPoint Pt[3]; // Top Triangle Pt[0] = CPoint(125, 10); Pt[1] = CPoint( 95, 70); Pt[2] = CPoint(155, 70); pBrush = mfDC.SelectObject(&BrushGreen); mfDC.Polygon(Pt, 3); // Left Triangle Pt[0] = CPoint( 80, 80); Pt[1] = CPoint( 20, 110); Pt[2] = CPoint( 80, 140); pBrush = mfDC.SelectObject(&BrushRed); mfDC.Polygon(Pt, 3); // Bottom Triangle Pt[0] = CPoint( 95, 155); Pt[1] = CPoint(125, 215); Pt[2] = CPoint(155, 155); pBrush = mfDC.SelectObject(&BrushYellow); mfDC.Polygon(Pt, 3); // Right Triangle Pt[0] = CPoint(170, 80); Pt[1] = CPoint(170, 140); Pt[2] = CPoint(230, 110); pBrush = mfDC.SelectObject(&BrushBlue); mfDC.Polygon(Pt, 3); mfDC.SelectObject(pBrush); } |
After creating a metafile, it becomes a regular Windows file: You can open it in your application when necessary. Here is an example that opens the above metafile when the user clicks the view area of the application:
Of course, you can also provides the user with the ability to select a metafile to open using the File Dialog box. As mentioned already, a metafile can also be opened by any application that can read it. For example, here is the above metafile opened in Paint Shop Pro: |
|
||
Home | Copyright © 2002-2007 FunctionX, Inc. | |
|