【VC开源代码栏目提醒】:网学会员在VC开源代码频道为大家收集整理了Ex07_2View.cpp提供大家参考,希望对大家有所帮助!
// Ex07_2View.cpp : implementation of the CEx07_2View class
//
#include "stdafx.h"
#include "Ex07_2.h"
#include "Ex07_2Doc.h"
#include "Ex07_2View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CEx07_2View
IMPLEMENT_DYNCREATE(CEx07_2View, CScrollView)
BEGIN_MESSAGE_MAP(CEx07_2View, CScrollView)
//{{AFX_MSG_MAP(CEx07_2View)
ON_WM_LBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONUP()
ON_WM_CHAR()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEx07_2View construction/destruction
CEx07_2View::CEx07_2View()
{
// TODO: add construction code here
m_DrawType=Line;
m_bMouseDown=false;
m_hCursor=AfxGetApp()->LoadStandardCursor(IDC_CROSS);
m_nLineNo=0;
}
CEx07_2View::~CEx07_2View()
{
}
BOOL CEx07_2View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CScrollView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CEx07_2View drawing
void CEx07_2View::OnDraw(CDC* pDC)
{
CEx07_2Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
void CEx07_2View::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
CSize sizeTotal;
// TODO: calculate the total size of this view
sizeTotal.cx = sizeTotal.cy = 100;
SetScrollSizes(MM_TEXT, sizeTotal);
}
/////////////////////////////////////////////////////////////////////////////
// CEx07_2View printing
BOOL CEx07_2View::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CEx07_2View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CEx07_2View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CEx07_2View diagnostics
#ifdef _DEBUG
void CEx07_2View::AssertValid() const
{
CScrollView::AssertValid();
}
void CEx07_2View::Dump(CDumpContext& dc) const
{
CScrollView::Dump(dc);
}
CEx07_2Doc* CEx07_2View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CEx07_2Doc)));
return (CEx07_2Doc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CEx07_2View message handlers
///鼠标消息函数
void CEx07_2View::OnLButtonDown(UINT nFlags, CPoint point) //鼠标按下
{
// TODO: Add your message handler code here and/or call default
m_bMouseDown=true;
m_StartPt=point;//存放画线的起始位置
m_OldPt=point;//存放光标的当前位置
SetCapture();//捕获鼠标
CRect rect;
GetClientRect(&rect);//获取视图窗口客户区的坐标
ClientToScreen(&rect);//转换客户区坐标为屏幕坐标
ClipCursor(&rect);//将光标限定在视图窗口客户区内
CScrollView::OnLButtonDown(nFlags, point);
}
void CEx07_2View::OnMouseMove(UINT nFlags, CPoint point) //鼠标移动
{
// TODO: Add your message handler code here and/or call default
SetCursor(m_hCursor);//设置鼠标形状为十字形
if(m_bMouseDown)
{
CClientDC dc(this);
dc.SetROP2(R2_NOT);
//以下两行代码擦除从起点(鼠标按下点)到赏赐鼠标移动到的位置之间的临时线
dc.MoveTo(m_StartPt);
dc.LineTo(m_OldPt);
// //以下两行代码玛从起点到鼠标当前位置画线
dc.MoveTo(m_StartPt);
dc.LineTo(point);
m_OldPt=point;//鼠标当前位置为下一次移动的旧位置
}
CScrollView::OnMouseMove(nFlags, point);
}
void CEx07_2View::OnLButtonUp(UINT nFlags, CPoint point) //释放鼠标
{
// TODO: Add your message handler code here and/or call default
m_bMouseDown=false;
ReleaseCapture();//释放鼠标捕获并恢复正常输入
ClipCursor(NULL);//使光标可以移动到屏幕任意位置
CClientDC dc(this);
dc.SetROP2(R