www.pudn.com > MyCAD.rar > MyCADView.cpp


// MyCADView.cpp : implementation of the CMyCADView class 
// 
 
#include "stdafx.h" 
#include "MyCAD.h" 
 
#include "MyCADDoc.h" 
#include "MyCADView.h" 
#include "resource.h" 
 
#include "LineWidthDlg.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CMyCADView 
 
IMPLEMENT_DYNCREATE(CMyCADView, CView) 
 
BEGIN_MESSAGE_MAP(CMyCADView, CView) 
	ON_WM_CONTEXTMENU() 
	//{{AFX_MSG_MAP(CMyCADView) 
	ON_COMMAND(ID_GRAPH_POINT, OnGraphPoint) 
	ON_UPDATE_COMMAND_UI(ID_GRAPH_POINT, OnUpdateGraphPoint) 
	ON_WM_LBUTTONDOWN() 
	ON_WM_SETCURSOR() 
	ON_WM_KEYDOWN() 
	ON_WM_MOUSEMOVE() 
	ON_COMMAND(ID_SET_LINEWIDTH, OnSetLinewidth) 
	ON_COMMAND(ID_SET_COLOR, OnSetColor) 
	//}}AFX_MSG_MAP 
	// 为画线菜单项手动添加消息映射 
	ON_COMMAND(ID_GRAPH_LINE, OnGraphLine) 
	ON_UPDATE_COMMAND_UI(ID_GRAPH_LINE, OnUpdateGraphLine) 
	// 为自定义消息建立消息映射宏 
	ON_MESSAGE(WM_MY_MESSAGE, OnMyMessage) 
	// 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() 
 
///////////////////////////////////////////////////////////////////////////// 
// CMyCADView construction/destruction 
 
CMyCADView::CMyCADView() 
{ 
	// TODO: add construction code here 
	this->m_bIsPoint = FALSE; 
	this->m_bIsLine = FALSE; 
	this->m_nStartX = 0; 
	this->m_nStartY = 0; 
	this->m_nStep = 0; 
	this->m_nLineWidth = 1; 
	this->m_nLineStyle = PS_SOLID; 
	this->m_LineColor = RGB(0,0,0); 
} 
 
CMyCADView::~CMyCADView() 
{ 
} 
 
BOOL CMyCADView::PreCreateWindow(CREATESTRUCT& cs) 
{ 
	// TODO: Modify the Window class or styles here by modifying 
	//  the CREATESTRUCT cs 
 
	return CView::PreCreateWindow(cs); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CMyCADView drawing 
 
void CMyCADView::OnDraw(CDC* pDC) 
{ 
	CMyCADDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc); 
	// TODO: add draw code for native data here 
	//重新绘制所有点 
	for(int i=0;im_nPointNum;i++) 
	{ 
		int x = pDoc->m_Point[i][0]; 
		int y = pDoc->m_Point[i][1]; 
		 
		pDC->Ellipse(x-2, y-2, x+2, y+2); 
	} 
 
	////////////////////////////////////////////// 
	//重新绘制所有线段 
	for( i=0;im_nLineNum;i++) 
	{ 
		int x1 = pDoc->m_Line[i][0]; 
		int y1 = pDoc->m_Line[i][1]; 
		int x2 = pDoc->m_Line[i][2]; 
		int y2 = pDoc->m_Line[i][3]; 
		 
		pDC->MoveTo(x1, y1); 
		pDC->LineTo(x2, y2); 
	} 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CMyCADView printing 
 
BOOL CMyCADView::OnPreparePrinting(CPrintInfo* pInfo) 
{ 
	// default preparation 
	return DoPreparePrinting(pInfo); 
} 
 
void CMyCADView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add extra initialization before printing 
} 
 
void CMyCADView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add cleanup after printing 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CMyCADView diagnostics 
 
#ifdef _DEBUG 
void CMyCADView::AssertValid() const 
{ 
	CView::AssertValid(); 
} 
 
void CMyCADView::Dump(CDumpContext& dc) const 
{ 
	CView::Dump(dc); 
} 
 
CMyCADDoc* CMyCADView::GetDocument() // non-debug version is inline 
{ 
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyCADDoc))); 
	return (CMyCADDoc*)m_pDocument; 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CMyCADView message handlers 
 
void CMyCADView::OnGraphPoint()  
{ 
	// TODO: Add your command handler code here 
	CClientDC dc(this); 
	dc.TextOut(100,100,"画点");	 
	this->m_bIsPoint = TRUE; 
	this->m_bIsLine = FALSE; 
 
	// 发送自定义消息 
	this->SendMessage(WM_MY_MESSAGE); 
} 
 
void CMyCADView::OnUpdateGraphPoint(CCmdUI* pCmdUI)  
{ 
	// TODO: Add your command update UI handler code here 
	pCmdUI->SetRadio(this->m_bIsPoint); 
} 
 
// 手动添加的消息映射函数 
void CMyCADView::OnGraphLine() 
{ 
	// 获取视图窗口的DC 
	CClientDC dc(this); 
	// 在视图区输出文本 
	dc.TextOut(200,100,"画线"); 
	this->m_bIsLine = TRUE; 
	this->m_bIsPoint = FALSE; 
} 
 
void CMyCADView::OnUpdateGraphLine(CCmdUI* pCmdUI)  
{ 
	// TODO: Add your command update UI handler code here 
	pCmdUI->SetRadio(this->m_bIsLine); 
} 
 
