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

📄 ch10demo3view.cpp

📁 零基础学Visual.C....教案PPT.随书光盘-452M.zip
💻 CPP
字号:
// Ch10Demo3View.cpp : implementation of the CCh10Demo3View class
//

#include "stdafx.h"
#include "Ch10Demo3.h"

#include "Ch10Demo3Doc.h"
#include "Ch10Demo3View.h"

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

/////////////////////////////////////////////////////////////////////////////
// CCh10Demo3View

IMPLEMENT_DYNCREATE(CCh10Demo3View, CScrollView)

BEGIN_MESSAGE_MAP(CCh10Demo3View, CScrollView)
	//{{AFX_MSG_MAP(CCh10Demo3View)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_COMMAND(ID_DRAWLINE, OnDrawline)
	ON_UPDATE_COMMAND_UI(ID_DRAWLINE, OnUpdateDrawline)
	ON_COMMAND(ID_DRAWRECT, OnDrawrect)
	ON_UPDATE_COMMAND_UI(ID_DRAWRECT, OnUpdateDrawrect)
	ON_COMMAND(ID_DRAWCIRCLE, OnDrawcircle)
	ON_UPDATE_COMMAND_UI(ID_DRAWCIRCLE, OnUpdateDrawcircle)
	ON_COMMAND(ID_CANCEL, OnCancel)
	ON_COMMAND(ID_CLEAR, OnClear)
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CCh10Demo3View construction/destruction

CCh10Demo3View::CCh10Demo3View()
{
	// TODO: add construction code here
	bMove=false;
	bDown=false;

}

CCh10Demo3View::~CCh10Demo3View()
{
}

BOOL CCh10Demo3View::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CScrollView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CCh10Demo3View drawing

void CCh10Demo3View::OnDraw(CDC* pDC)
{
	CCh10Demo3Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	CBrush *poldBrush;
	poldBrush=(CBrush *)pDC->SelectStockObject(NULL_BRUSH);     //选择库存空画刷
	// TODO: add draw code for native data here
	if(!pDoc->m_graphlist.IsEmpty())									//链表不为空
	{
		POSITION pos = pDoc->m_graphlist.GetHeadPosition();					//获取链表头的位置
		for (int i=0;i<pDoc->m_graphlist.GetCount();i++)						//遍历链表
		{
			
			CGraph* m_pGraph= (CGraph*)pDoc->m_graphlist.GetNext(pos);
			if(m_pGraph->m_type==0)//如果图形数据为直线
			{
				pDC->MoveTo(m_pGraph->m_star);//绘制直线
				pDC->LineTo(m_pGraph->m_end);
			}
			else if(m_pGraph->m_type==1)//如果图形数据为矩形
			{
				//绘制矩形
				pDC->Rectangle(m_pGraph->m_star.x,m_pGraph->m_star.y,m_pGraph->m_end.x,m_pGraph->m_end.y);
			}
			else if(m_pGraph->m_type==2)//如果图形数据为椭圆
			{
				//绘制椭圆
				pDC->Ellipse(m_pGraph->m_star.x,m_pGraph->m_star.y,m_pGraph->m_end.x,m_pGraph->m_end.y);
			}
		}
	}
	if(bMove)//绘图过程中
	{
		if(m_drawtype==0)//正在绘制直线
		{
			pDC->MoveTo(ptstar);
			pDC->LineTo(ptend);
		}
		else if(m_drawtype==1)//正在绘制矩形
		{
			pDC->Rectangle(ptstar.x,ptstar.y,ptend.x,ptend.y);
		}
		else if(m_drawtype==2)//正在绘制椭圆
		{
			pDC->Ellipse(ptstar.x,ptstar.y,ptend.x,ptend.y);
		}
	}
	pDC->SelectObject(poldBrush); //画刷恢复原值
}

/////////////////////////////////////////////////////////////////////////////
// CCh10Demo3View printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CCh10Demo3View diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CCh10Demo3View message handlers

