VC视图View操作

本文以一个简单的例子实现View视图的相关操作。


首先,新建一个单文档应用程序


然后,修改菜单栏

 


添加图片



在View类添加相关变量和函数

class CDrawOnViewView : public CView
{
protected: // create from serialization only
	CDrawOnViewView();
	DECLARE_DYNCREATE(CDrawOnViewView)

// Attributes
public:
	CDrawOnViewDoc* GetDocument();

// Operations
public:

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CDrawOnViewView)
	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 ~CDrawOnViewView();
#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif

protected:

// Generated message map functions
protected:
	//{{AFX_MSG(CDrawOnViewView)
	afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
	afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
	afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
	afx_msg void OnEditPen();
	afx_msg void OnEditBrush();
	afx_msg void OnEditBitmap();
	afx_msg void OnEditNone();
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
public:
	CPoint m_ptOrigin;
	CBitmap m_bmp;
	DWORD m_dwDrawType;
};

功能实现

IMPLEMENT_DYNCREATE(CDrawOnViewView, CView)

BEGIN_MESSAGE_MAP(CDrawOnViewView, CView)
	//{{AFX_MSG_MAP(CDrawOnViewView)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_CREATE()
	ON_COMMAND(ID_EDIT_PEN, OnEditPen)
	ON_COMMAND(ID_EDIT_BRUSH, OnEditBrush)
	ON_COMMAND(ID_EDIT_BITMAP, OnEditBitmap)
	ON_COMMAND(ID_EDIT_NONE, OnEditNone)
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CDrawOnViewView construction/destruction

CDrawOnViewView::CDrawOnViewView()
{
	// TODO: add construction code here
	m_dwDrawType = 0;
}

CDrawOnViewView::~CDrawOnViewView()
{
}

BOOL CDrawOnViewView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CDrawOnViewView drawing

/////////////////////////////////////////////////////////////////////////////
// CDrawOnViewView printing

BOOL CDrawOnViewView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CDrawOnViewView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CDrawOnViewView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CDrawOnViewView diagnostics

#ifdef _DEBUG
void CDrawOnViewView::AssertValid() const
{
	CView::AssertValid();
}

void CDrawOnViewView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CDrawOnViewDoc* CDrawOnViewView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDrawOnViewDoc)));
	return (CDrawOnViewDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CDrawOnViewView message handlers

void CDrawOnViewView::OnDraw(CDC* pDC)
{
	CDrawOnViewDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	
	CString str="";
	str = "TestWord";
	pDC->TextOut(80,70,str);

	str.LoadString(IDS_STRING_TEST);
	pDC->TextOut(80,100,str);
}

void CDrawOnViewView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	m_ptOrigin = point;
	
	CView::OnLButtonDown(nFlags, point);
}

void CDrawOnViewView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CClientDC dc(this);

	switch(m_dwDrawType)
	{
	case 0://画笔
		{
			CPen Pen(PS_SOLID, 1, RGB(255,0,0));
			CPen *pOldPen = dc.SelectObject(&Pen);
			dc.MoveTo(m_ptOrigin);
			dc.LineTo(point);
			dc.SelectObject(pOldPen);
		}
		break;
	case 1://画刷
		{
			CBrush Brush(RGB(255,0,0));
			dc.FillRect(CRect(m_ptOrigin,point),&Brush);
		}
		break;
	case 2://位图画刷
		{
			CBitmap Bitmap;
			Bitmap.LoadBitmap(IDB_BITMAP1);
			CBrush Brush(&Bitmap);
			dc.FillRect(CRect(m_ptOrigin,point),&Brush);
		}
		break;
	case 3://透明画刷
		{
			CBrush *pBrush = CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
			CBrush *pOldBrush = dc.SelectObject(pBrush);
			dc.Rectangle(CRect(m_ptOrigin,point));
			dc.SelectObject(pOldBrush);
		}
		break;
	default:
		break;
	}

	CView::OnLButtonUp(nFlags, point);
}

int CDrawOnViewView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	CClientDC dc(this);
	TEXTMETRIC tm;
	dc.GetTextMetrics(&tm);
	
//	CreateSolidCaret(20,100);
	
	m_bmp.LoadBitmap(IDB_BITMAP1);
	CreateCaret(&m_bmp);
	ShowCaret();
	
	return 0;
}

void CDrawOnViewView::OnEditPen() 
{
	m_dwDrawType = 0;
}

void CDrawOnViewView::OnEditBrush() 
{
	m_dwDrawType = 1;	
}

void CDrawOnViewView::OnEditBitmap() 
{
	m_dwDrawType = 2;
}

void CDrawOnViewView::OnEditNone() 
{
	m_dwDrawType = 3;
}

运行效果



画图后

VC视图View操作_第1张图片


源码下载


你可能感兴趣的:(操作,视图,view,VC)