首先在过程内建立一个位图,然后以该位图作为画笔原型。程序如下:
// lessonMyBrushView.cpp : implementation of the ClessonMyBrushView class
//
#include "stdafx.h"
// SHARED_HANDLERS can be defined in an ATL project implementing preview, thumbnail
// and search filter handlers and allows sharing of document code with that project.
#ifndef SHARED_HANDLERS
#include "lessonMyBrush.h"
#endif
#include "lessonMyBrushDoc.h"
#include "lessonMyBrushView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// ClessonMyBrushView
IMPLEMENT_DYNCREATE(ClessonMyBrushView, CView)
BEGIN_MESSAGE_MAP(ClessonMyBrushView, CView)
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, &ClessonMyBrushView::OnFilePrintPreview)
ON_WM_CONTEXTMENU()
ON_WM_RBUTTONUP()
ON_WM_LBUTTONUP()
ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()
// ClessonMyBrushView construction/destruction
ClessonMyBrushView::ClessonMyBrushView()
: myPoint(0)
{
// TODO: add construction code here
}
ClessonMyBrushView::~ClessonMyBrushView()
{
}
BOOL ClessonMyBrushView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
// ClessonMyBrushView drawing
void ClessonMyBrushView::OnDraw(CDC* /*pDC*/)
{
ClessonMyBrushDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
// TODO: add draw code for native data here
}
// ClessonMyBrushView printing
void ClessonMyBrushView::OnFilePrintPreview()
{
#ifndef SHARED_HANDLERS
AFXPrintPreview(this);
#endif
}
BOOL ClessonMyBrushView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void ClessonMyBrushView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void ClessonMyBrushView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
void ClessonMyBrushView::OnRButtonUp(UINT /* nFlags */, CPoint point)
{
ClientToScreen(&point);
OnContextMenu(this, point);
}
void ClessonMyBrushView::OnContextMenu(CWnd* /* pWnd */, CPoint point)
{
#ifndef SHARED_HANDLERS
theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
#endif
}
// ClessonMyBrushView diagnostics
#ifdef _DEBUG
void ClessonMyBrushView::AssertValid() const
{
CView::AssertValid();
}
void ClessonMyBrushView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
ClessonMyBrushDoc* ClessonMyBrushView::GetDocument() const // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(ClessonMyBrushDoc)));
return (ClessonMyBrushDoc*)m_pDocument;
}
#endif //_DEBUG
// ClessonMyBrushView message handlers
void ClessonMyBrushView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
//CBitmap bitmap;
/*CBrush brush(RGB(255,0,0));
CClientDC ccdc(this);
ccdc.FillRect(CRect(myPoint,point),&brush);
*/
CBitmap bitmap;
bitmap.LoadBitmap(IDB_BITMAP1);
CBrush brush(&bitmap);
CClientDC ccdc(this);
ccdc.FillRect(CRect(myPoint,point),&brush);
CView::OnLButtonUp(nFlags, point);
}
void ClessonMyBrushView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
myPoint=point;
CView::OnLButtonDown(nFlags, point);
}
参考:
VC++2010开发权威指南,尹成,pp.431