void CCh10Demo3View::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CClientDC dc(this);
	OnPrepareDC(&dc);
	dc.DPtoLP(&point);									//屏幕坐标转换为逻辑坐标
	bDown=true;//表明按下了鼠标左键
	ptstar=point;//记录绘图的起点
	CScrollView::OnLButtonDown(nFlags, point);
}

void CCh10Demo3View::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CClientDC dc(this);
	OnPrepareDC(&dc);
	dc.DPtoLP(&point);									//屏幕坐标转换为逻辑坐标
	bMove=false;
	bDown=false;
	CCh10Demo3Doc* pDoc = GetDocument();//获取文档指针
	ASSERT_VALID(pDoc);
	ptend=point;//记录鼠标释放位置,即图形对象的终点
	CGraph* m_graph = new CGraph;//创建结构体对象指针
	//为结构体赋值
	if((m_drawtype==0)|(m_drawtype==1)|(m_drawtype==2))//正在绘图
	{
	m_graph->m_star=ptstar;
	m_graph->m_end=ptend;
	m_graph->m_type=m_drawtype;
	pDoc->m_graphlist.AddTail(m_graph);//添加到文档链表中
	Invalidate(true);//更新视图,调用OnDraw
	pDoc->SetModifiedFlag();
	pDoc->UpdateAllViews(NULL);							//更新所有视图
	}
	CScrollView::OnLButtonUp(nFlags, point);
}

void CCh10Demo3View::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CClientDC dc(this);//获取客户窗口DC
	OnPrepareDC(&dc);//进行坐标原点的匹配
	dc.DPtoLP(&point);	//将视图坐标转换为文档坐标								//屏幕坐标转换为逻辑坐标
	if(bDown)//如果鼠标左键按下了
	{
		ptend=point;//绘图终点
		bMove=true;
		Invalidate(true);//重绘客户区
	
	}
	CScrollView::OnMouseMove(nFlags, point);
}

void CCh10Demo3View::OnDrawline() 
{
	// TODO: Add your command handler code here
	m_drawtype=0;	//设置绘图类型
}

void CCh10Demo3View::OnUpdateDrawline(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_drawtype==0);			//若为直线,设置选中标记	
}

void CCh10Demo3View::OnDrawrect() 
{
	// TODO: Add your command handler code here
	m_drawtype=1;	//设置绘图类型
}

void CCh10Demo3View::OnUpdateDrawrect(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_drawtype==1);			//若为矩形,设置选中标记	
}

void CCh10Demo3View::OnDrawcircle() 
{
	// TODO: Add your command handler code here
	m_drawtype=2;//设置绘图类型
	
}

void CCh10Demo3View::OnUpdateDrawcircle(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_drawtype==2);			//若为圆形,设置选中标记	
}

void CCh10Demo3View::OnCancel() 
{
	// TODO: Add your command handler code here
	CCh10Demo3Doc* pDoc = GetDocument();//获取文档指针
	ASSERT_VALID(pDoc);
	if(!pDoc->m_graphlist.IsEmpty())									//链表不为空
	pDoc->m_graphlist.RemoveTail();//去除链表尾
	Invalidate(true);//更新视图,调用OnDraw
	pDoc->SetModifiedFlag();//设置修改标记
	pDoc->UpdateAllViews(NULL);							//更新所有视图

	
}

void CCh10Demo3View::OnClear() 
{
	// TODO: Add your command handler code here
	CCh10Demo3Doc* pDoc = GetDocument();//获取文档指针
	ASSERT_VALID(pDoc);
	if(!pDoc->m_graphlist.IsEmpty())									//链表不为空
	pDoc->m_graphlist.RemoveAll();//清空链表
	Invalidate(true);//更新视图,调用OnDraw
	pDoc->SetModifiedFlag();//设置修改标记
	pDoc->UpdateAllViews(NULL);							//更新所有视图
	
}

void CCh10Demo3View::OnInitialUpdate() 
{
	CScrollView::OnInitialUpdate();	
	// TODO: Add your specialized code here and/or call the base class
	SIZE size={3000,1500};
	SetScrollSizes(MM_TEXT,size);								//滚动窗口的最大区域
}

⌨️ 快捷键说明

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