在VC6中建立了一个名为CDrawPicView的基于单文档的工程,相关的知识在后面的程序及注释中
// DrawPicView.h中的主要内容如下:
class CDrawPicView : public CView
{
protected: // create from serialization only
CDrawPicView();
DECLARE_DYNCREATE(CDrawPicView)
// Attributes
public:
CDrawPicDoc* GetDocument();
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CDrawPicView)
public:
virtual void OnDraw(CDC* pDC); // overridden to draw this view
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CDrawPicView();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
// Generated message map functions
protected:
//{{AFX_MSG(CDrawPicView)
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
afx_msg void OnRButtonUp(UINT nFlags, CPoint point);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
private:
CPoint m_OldPoint;
bool m_drawflag;
CPoint m_originalpoint;
};
//DrawPicView.cpp中的核心代码如下
// DrawPicView.cpp : implementation of the CDrawPicView class
//
#include "stdafx.h"
#include "DrawPic.h"
#include "DrawPicDoc.h"
#include "DrawPicView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/
// CDrawPicView
IMPLEMENT_DYNCREATE(CDrawPicView, CView)
BEGIN_MESSAGE_MAP(CDrawPicView, CView)
//{{AFX_MSG_MAP(CDrawPicView)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_RBUTTONDOWN()
ON_WM_RBUTTONUP()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/
// CDrawPicView construction/destruction
CDrawPicView::CDrawPicView()
{
// TODO: add construction code here
m_originalpoint=0;
m_OldPoint=0;
m_drawflag=false;
}
CDrawPicView::~CDrawPicView()
{
}
BOOL CDrawPicView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/
// CDrawPicView drawing
void CDrawPicView::OnDraw(CDC* pDC)
{
CDrawPicDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/
// CDrawPicView printing
BOOL CDrawPicView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CDrawPicView::OnBeginPrinting(CDC* , CPrintInfo* )
{
// TODO: add extra initialization before printing
}
void CDrawPicView::OnEndPrinting(CDC* , CPrintInfo* )
{
// TODO: add cleanup after printing
}
/
// CDrawPicView diagnostics
/
// CDrawPicView message handlers
void CDrawPicView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_originalpoint=point;//点击鼠标左键就获取一个起始点
m_drawflag=true;
CView::OnLButtonDown(nFlags, point);
}
void CDrawPicView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
//利用SDK的全局函数实现画线功能
//获得窗口的设备描述表
HDC hdc=::GetDC(m_hWnd);//若不加冒号,会默认为CWnd类的成员函数
//移动到线条的起点
MoveToEx(hdc,m_originalpoint.x,m_originalpoint.y,NULL);
//画线
LineTo(hdc,point.x,point.y);
//释放设备描述表
::ReleaseDC(m_hWnd,hdc);
m_drawflag=false;
CView::OnLButtonUp(nFlags, point);
}
void CDrawPicView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
//利用MFC的CDC类实现画带边框的扇形功能,需添加一个CPoint类型的变量m_OldPoint
CDC *pDC=GetDC();
pDC->SetROP2(R2_MASKNOTPEN);//设置绘图模式的函数,(final pixel = (NOT pen) AND screen pixel).
CPen pen(PS_SOLID,2,RGB(255,0,0));//构造一个GDI对象,虚线线性只在画笔宽度不大于1时才有效
CPen* poldpen=pDC->SelectObject(&pen);//返回先前被选对象的指针
pDC->MoveTo(m_originalpoint);
pDC->LineTo(point);
pDC->LineTo(m_OldPoint);
m_OldPoint=point;
pDC->SelectObject(poldpen);//将先前的GDI对象选人设备描述表,以便恢复先前的状态
ReleaseDC(pDC);
CView::OnMouseMove(nFlags, point);
}
void CDrawPicView::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
//透明画刷
//CBrush并没有提供创建透明画刷的功能
CWindowDC dc(this);
//创建一个空画刷
//GetObject函数返回的是一个画刷句柄,返回类型是HGDIOBJECT,需强制转为HBRUSH类型
//CBrush类提供了一个将画刷句柄转换为画刷对象的静态成员函数
CBrush *pbrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
//将空画刷选人设备描述表
CBrush *poldbrush=dc.SelectObject(pbrush);
//绘制一个矩形
dc.Rectangle(CRect(m_originalpoint,point));
//恢复先前的画刷
dc.SelectObject(poldbrush);
CView::OnRButtonDown(nFlags, point);
}
void CDrawPicView::OnRButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
//创建一个简单的蓝色画刷,设备描述表中有一个默认的白色画刷
//CBrush brush(RGB(0,0,255));
//创建位图对象
CBitmap bitmap;
//CBitmap类提供了LoadBitmap、CreateBitmap、CreateBitmIndirect等初始化函数
bitmap.LoadBitmap(IDB_BITMAP);
//创建位图画刷
CBrush brush(&bitmap);
//创建并获得设备描述表
CClientDC dc(this);
//利用红色画刷填充矩形区域
//FillRect直接利用指定画刷填充矩形,因此不需将画刷提前选人设备描述表
dc.FillRect(CRect(m_originalpoint,point),&brush);
CView::OnRButtonUp(nFlags, point);
}