vc++ 学习笔记11
图形的保存和重绘
首先建立画图的四个选项:点,线,矩形,椭圆的按钮,并添加成员变量,表示每个图形的种类;
其次添加m_drawtype,和cpoint记录用户选择的画图类型和点的坐标位置;
添加消息响应函数,BUTTONDOWN和BTUTTONUP;并在函数里面实现画图的函数;
void CGraphic1View::OnPoint() { // TODO: Add your command handler code here m_nDrawType=1; } void CGraphic1View::OnLine() { // TODO: Add your command handler code here m_nDrawType=2; } void CGraphic1View::OnRectangle() { // TODO: Add your command handler code here m_nDrawType=3; } void CGraphic1View::OnElipse() { // TODO: Add your command handler code here m_nDrawType=4; } void CGraphic1View::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CView::OnLButtonDown(nFlags, point); m_point=point; } void CGraphic1View::OnLButtonUp(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CClientDC dc(this); CBrush *pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));//此处利用CBrush里面的静态创建Fromhandle()函数,通过一个dc句柄来创建一个画刷,dc句柄通过GetstockObject()获得,传递一个透明画刷,并强转成画刷格式; dc.SelectObject(pBrush); switch(m_nDrawType) { case 1: dc.SetPixel(point,RGB(0,1,0)); break; case 2: dc.MoveTo(m_point); dc.LineTo(point); break; case 3: dc.Rectangle(CRect(m_point,point)); break; case 4: dc.Ellipse(CRect(m_point,point)); break; default: break; } CView::OnLButtonUp(nFlags, point); }
此时可以画图,但是当窗口变化时,图像消失,因此,想到建立一个数据结构,保存用户画的图像特点,然后将其重画;因此插入一个普通类,保存图像数据;
class CGraphic { public: CGraphic(); CGraphic(UINT m_SaveDrawType,CPoint m_StartPoint,CPoint m_EndPoint);//重载一个初始化函数,方便用户对3个成员变量赋值 virtual ~CGraphic(); public: UINT m_SaveDrawType; CPoint m_StartPoint; CPoint m_EndPoint; };
关于图形坐标的轮换操作需要重新疏导,有点乱
Edit By SolarJupiter