void CMyCADView::OnContextMenu(CWnd*, CPoint point) 
{ 

	// CG: This block was added by the Pop-up Menu component
	{
		if (point.x == -1 && point.y == -1){
			//keystroke invocation
			CRect rect;
			GetClientRect(rect);
			ClientToScreen(rect);

			point = rect.TopLeft();
			point.Offset(5, 5);
		}

		CMenu menu;
		VERIFY(menu.LoadMenu(CG_IDR_POPUP_MY_CADVIEW));

		CMenu* pPopup = menu.GetSubMenu(0);
		ASSERT(pPopup != NULL);
		CWnd* pWndPopupOwner = this;

		while (pWndPopupOwner->GetStyle() & WS_CHILD)
			pWndPopupOwner = pWndPopupOwner->GetParent();

		pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y,
			pWndPopupOwner);
	} 
} 
 
// 自定义消息的处理函数 
LRESULT CMyCADView::OnMyMessage(WPARAM wParam, LPARAM lParam) 
{ 
	CClientDC dc(this); 
	dc.TextOut(200,200,"测试自定义消息"); 
 
	return 1; 
} 
void CMyCADView::OnLButtonDown(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
	CMyCADDoc* pDoc = GetDocument();  // 获取文档指针 
	CClientDC dc(this); 
	//dc.TextOut(100,100,"鼠标左键按下"); 
	 
	// 绘图之前创建一支画笔 
	CPen newPen(m_nLineStyle,m_nLineWidth,m_LineColor); 
	CPen * poldPen = dc.SelectObject(&newPen); 
 
	if(m_bIsPoint) 
	{ 
		dc.Ellipse(point.x - 2,point.y - 2,point.x + 2,point.y + 2); 
		//修改文档中的记录点的数据成员 
		int i = pDoc->m_nPointNum; 
		pDoc->m_Point[i][0] = point.x;  //记录点的坐标值 
		pDoc->m_Point[i][1] = point.y; 
 
		pDoc->m_nPointNum ++;         //点数加1 
 
		pDoc->SetModifiedFlag();    //设置文档修改标志 
	} 
	if(m_bIsLine) 
	{ 
		if(m_nStep == 0) 
		{ 
			this->m_nStartX = point.x; 
			this->m_nStartY = point.y; 
			this->m_nStep = 1; 
		} 
		else 
		{ 
			dc.MoveTo(this->m_nStartX,this->m_nStartY); 
			dc.LineTo(point.x,point.y); 
			//修改文档中的记录点的数据成员 
			int i = pDoc->m_nLineNum; 
			pDoc->m_Line[i][0] = m_nStartX;  //记录线段起点坐标 
			pDoc->m_Line[i][1] = m_nStartY;   
			pDoc->m_Line[i][2] = point.x;  //记录线段终点坐标 
			pDoc->m_Line[i][3] = point.y;   
 
			pDoc->m_nLineNum ++;         //线段总数加1 
		 
			pDoc->SetModifiedFlag();    //设置文档修改标志 
 
			this->m_nStep = 0; 
			this->m_nStartX = 0; 
			this->m_nStartY = 0; 
		} 
	} 
	//dc.SelectObject(poldPen); // 绘图完毕后选回原来的画笔 
	CView::OnLButtonDown(nFlags, point); 
} 
 
BOOL CMyCADView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)  
{ 
	// TODO: Add your message handler code here and/or call default 
	if(m_bIsPoint) 
	{ 
		HCURSOR hCursor = AfxGetApp()->LoadCursor(IDC_POINTER); 
		SetCursor(hCursor); 
		return TRUE; //直接返回,不调用CView::OnSetCursor 
	} 
	if(m_bIsLine) 
	{ 
		HCURSOR hCursor = AfxGetApp()->LoadStandardCursor(IDC_CROSS); 
		SetCursor(hCursor); 
		return TRUE; 
	} 
	return CView::OnSetCursor(pWnd, nHitTest, message); 
} 
 
void CMyCADView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)  
{ 
	// TODO: Add your message handler code here and/or call default 
	if(nChar == VK_ESCAPE) 
	{ 
		// 恢复成员变量的厨师状态 
		this->m_bIsPoint = FALSE; 
		this->m_bIsLine = FALSE; 
		this->m_nStep = 0; 
		this->m_nStartX = 0; 
		this->m_nStartY = 0; 
	} 
	CView::OnKeyDown(nChar, nRepCnt, nFlags); 
} 
 
void CMyCADView::OnMouseMove(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
	CString str; 
	CStatusBar * pStatus; 
	pStatus = (CStatusBar *)AfxGetApp()->m_pMainWnd->GetDescendantWindow(AFX_IDW_STATUS_BAR); 
	if(pStatus) 
	{ 
		str.Format("x = %3d, y = %3d", point.x, point.y); 
		pStatus->SetPaneText(1,str); 
	} 
	CView::OnMouseMove(nFlags, point); 
} 
 
void CMyCADView::OnSetLinewidth()  
{ 
	// TODO: Add your command handler code here 
	CLineWidthDlg dlg; 
	dlg.m_nLineWidth = this->m_nLineWidth; 
	if(dlg.DoModal() == IDOK) 
	{ 
		this->m_nLineWidth = dlg.m_nLineWidth; 
	} 
} 
 
void CMyCADView::OnSetColor()  
{ 
	// TODO: Add your command handler code here 
	CColorDialog dlg(m_LineColor); 
	if(dlg.DoModal() == IDOK) 
	{ 
		m_LineColor = dlg.GetColor(); 
	} 
}