⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sdocdemoview.cpp

📁 VC++6开发指南的源代码第7章-第11章
💻 CPP
字号:
// SDocDemoView.cpp : implementation of the CSDocDemoView class
//

#include "stdafx.h"
#include "SDocDemo.h"

#include "SDocDemoDoc.h"
#include "SDocDemoView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSDocDemoView

IMPLEMENT_DYNCREATE(CSDocDemoView, CScrollView)

BEGIN_MESSAGE_MAP(CSDocDemoView, CScrollView)
	//{{AFX_MSG_MAP(CSDocDemoView)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CSDocDemoView construction/destruction

CSDocDemoView::CSDocDemoView()
{
	// TODO: add construction code here
	m_bDraw=false;
	m_Hcursor=AfxGetApp()->LoadStandardCursor(IDC_CROSS);//载入十字光标

}

CSDocDemoView::~CSDocDemoView()
{
}

BOOL CSDocDemoView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	cs.lpszClass=AfxRegisterWndClass( CS_VREDRAW | CS_HREDRAW,
      ::LoadCursor(NULL, IDC_ARROW),(HBRUSH)::GetStockObject(LTGRAY_BRUSH),0);//设置窗口背景为浅灰色
	return CScrollView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CSDocDemoView drawing

void CSDocDemoView::OnDraw(CDC* pDC)
{
	CSDocDemoDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	int index;
	index=pDoc->GetTotalLine();//获取线条数目
	while(index--)
	{
		pDoc->GetLine(index)->DrawLine(pDC);//绘制线条
	}
}

/////////////////////////////////////////////////////////////////////////////
// CSDocDemoView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CSDocDemoView diagnostics

#ifdef _DEBUG
void CSDocDemoView::AssertValid() const
{
	CScrollView::AssertValid();
}

void CSDocDemoView::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CSDocDemoView message handlers

void CSDocDemoView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CClientDC dc(this);
	OnPrepareDC(&dc);
	dc.DPtoLP(&point);//屏幕坐标转换为逻辑坐标
	SetCursor(m_Hcursor);									//使用新光标
	m_bDraw=true;//进入绘图状态
	m_pOld=point;
	SetCapture();//捕捉鼠标
	CRect rect;
	GetClientRect(&rect);//获取客户窗口矩形区域
	ClientToScreen(&rect);//转换为屏幕坐标
	ClipCursor(rect);//限定鼠标不能移出客户窗口
	CScrollView::OnLButtonDown(nFlags, point);
}

void CSDocDemoView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CClientDC dc(this);
	OnPrepareDC(&dc);
	dc.DPtoLP(&point);//屏幕坐标转换为逻辑坐标
	m_bDraw=false;//取消绘图状态
	ReleaseCapture();//释放鼠标捕捉
	ClipCursor(NULL);//取消鼠标区域的限制
	CScrollView::OnLButtonUp(nFlags, point);
}

void CSDocDemoView::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default	
	if(m_bDraw)
	{
		CClientDC dc(this);
		OnPrepareDC(&dc);
		dc.DPtoLP(&point);//屏幕坐标转换为逻辑坐标
		CSDocDemoDoc *pDoc=GetDocument();//获取文档指针
		dc.MoveTo(m_pOld);
		dc.LineTo(point);
		pDoc->AddLine(m_pOld.x,m_pOld.y,point.x,point.y);//存储线条
		m_pOld=point;	
	}	
	CScrollView::OnMouseMove(nFlags, point);
}

void CSDocDemoView::OnInitialUpdate()
{
	SIZE size={2800,1600};
	SetScrollSizes(MM_TEXT,size);//滚动窗口的最大区域
	CScrollView::OnInitialUpdate();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -