本节包含一个演示以下任务的示例:
*使用预定义的单位绘制图形。
*在应用程序的客户区中居中图形。
*将图形输出缩放到其原始尺寸的一半。
*向右翻译图形输出3/4英寸。
*旋转图形30度。
*沿x轴剪切图形输出。
*反映出通过其中点绘制的假想水平轴的图形输出。
以下示例用于创建本主题前面显示的插图。
void TransformAndDraw(int iTransform,HWND hWnd)
{
HDC hDC;
XFORM xForm;
RECT rect;
/ *检索应用程序窗口的DC句柄。*/
hDC = GetDC(hWnd);
/*
*将映射模式设置为LOENGLISH。这移动了
*客户区起源于左上角
*窗口左下角(这也重新定位
* y轴使绘图操作发生在真实状态
*笛卡尔空间)。它保证可移植性
*绘制的对象保留其任何尺寸
*显示运行Windows。
*/
SetMapMode(hDC, MM_LOENGLISH);
/*
*设置适当的世界变革(基于
*用户的菜单选择)。
*/
开关(iTransform){
case SCALE: /* Scale to 1/2 of the original size. */
xForm.eM11 = (FLOAT) 0.5;
xForm.eM12 = (FLOAT) 0.0;
xForm.eM21 = (FLOAT) 0.0;
xForm.eM22 = (FLOAT) 0.5;
xForm.eDx = (FLOAT) 0.0;
xForm.eDy = (FLOAT) 0.0;
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;
SetWorldTransform(hDC, &xForm);
break;
case ROTATE: /* Rotate 30 degrees counterclockwise. */
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;
SetWorldTransform(hDC, &xForm);
break;
case SHEAR: /* Shear along the x-axis with a */
/ *比例常数为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;
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;
SetWorldTransform(hDC, &xForm);
break;
case NORMAL: /* Set the unity transformation. */
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;
SetWorldTransform(hDC, &xForm);
break;
}
/ *查找客户区域的中点。*/
GetClientRect(hWnd, (LPRECT) &rect);
DPtoLP(hDC, (LPPOINT) &rect, 2);
/ *选择中空刷。*/
SelectObject(hDC, GetStockObject(HOLLOW_BRUSH));
/ *绘制外圆。*/
椭圆(hDC,(rect.right / 2 - 100),(rect.bottom / 2 + 100),
(rect.right / 2 + 100), (rect.bottom / 2 - 100));
/ *绘制内圈。*/
椭圆(hDC,(rect.right / 2 -94),(rect.bottom / 2 + 94),
(rect.right / 2 + 94), (rect.bottom / 2 - 94));
/ *绘制钥匙。 * /
矩形(hDC,(rect.right / 2-13),(rect.bottom / 2 + 113),
(rect.right / 2 + 13), (rect.bottom / 2 + 50));
矩形(hDC,(rect.right / 2-13),(rect.bottom / 2 + 96),
(rect.right / 2 + 13), (rect.bottom / 2 + 50));
/ *绘制水平线。*/
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));
/ *绘制垂直线。*/
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);
}