www.pudn.com > 简单的绘工具.rar > MyPaintView.cpp


// MyPaintView.cpp : implementation of the CMyPaintView class 
// 
 
#include "stdafx.h" 
#include "MyPaint.h" 
 
#include "MyPaintDoc.h" 
#include "MyPaintView.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CMyPaintView 
 
IMPLEMENT_DYNCREATE(CMyPaintView, CScrollView) 
 
BEGIN_MESSAGE_MAP(CMyPaintView, CScrollView) 
	//{{AFX_MSG_MAP(CMyPaintView) 
	ON_WM_LBUTTONDOWN() 
	ON_WM_MOUSEMOVE() 
	ON_WM_RBUTTONDOWN() 
	ON_COMMAND(ID_OPTION_PICK, OnOptionPick) 
	ON_UPDATE_COMMAND_UI(ID_OPTION_PICK, OnUpdateOptionPick) 
	//}}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) 
 
	ON_COMMAND_RANGE(ID_CREATE_LINE, ID_CREATE_ARC, OnCreateEntity) 
	ON_UPDATE_COMMAND_UI_RANGE(ID_CREATE_LINE, ID_CREATE_ARC, OnUpdateCreateCommand) 
 
	ON_COMMAND_RANGE(ID_OPTION_MOVE, ID_OPTION_MIRROR, OnModifyEntity) 
	ON_UPDATE_COMMAND_UI_RANGE(ID_OPTION_MOVE, ID_OPTION_MIRROR, OnUpdateModifyCommand) 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CMyPaintView construction/destruction 
 
CMyPaintView::CMyPaintView() 
{ 
	//  
	m_pCmd = NULL; 
	g_pView = this; 
 
	//  
	m_dOrgX = m_dOrgY = 0.; 
	scale = 1.; 
 
} 
 
CMyPaintView::~CMyPaintView() 
{ 
} 
 
BOOL CMyPaintView::PreCreateWindow(CREATESTRUCT& cs) 
{ 
	// TODO: Modify the Window class or styles here by modifying 
	//  the CREATESTRUCT cs 
 
	return CScrollView::PreCreateWindow(cs); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CMyPaintView drawing 
 
void CMyPaintView::OnDraw(CDC* pDC) 
{ 
	g_nRefresh ++; // 刷新次数加1 
 
	CMyPaintDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc); 
 
	pDoc->Draw(pDC); 
} 
 
void CMyPaintView::OnInitialUpdate() 
{ 
	CScrollView::OnInitialUpdate(); 
 
	CSize sizeTotal; 
	// TODO: calculate the total size of this view 
	sizeTotal.cx = 800; 
	sizeTotal.cy = 600; 
	SetScrollSizes(MM_TEXT, sizeTotal); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CMyPaintView printing 
 
BOOL CMyPaintView::OnPreparePrinting(CPrintInfo* pInfo) 
{ 
	// default preparation 
	return DoPreparePrinting(pInfo); 
} 
 
void CMyPaintView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add extra initialization before printing 
} 
 
void CMyPaintView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add cleanup after printing 
} 
 
 
void CMyPaintView::WorldtoScreen(const Position& pos, CPoint& screenPt) 
{ 
	// 获取当前客户区的大小 
	CRect rect ; 
	GetClientRect(&rect) ; 
 
	// 将屏幕原点设置为客户区的中心 
	int	nSOrgX = (rect.left + rect.right)  / 2 ; 
	int nSOrgY = -(rect.top + rect.bottom) / 2 ; 
 
	// 计算屏幕坐标值 
	screenPt.x = (int)((pos.x - m_dOrgX) / scale + nSOrgX) ; 
	screenPt.y = (int)((pos.y - m_dOrgY) / scale + nSOrgY) ; 
} 
 
void CMyPaintView::ScreentoWorld(const CPoint& pt, Position& pos) 
{ 
	CPoint screenPt = pt; 
	CDC* pDC = GetDC(); 
	pDC->SetMapMode(MM_LOENGLISH);  
	pDC->DPtoLP(&screenPt); 
	ReleaseDC(pDC);	 
 
	// 获取当前客户区的大小 
	CRect rect ; 
	GetClientRect(&rect) ; 
 
	// 将屏幕原点设置为客户区的中心 
	int	nSOrgX = (rect.left + rect.right)  / 2 ; 
	int nSOrgY = -(rect.top + rect.bottom) / 2 ; 
 
	// 计算世界坐标值 
	pos.x = (screenPt.x - nSOrgX ) * scale + m_dOrgX; 
	pos.y = (screenPt.y - nSOrgY ) * scale + m_dOrgY; 
} 
 
double CMyPaintView::GetScale() 
{ 
	return scale; 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CMyPaintView diagnostics 
 
#ifdef _DEBUG 
void CMyPaintView::AssertValid() const 
{ 
	CScrollView::AssertValid(); 
} 
 
void CMyPaintView::Dump(CDumpContext& dc) const 
{ 
	CScrollView::Dump(dc); 
} 
 
CMyPaintDoc* CMyPaintView::GetDocument() // non-debug version is inline 
{ 
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyPaintDoc))); 
	return (CMyPaintDoc*)m_pDocument; 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CMyPaintView message handlers 
 
 
