第1个回答 2008-04-22
我只会写简单的操作,给你做个参考吧
// DrawView.cpp : implementation of the CDrawView class
//
#include "stdafx.h"
#include "Draw.h"
#include "DrawDoc.h"
#include "DrawView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDrawView
IMPLEMENT_DYNCREATE(CDrawView, CView)
BEGIN_MESSAGE_MAP(CDrawView, CView)
//{{AFX_MSG_MAP(CDrawView)
ON_WM_CREATE()
ON_COMMAND(ID_DRAW_LINECOLOR, OnDrawLinecolor)
ON_COMMAND(ID_DRAW_FILLCOLOR, OnDrawFillcolor)
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDOWN()
ON_WM_SETCURSOR()
//}}AFX_MSG_MAP
ON_COMMAND_RANGE(ID_DRAW_LINE,ID_DRAW_FILL, OnSelectDrawType)
ON_UPDATE_COMMAND_UI_RANGE(ID_DRAW_LINE,ID_DRAW_FILL, OnUpdateSelectDrawType)
ON_COMMAND_RANGE(ID_DRAW_LINEWIDTH_ONE,ID_DRAW_LINEWIDTH_FIVE, OnDrawLineWidth)
ON_UPDATE_COMMAND_UI_RANGE(ID_DRAW_LINEWIDTH_ONE,ID_DRAW_LINEWIDTH_FIVE,OnUpdateDrawLineWidth)
// 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()
/////////////////////////////////////////////////////////////////////////////
// CDrawView construction/destruction
CDrawView::CDrawView()
{
// TODO: add construction code here
m_pMemDC = new CDC;
m_pBitmap = new CBitmap;
m_nDrawType = -1;
m_nLineWidth = 1;
m_cLineColor = RGB(0,0,0);
m_cFillColor = RGB(0,0,255);
m_bDrawing = false;
}
CDrawView::~CDrawView()
{
delete m_pMemDC;
delete m_pBitmap;
}
BOOL CDrawView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CDrawView drawing
void CDrawView::OnDraw(CDC* pDC)
{
CDrawDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CBitmap * pOldBitmap = m_pMemDC->SelectObject(m_pBitmap);
pDC->BitBlt(0,0,m_nMaxX, m_nMaxY, m_pMemDC, 0,0, SRCCOPY);
m_pMemDC->SelectObject(pOldBitmap);
}
/////////////////////////////////////////////////////////////////////////////
// CDrawView printing
BOOL CDrawView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CDrawView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CDrawView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CDrawView diagnostics
#ifdef _DEBUG
void CDrawView::AssertValid() const
{
CView::AssertValid();
}
void CDrawView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CDrawDoc* CDrawView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDrawDoc)));
return (CDrawDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CDrawView message handlers
int CDrawView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
m_nMaxX = GetSystemMetrics(SM_CXSCREEN);
m_nMaxY = GetSystemMetrics(SM_CYSCREEN);
CDC * pDC = GetDC();
m_pMemDC->CreateCompatibleDC(pDC);
m_pBitmap->CreateCompatibleBitmap(pDC, m_nMaxX, m_nMaxY);
CBitmap * pOldBitmap = m_pMemDC->SelectObject(m_pBitmap);
CBrush brush;
brush.CreateStockObject(WHITE_BRUSH);
CRect rect(-1,-1,m_nMaxX, m_nMaxY);
m_pMemDC->FillRect(rect,&brush);
m_pMemDC->SelectObject(pOldBitmap);
ReleaseDC(pDC);
return 0;
}
void CDrawView::OnSelectDrawType(UINT nID)
{
m_nDrawType = nID - ID_DRAW_LINE;
}
void CDrawView::OnUpdateSelectDrawType(CCmdUI * pCmdUI)
{
int nID = pCmdUI->m_nID - ID_DRAW_LINE;
if(nID == m_nDrawType)
pCmdUI->SetCheck(true);
else
pCmdUI->SetCheck(false);
}
void CDrawView::OnDrawLineWidth(UINT nID)
{
m_nLineWidth = nID - ID_DRAW_LINEWIDTH_ONE + 1;
}
void CDrawView::OnUpdateDrawLineWidth(CCmdUI * pCmdUI)
{
int nID = pCmdUI->m_nID - ID_DRAW_LINEWIDTH_ONE + 1;
if(nID == m_nLineWidth)
pCmdUI->SetCheck(true);
else
pCmdUI->SetCheck(false);
}
void CDrawView::OnDrawLinecolor()
{
// TODO: Add your command handler code here
CColorDialog dlg;
dlg.m_cc.Flags |= CC_PREVENTFULLOPEN | CC_RGBINIT;
dlg.m_cc.rgbResult = m_cLineColor;
if(dlg.DoModal() == IDOK)
{
m_cLineColor = dlg.GetColor();
}
}
void CDrawView::OnDrawFillcolor()
{
// TODO: Add your command handler code here
CColorDialog dlg;
dlg.m_cc.Flags |= CC_PREVENTFULLOPEN | CC_RGBINIT;
dlg.m_cc.rgbResult = m_cFillColor;
if(dlg.DoModal() == IDOK)
{
m_cFillColor = dlg.GetColor();
}
}
void CDrawView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
SetCapture();
m_bDrawing = true;
m_ptStart = point;
m_ptOld = point;
if(m_nDrawType == 3)
{
CBrush * pOldBrush;
CBitmap * pOldBitmap;
CBrush brFill;
brFill.CreateSolidBrush(m_cFillColor);
pOldBrush = m_pMemDC->SelectObject(&brFill);
pOldBitmap = m_pMemDC->SelectObject(m_pBitmap);
m_pMemDC->ExtFloodFill(point.x,point.y, m_pMemDC->GetPixel(point), FLOODFILLSURFACE);
Invalidate(false);
m_pMemDC->SelectObject(pOldBrush);
m_pMemDC->SelectObject(pOldBitmap);
m_bDrawing = false;
}
CView::OnLButtonDown(nFlags, point);
}
void CDrawView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_bDrawing)
{
CDC * pDC = GetDC();
CBitmap * pOldBitmap = m_pMemDC->SelectObject(m_pBitmap);
CPen pen;
pen.CreatePen(PS_SOLID, m_nLineWidth, m_cLineColor);
CPen * pOldPen = pDC->SelectObject(&pen);
CBrush * pOldBrush = (CBrush *) pDC->SelectStockObject(NULL_BRUSH);
CRect rectOld(m_ptStart, m_ptOld);
rectOld.NormalizeRect();
rectOld.InflateRect(m_nLineWidth, m_nLineWidth);
pDC->BitBlt(rectOld.left, rectOld.top, rectOld.Width(), rectOld.Height(),m_pMemDC, rectOld.left, rectOld.top, SRCCOPY);
CRect rectNew(m_ptStart,point);
switch(m_nDrawType)
{
case 0:
pDC->MoveTo(m_ptStart);
pDC->LineTo(point);
break;
case 1:
pDC->Rectangle(rectNew);
break;
case 2:
pDC->Ellipse(rectNew);
break;
default:
break;
}
m_pMemDC->SelectObject(pOldBitmap);
pDC->SelectObject(pOldPen);
pDC->SelectObject(pOldBrush);
ReleaseDC(pDC);
m_ptOld = point;
}
CView::OnMouseMove(nFlags, point);
}
void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_bDrawing)
{
m_bDrawing = false;
CBitmap * pOldBitmap = m_pMemDC->SelectObject(m_pBitmap);
CPen pen;
pen.CreatePen(PS_SOLID, m_nLineWidth, m_cLineColor);
CPen * pOldPen = m_pMemDC->SelectObject(&pen);
CBrush * pOldBrush = (CBrush *) m_pMemDC->SelectStockObject(NULL_BRUSH);
CRect rectNew(m_ptStart,point);
switch(m_nDrawType)
{
case 0:
m_pMemDC->MoveTo(m_ptStart);
m_pMemDC->LineTo(point);
break;
case 1:
m_pMemDC->Rectangle(rectNew);
break;
case 2:
m_pMemDC->Ellipse(rectNew);
break;
default:
break;
}
Invalidate(false);
m_pMemDC->SelectObject(pOldBitmap);
m_pMemDC->SelectObject(pOldPen);
m_pMemDC->SelectObject(pOldBrush);
}
ReleaseCapture();
CView::OnLButtonUp(nFlags, point);
}
BOOL CDrawView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
// TODO: Add your message handler code here and/or call default
switch(m_nDrawType)
{
case 0:
case 1:
case 2:
::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_CROSS));
break;
case 3:
::SetCursor(AfxGetApp()->LoadCursor(ID_CURSOR_FILL));
break;
default:
::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
break;
}
return CView::OnSetCursor(pWnd, nHitTest, message);
}
好久以前的了,要的话留下邮箱,我发给你~本回答被提问者采纳