最近刚开始学习Visual c++6.0
还是菜鸟级别
单击左键,画点
按住左键拖动,画线
单击右键,清除屏幕
改变画框大小,画面重画
// MyProg2bView.cpp : implementation of the CMyProg2bView class
//
#include "stdafx.h"
#include "MyProg2b.h"
#include "MyProg2bDoc.h"
#include "MyProg2bView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMyProg2bView
IMPLEMENT_DYNCREATE(CMyProg2bView, CView)
BEGIN_MESSAGE_MAP(CMyProg2bView, CView)
//{{AFX_MSG_MAP(CMyProg2bView)
ON_WM_LBUTTONDOWN()
ON_WM_RBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_WM_ERASEBKGND()
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CMyProg2bView construction/destruction
CMyProg2bView::CMyProg2bView()
{
// TODO: add construction code here
m_MousePositions.SetSize(0,100);
m_ButtonDown.SetSize(0,100); //用来记录按钮的状态信息
m_nPrevIndex=0;
}
CMyProg2bView::~CMyProg2bView()
{
ClearList();
}
BOOL CMyProg2bView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMyProg2bView drawing
void CMyProg2bView::OnDraw(CDC* pDC)
{
CMyProg2bDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
int nPositions=m_MousePositions.GetSize();
if(m_nPrevIndex>=nPositions)
{
return;
}
CPoint *pPoint;
int i=0;
pPoint =(CPoint *)m_MousePositions[m_nPrevIndex];
pDC->MoveTo(* pPoint);
for(i=m_nPrevIndex;i
pPoint=(CPoint *)m_MousePositions[i];
if(m_ButtonDown[i])
{
pDC->MoveTo(*pPoint);
pDC->SetPixel(*pPoint,RGB(0,0,0));
}
else
{
pDC->LineTo(* pPoint);
}
}
m_nPrevIndex=nPositions-1;
}
(增加变量m_nPrevIndex来保存最新画过的鼠标位置在表中的下标,画完之后,更新该成员保存的下标值以提示最新画过的鼠标的位置)
/////////////////////////////////////////////////////////////////////////////
// CMyProg2bView printing
BOOL CMyProg2bView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMyProg2bView::OnBeginPrinting(CDC* , CPrintInfo* )
{
// TODO: add extra initialization before printing
}
void CMyProg2bView::OnEndPrinting(CDC* , CPrintInfo* )
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMyProg2bView diagnostics
#ifdef _DEBUG
void CMyProg2bView::AssertValid() const
{
CView::AssertValid();
}
void CMyProg2bView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMyProg2bDoc* CMyProg2bView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyProg2bDoc)));
return (CMyProg2bDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMyProg2bView message handlers
void CMyProg2bView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CView::OnLButtonDown(nFlags, point);
AddList(TRUE,point);
Invalidate();
}
void CMyProg2bView::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CView::OnRButtonDown(nFlags, point);
ClearList();
Invalidate();
}
void CMyProg2bView::ClearList()
{
int i;
CPoint *pPoint;
for(i=0;i
pPoint=(CPoint *)m_MousePositions[i];
delete pPoint;
}
m_MousePositions.RemoveAll();
m_ButtonDown.RemoveAll();
}
//每个CPoint对象都是由一个new操作符从堆中分配内存的,所以与之对应,每个CPoint对象也要由一个delete来释放。m_MousePosition中所有指针指向的对象都已释放之后,调用RemoveAll()将用到的数组置空。
void CMyProg2bView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CView::OnMouseMove(nFlags, point);
if(nFlags&MK_LBUTTON)
{
AddList(FALSE,point);
Invalidate(FALSE);
}
}
void CMyProg2bView::AddList(int button, CPoint point)
{
CPoint * pnewPoint=new CPoint(point);
m_MousePositions.Add((void *)pnewPoint);
m_ButtonDown.Add(button);
}
BOOL CMyProg2bView::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
m_nPrevIndex=0;
return CView::OnEraseBkgnd(pDC);
}
(无论何时客户区被擦除,就发送一个名为WM_REASEBKGND的消息,WM_LBUTTONDOWN和WM_MOUSEMOVE消息引起的扥更新并不擦除背景(若要跳过擦屏,调用Invalidate()时就要以FALSE作参数).一收到WM_REASEBKGND消息,就知道必须用保存的信息重画整个窗口)