图形的保存与重绘
1.创建图形
新建一个MFC单文档应用程序,仍然增加一个菜单,命名绘图,再添加几个菜单项:
IDM_POINT(点)、IDM_LINE(直线)、IDM_
并在构造方法中初始化:
CGraphic2View::CGraphic2View()
{
}
再编辑四个菜单项的命令响应:
void
{
}
void
{
m_nDrawType=2;
}
void
{
}
void
{
}
再在CGraphic2View类中添加一个私有的成员变量m_ptOrigin用来保存原点坐标,并在构造函数中初始化,上面已列出。
接着再给CGraphic2View类添加两个WM_LBUTTONDOWN和WM_LBUTTONUP消息响应函数,编辑:
void
{
m_ptOrigin=point;//将这个点保存起来
}
void
{
}
运行可以发现当窗口尺寸发现变化是,所绘制的图形也消失了!
2.图形重绘
新建一个Generic Class (name:CGraph),并在这个类当中增加3个成员变量,同时为了对这3个成员进行赋值,定义一个带参数的构造函数,如下:
public:
并在源文件Graph.cpp当中,对这个类的成员变量赋值:
CGraph::CGraph(UINT
{
}
在CGraphic2View类当中,新增加一个成员变量CPtrArray m_ptrArray;
CGraph graph(m_nDrawType,m_ptOrigin,point);//构造一个对象
//注:此时graph是一个局部变量,为它分配的内存在栈中,当这个函数结束时,对象也会被析构
m_ptrArray.Add(&graph);//将graph对象保存到集合类的对象m_ptArray当中
接下来在CGraphic2View::OnDraw函数中添加:
CBrush
运行,当改变窗口大小时,图像仍然消息了,下面继续修改。
在CGraphic2View::OnLButtonUp中修改:
//CGraph
运行,绘制图形,改变窗口大小,图形没有消失,OK!
3.将图形输出放置到WM_PAINT消息响应函数中
在CGraphic2View类上添加一个WM_PAINT消息处理,编辑:
void
{
}
4.给窗口添加滚动条
首先将CGraphic2View的基类CView手动改成CScorllView,在CGraphic2View的头文件中和源文件中将所有的CView都替换成CScrollView,编译运行,会发现出现了非法操作,
Coordinate Spaces and Transformations
Applications use coordinate spaces and transformations to scale, rotate, translate, shear, and reflect graphics output. Acoordinate space
A
一个例子:
Using Coordinate Spaces and Transformations
This section contains an example that demonstrates the following tasks:
- Drawing graphics with predefined units.
- Centering graphics in the application's client area.
- Scaling graphics output to half its original size.
- Translating graphics output 3/4 of an inch to the right.
- Rotating graphics 30 degrees.
- Shearing graphics output along the x-axis.
- Reflecting graphics output about an imaginary horizontal axis drawn through its midpoint.
The following example was used to create the illustrations that appear earlier in this overview.
void TransformAndDraw(int iTransform, HWND hWnd)
{
HDC hDC;
XFORM xForm;
RECT rect;
// Retrieve a DC handle for the application's window.
hDC = GetDC(hWnd);
// Set the mapping mode to LOENGLISH. This moves the
// client area origin from the upper left corner of the
// window to the lower left corner (this also reorients
// the y-axis so that drawing operations occur in a true
// Cartesian space). It guarantees portability so that
// the object drawn retains its dimensions on any display.
SetGraphicsMode(hDC, GM_ADVANCED);
SetMapMode(hDC, MM_LOENGLISH);
// Set the appropriate world transformation (based on the
// user's menu selection).
switch (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
// 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;
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;
}
// Find the midpoint of the client area.
GetClientRect(hWnd, (LPRECT) &rect);
DPtoLP(hDC, (LPPOINT) &rect, 2);
// Select a hollow brush.
SelectObject(hDC, GetStockObject(HOLLOW_BRUSH));
// Draw the exterior circle.
Ellipse(hDC, (rect.right / 2 - 100), (rect.bottom / 2 + 100),
(rect.right / 2 + 100), (rect.bottom / 2 - 100));
// Draw the interior circle.
Ellipse(hDC, (rect.right / 2 -94), (rect.bottom / 2 + 94),
(rect.right / 2 + 94), (rect.bottom / 2 - 94));
// Draw the key.
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.
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.
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);
}
逻辑坐标与设备坐标的相互转换
DMM_TEXT映射方式下逻辑坐标与设备坐标的相互转换
接下来,在CGraphic2View类上添加一个虚函数,编辑:
void
{
}
运行,可以发现多了滚动条,当把窗口滚动条拖到最下方时,再画一图形,切换窗口,再切回来,发现图形上移,下面解决这个问题。
视口和窗口原点的改变
关于图形错位的说明
解决方法
在CGraphic2View::OnLButtonUp中编辑:
说明:OnPrepareDC会随时根据滚动窗口的位置来调整视口的原点。
5.保存图形和重绘图形的另两种方式
先在CGraphic2View类上添加一个私有的成员变量CMetaFileDC m_dcMetaFile;
然后在构造函数当中:
CGraphic2View::CGraphic2View()
{
}
然后在CGraphic2View::OnLButtonUp函数中修改:
void
{
}
并在CGraphic2View::OnDraw函数中修改:
void
{
}
运行,绘制图形,改变窗口大小后,图形显示出来了。
6.将绘制的元文件保存到元文件当中和打开保存的元文件
方式一:利用元文件
先给IDR_MAINFRAME菜单的保存和打开子项添加命令响应,编辑:
void
{
//保存元文件
}
{
//打开元文件
}
方式二:利用兼容DC
首先在CGraphic2View类当中增加一个私有成员变量
void
{
}
并在CGraphic2View::OnDraw函数中编辑:
void
{
}
转载出处:http://www.360doc.com/content/12/1112/15/9200790_247407525.shtml
示例下载地址:http://download.csdn.net/detail/qq_23992597/9436826