void CMyPaintView::OnCreateEntity(int m_nID) 
{ 
	if( m_pCmd ){  
		m_pCmd->Cancel(); 
		delete m_pCmd ; 
		m_pCmd = NULL;  
	} 
 
	// 下面根据不同的菜单命令创建不同的命令对象 
	switch(m_nID) 
	{ 
		case ID_CREATE_LINE: // 直线 
		{ 
			m_pCmd = new CCreateLine(); 
			break; 
		} 
		case ID_CREATE_RECTANGLE: // 矩形 
		{ 
			m_pCmd = new CCreateRect(); 
			break; 
		} 
		case ID_CREATE_ARC: // 圆弧 
		{ 
			m_pCmd = new CCreateArc(); 
			break; 
		} 
	} 
} 
 
void CMyPaintView::OnUpdateCreateCommand(CCmdUI* pCmdUI) 
{ 
	int flag = 0 ; 
	switch(pCmdUI->m_nID) 
	{ 
		case ID_CREATE_LINE: 
		{ 
			if( (m_pCmd != NULL ) && (m_pCmd->GetType() == ctCreateLine) ) 
				flag = 1; 
			break; 
		} 
		case ID_CREATE_RECTANGLE: 
		{ 
			if( (m_pCmd != NULL && m_pCmd->GetType() == ctCreateRectangle) ) 
				flag = 1; 
			break; 
		} 
		case ID_CREATE_ARC: 
		{ 
			if( (m_pCmd != NULL && m_pCmd->GetType() == ctCreateArc) ) 
				flag = 1; 
			break; 
		} 
		default: 
			break; 
	} 
	pCmdUI->SetCheck(flag); 
} 
 
void CMyPaintView::OnModifyEntity(int m_nID) 
{ 
	CMyPaintDoc* pDoc = GetDocument(); 
	ASSERT(pDoc) ; 
 
	if( m_pCmd ){  
		m_pCmd->Cancel(); 
		delete m_pCmd ; 
		m_pCmd = NULL;  
	} 
 
	// 下面根据不同的菜单命令创建不同的命令对象 
	switch(m_nID) 
	{ 
		case ID_OPTION_MOVE:	// 平移 
		{ 
			m_pCmd = new CMove(); 
			break; 
		} 
		case ID_OPTION_MIRROR: // 镜像 
		{ 
			m_pCmd = new CRotate(); 
			break; 
		} 
		default: 
			break; 
	} 
} 
 
void CMyPaintView::OnUpdateModifyCommand(CCmdUI* pCmdUI) 
{ 
	CMyPaintDoc* pDoc = GetDocument(); 
	ASSERT(pDoc) ; 
 
	// 判断是否有实体图元被选中 
	if(pDoc->m_selectArray.GetSize() == 0) 
	{ 
		pCmdUI->Enable(FALSE);		 
		return; 
	} 
 
	int flag = 0 ; 
	switch(pCmdUI->m_nID) 
	{ 
		case ID_OPTION_MOVE: 
		{ 
			if( (m_pCmd != NULL && m_pCmd->GetType() == ctMove) ) 
				flag = 1; 
			break; 
		} 
		case ID_OPTION_MIRROR: 
		{ 
			if( (m_pCmd != NULL && m_pCmd->GetType() == ctRotate) ) 
				flag = 1; 
			break; 
		} 
		default: 
			break; 
	} 
	pCmdUI->SetCheck(flag); 
 
} 
 
void CMyPaintView::OnLButtonDown(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
	CMyPaintDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc); 
 
	Position pos; 
	ScreentoWorld(point, pos); // 将设备坐标转换为世界坐标 
 
	if(m_pCmd) 
		m_pCmd->OnLButtonDown(nFlags, pos); 
	else 
		pDoc->OnLButtonDown(nFlags, pos); 
	 
	CView::OnLButtonDown(nFlags, point); 
} 
 
void CMyPaintView::OnMouseMove(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
	CMyPaintDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc); 
 
	Position pos; 
	ScreentoWorld(point, pos);// 将设备坐标转换为世界坐标 
 
	//获得状态条的指针 
	CStatusBar* pStatus=(CStatusBar*) 
		AfxGetApp()->m_pMainWnd->GetDescendantWindow(ID_VIEW_STATUS_BAR); 
	if(pStatus) 
	{ 
		CString str; 
		str.Format("(%d,%d)",point.x, point.y); 
		//在状态条的第二个窗格中输出当前鼠标的位置 
		pStatus->SetPaneText(1,str); 
	} 
	if(m_pCmd) 
		m_pCmd->OnMouseMove(nFlags, pos); 
	else 
		pDoc->OnMouseMove(nFlags, pos); 
 
	CView::OnMouseMove(nFlags, point); 
} 
 
void CMyPaintView::OnRButtonDown(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
	Position pos; 
	ScreentoWorld(point, pos);// 将设备坐标转换为世界坐标 
 
	if(m_pCmd) 
		m_pCmd->OnRButtonDown(nFlags, pos); 
 
	CView::OnRButtonDown(nFlags, point); 
} 
 
void CMyPaintView::OnOptionPick()  
{ 
	if(m_pCmd){ 
		delete m_pCmd; 
		m_pCmd = NULL; 
	} 
	 
} 
 
void CMyPaintView::OnUpdateOptionPick(CCmdUI* pCmdUI)  
{ 
	pCmdUI->SetCheck(m_pCmd == NULL ? 1 : 0) ; 
	 
}