This section contains sample code that demonstrates the creation of a picture and the process of storing the corresponding records in a metafile. In order to create a picture, a display device context (or DC) is obtained by calling the GetDC function and then various GDI functions are called (using this DC) to draw the lines, arcs, rectangles and other parts of the picture. In order to store the picture in a metafile, an enhanced metafile device context (or DC) is created (as shown in the previous example) and the same GDI functions which draw the picture on the display are called a second time to store the records in the enhanced metafile. However, the second time a function is called, the DC handle identifies the metafile DC (rather than the display DC).
The following sample code is a function that draws an object in the client-area of the application's display and stores a copy of the picture in a metafile:
void TransformAndDraw(int iTransform, HWND hwnd, HDC hdcMeta)
{
XFORM xForm;
RECT rect;
HDC hDC;
/* Create a reference DC. */
hDC = GetDC(hwnd);
/* Set the mapmode in the display DC. */
SetMapMode(hDC, MM_LOENGLISH);
/* Set the mapmode in the metafile DC. */
SetMapMode(hdcMeta, MM_LOENGLISH);
/* */
/* Set the appropriate world-transform (based on the user's */
/* menu selection). */
/* */
switch (iTransform) {
case SCALE: /* Scale to 1/2 original size. */
xForm.eM11 = (FLOAT)1.5;
xForm.eM12 = (FLOAT)0.0;
xForm.eM21 = (FLOAT)0.0;
xForm.eM22 = (FLOAT)1.5;
xForm.eDx = (FLOAT)0.0;
xForm.eDy = (FLOAT)0.0;
if (hdcMeta)
SetWorldTransform(hdcMeta, &xForm);
SetWorldTransform(hDC, &xForm);
break;
case TRANSLATE: /* Translate right by 3/4 inch. */
xForm.eM11 = (FLOAT) 1.0;
xForm.eM12 = (FLOAT) 0.0;
xForm.eM21 = (FLOAT) 0.0;
xForm.eM22 = (FLOAT) 1.0;
xForm.eDx = (FLOAT)75.0;
xForm.eDy = (FLOAT) 0.0;
if (hdcMeta)
SetWorldTransform(hdcMeta, &xForm);
SetWorldTransform(hDC, &xForm);
break;
case ROTATE: /* Rotate 30 degrees counter clockwise. */
xForm.eM11 = (FLOAT) 0.8660;
xForm.eM12 = (FLOAT) 0.5000;
xForm.eM21 = (FLOAT)-0.5000;
xForm.eM22 = (FLOAT) 0.8660;
xForm.eDx = (FLOAT) 0.0;
xForm.eDy = (FLOAT) 0.0;
if (hdcMeta)
SetWorldTransform(hdcMeta, &xForm);
SetWorldTransform(hDC, &xForm);
break;
case SHEAR: /* Shear along the x-axis with a */
/* proportionality constant of 1.0. */
xForm.eM11 = (FLOAT) 1.0;
xForm.eM12 = (FLOAT) 1.0;
xForm.eM21 = (FLOAT) 0.0;
xForm.eM22 = (FLOAT) 1.0;
xForm.eDx = (FLOAT) 0.0;
xForm.eDy = (FLOAT) 0.0;
if (hdcMeta)
SetWorldTransform(hdcMeta, &xForm);
SetWorldTransform(hDC, &xForm);
break;
case REFLECT: /* Reflect about a horizontal axis. */
xForm.eM11 = (FLOAT) 1.0;
xForm.eM12 = (FLOAT) 0.0;
xForm.eM21 = (FLOAT) 0.0;
xForm.eM22 = (FLOAT)-1.0;
xForm.eDx = (FLOAT) 0.0;
xForm.eDy = (FLOAT) 0.0;
if (hdcMeta)
SetWorldTransform(hdcMeta, &xForm);
SetWorldTransform(hDC, &xForm);
break;
case NORMAL: /* Set the unity transform. */
xForm.eM11 = (FLOAT)1.0;
xForm.eM12 = (FLOAT)0.0;
xForm.eM21 = (FLOAT)0.0;
xForm.eM22 = (FLOAT)1.0;
xForm.eDx = (FLOAT)0.0;
xForm.eDy = (FLOAT)0.0;
if (hdcMeta)
SetWorldTransform(hdcMeta, &xForm);
SetWorldTransform(hDC, &xForm);
break;
}
/* Find the midpoint of the client area */
GetClientRect(hwnd, (LPRECT)&rect);
DPtoLP(hDC, (LPPOINT)&rect, 2);
/* */
/* Draw a front-view of a gib head key */
/* */
if (hdcMeta)
SelectObject(hdcMeta, GetStockObject(HOLLOW_BRUSH));
SelectObject(hDC, GetStockObject(HOLLOW_BRUSH));
/* Draw the exterior circle */
if (hdcMeta)
Ellipse(hdcMeta, (rect.right/2 - 100), (rect.bottom/2 + 100),
(rect.right/2 + 100), (rect.bottom/2 - 100));
Ellipse(hDC, (rect.right/2 - 100), (rect.bottom/2 + 100),
(rect.right/2 + 100), (rect.bottom/2 - 100));
/* Draw the interior circle */
if (hdcMeta)
Ellipse(hdcMeta, (rect.right/2 -94), (rect.bottom/2 + 94),
(rect.right/2 + 94), (rect.bottom/2 - 94));
Ellipse(hDC, (rect.right/2 -94), (rect.bottom/2 + 94),
(rect.right/2 + 94), (rect.bottom/2 - 94));
/* Draw the key */
if (hdcMeta){
Rectangle(hdcMeta, (rect.right/2 - 13), (rect.bottom/2 + 113),
(rect.right/2 + 13), (rect.bottom/2 + 50));
Rectangle(hdcMeta, (rect.right/2 - 13), (rect.bottom/2 + 96),
(rect.right/2 + 13), (rect.bottom/2 + 50));
} /* end if */
Rectangle(hDC, (rect.right/2 - 13), (rect.bottom/2 + 113),
(rect.right/2 + 13), (rect.bottom/2 + 50));
Rectangle(hDC, (rect.right/2 - 13), (rect.bottom/2 + 96),
(rect.right/2 + 13), (rect.bottom/2 + 50));
/* Draw the horizontal lines */
if (hdcMeta){
MoveToEx(hdcMeta, (rect.right/2 - 150), (rect.bottom/2 + 0), NULL);
LineTo(hdcMeta, (rect.right/2 - 16), (rect.bottom/2 + 0));
MoveToEx(hdcMeta, (rect.right/2 - 13), (rect.bottom/2 + 0), NULL);
LineTo(hdcMeta, (rect.right/2 + 13), (rect.bottom/2 + 0));
MoveToEx(hdcMeta, (rect.right/2 + 16), (rect.bottom/2 + 0), NULL);
LineTo(hdcMeta, (rect.right/2 + 150), (rect.bottom/2 + 0));
} /* end if */
MoveToEx(hDC, (rect.right/2 - 150), (rect.bottom/2 + 0), NULL);
LineTo(hDC, (rect.right/2 - 16), (rect.bottom/2 + 0));
MoveToEx(hDC, (rect.right/2 - 13), (rect.bottom/2 + 0), NULL);
LineTo(hDC, (rect.right/2 + 13), (rect.bottom/2 + 0));
MoveToEx(hDC, (rect.right/2 + 16), (rect.bottom/2 + 0), NULL);
LineTo(hDC, (rect.right/2 + 150), (rect.bottom/2 + 0));
/* Draw the vertical lines */
if (hdcMeta){
MoveToEx(hdcMeta, (rect.right/2 + 0), (rect.bottom/2 - 150), NULL);
LineTo(hdcMeta, (rect.right/2 + 0), (rect.bottom/2 - 16));
MoveToEx(hdcMeta, (rect.right/2 + 0), (rect.bottom/2 - 13), NULL);
LineTo(hdcMeta, (rect.right/2 + 0), (rect.bottom/2 + 13));
MoveToEx(hdcMeta, (rect.right/2 + 0), (rect.bottom/2 + 16), NULL);
LineTo(hdcMeta, (rect.right/2 + 0), (rect.bottom/2 + 150));
} /* end if */
MoveToEx(hDC, (rect.right/2 + 0), (rect.bottom/2 - 150), NULL);
LineTo(hDC, (rect.right/2 + 0), (rect.bottom/2 - 16));
MoveToEx(hDC, (rect.right/2 + 0), (rect.bottom/2 - 13), NULL);
LineTo(hDC, (rect.right/2 + 0), (rect.bottom/2 + 13));
MoveToEx(hDC, (rect.right/2 + 0), (rect.bottom/2 + 16), NULL);
LineTo(hDC, (rect.right/2 + 0), (rect.bottom/2 + 150));
ReleaseDC(hwnd, hDC);
}
The following illustration shows a page from a Word for Windows document that contains an illustration. (This illustration was created by the previous code sample, stored in an enhanced metafile, and then copied into the document):
To be supplied.