孙鑫vc学习笔记_第11课_part1

来源:互联网 发布:ueba 网络事件 编辑:程序博客网 时间:2024/06/07 06:31
 
如何让CDC上输出的文字、图形具有保持功能,集合类CPtrArray的使用,CPaintDC与CClientDC的区别与应用,OnPaint与OnDraw在CView中的关系及实现内幕,滚动窗口的实现,坐标空间,映射方式,设备坐标与逻辑坐标的转换。元文件设备描述表的使用,如何利用兼容DC实现图形的保存和再现。
 
#先实现一下上节课的绘图功能
#保存所绘制的图像以及图像的重绘00:09
3个要素 绘制的类型,起点,终点
保存这3个要素就可以在OnDraw中对其进行重绘
×新建一个类来保存CGraph
class CGraph 
{
public:
    CPoint m_ptOrigin; //起点
    CPoint m_ptEnd; //终点
    UINT m_nDrawType; //类型
    CGraph();
    CGraph(UINT m_nDrawType,CPoint m_ptOrigin,CPoint m_ptEnd);
    virtual ~CGraph();
};
×动态存储这些对象
用MFC的集合类CPtrArray,支持void指针数组。CPtrArray与CObArray相似
MSDN:
The CPtrArray class supports arrays of void pointers.
The member functions of CPtrArray are similar to the member functions of class CObArray. Because of this similarity, you can use the CObArray reference documentation for member function specifics. Wherever you see a CObject pointer as a function parameter or return value, substitute a pointer to void.
CObject* CObArray::GetAt( int <nIndex> ) const;
for example, translates to
void* CPtrArray::GetAt( int <nIndex> ) const;
 
#增加Add,获取元素GetAt,获取元素数目GetSize
#代码:
//void CGraphicView::OnLButtonUp(UINT nFlags, CPoint point)
//使用new在堆中分配空间,在栈中分配的话CGraph的对象析构以后内存被回收
       CGraph *pGraph=new CGraph(m_nDrawType,m_ptOrigin,point);
       m_ptrArray.Add(pGraph); //CPtrArray m_ptrArray;
 
//void CGraphicView::OnDraw(CDC* pDC)
       CBrush *pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
       pDC->SelectObject(pBrush);
 
       for(int i=0;i<m_ptrArray.GetSize();i++)
       {
              //void* to CGraph* 类型转换
              switch(((CGraph*)m_ptrArray.GetAt(i))->m_nDrawType)
              {
              case 1:
                     pDC->SetPixel(((CGraph*)m_ptrArray.GetAt(i))->m_ptEnd,RGB(0,0,0));
                     break;
              case 2:
                     pDC->MoveTo(((CGraph*)m_ptrArray.GetAt(i))->m_ptOrigin);
                     pDC->LineTo(((CGraph*)m_ptrArray.GetAt(i))->m_ptEnd);
                     break;
              case 3:
                     pDC->Rectangle(CRect(((CGraph*)m_ptrArray.GetAt(i))->m_ptOrigin,
                            ((CGraph*)m_ptrArray.GetAt(i))->m_ptEnd));
                     break;
              case 4:
                     pDC->Ellipse(CRect(((CGraph*)m_ptrArray.GetAt(i))->m_ptOrigin,
                            ((CGraph*)m_ptrArray.GetAt(i))->m_ptEnd));
                     break;
              }
       }