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

📄 2dcadview.cpp

📁 仿位图源程序
💻 CPP
字号:
// 2DCADView.cpp : implementation of the CMy2DCADView class
//

#include "stdafx.h"
#include "2DCAD.h"
#include "MainFrm.h"
#include "2DCADDoc.h"
#include "2DCADView.h"

#include "resource.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include "LineWidthDlg.h"
#define WM_MY_MESSAGE (WM_USER + 101)
/////////////////////////////////////////////////////////////////////////////
// CMy2DCADView

IMPLEMENT_DYNCREATE(CMy2DCADView, CView)

BEGIN_MESSAGE_MAP(CMy2DCADView, CView)
ON_WM_CONTEXTMENU()
//{{AFX_MSG_MAP(CMy2DCADView)
ON_COMMAND(ID_GRAPH_POINT, OnGraphPoint)
ON_UPDATE_COMMAND_UI(ID_GRAPH_POINT, OnUpdateGraphPoint)
ON_WM_LBUTTONDOWN()
ON_WM_KEYDOWN()
ON_WM_MOUSEMOVE()
ON_COMMAND(ID_SET_LINEWIDTH, OnSetLinewidth)
ON_COMMAND(ID_GRAPH_LINE, OnGraphLine)
	ON_COMMAND(ID_SET_LINESTYLE, OnSetLinestyle)
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CMy2DCADView construction/destruction

CMy2DCADView::CMy2DCADView()
{
	m_iPenSlct = 0;
	m_nStep = 0;
	m_nStartX = m_nStartY = 0;
	m_nEndX = m_nEndY = 0;
	m_nLineWidth = 1;
	m_nLineStyle = PS_SOLID;
	
}

CMy2DCADView::~CMy2DCADView()
{
}

BOOL CMy2DCADView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	
	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMy2DCADView drawing

void CMy2DCADView::OnDraw(CDC* pDC)
{
	CMy2DCADDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here

	//重新绘制所有点
	for(int i=0; i<pDoc->m_nPointNum ;i++)
	{
		int x = pDoc->m_Point [i][0];
		int y = pDoc->m_Point [i][1];

		pDC->Ellipse (x-5, y-5, x+5, y+5);
		
	}

	//重新绘制所有线段
	//暂略
	
	
	
}

/////////////////////////////////////////////////////////////////////////////
// CMy2DCADView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CMy2DCADView diagnostics

#ifdef _DEBUG
void CMy2DCADView::AssertValid() const
{
	CView::AssertValid();
}

void CMy2DCADView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CMy2DCADView message handlers

void CMy2DCADView::OnGraphPoint() 
{
	CClientDC dc(this);
	dc.TextOut (100,100,"画点");
	m_iPenSlct = POINT;
	
}

void CMy2DCADView::OnGraphLine() 
{	
	SendMessage(WM_MY_MESSAGE);
	CClientDC dc(this);
	dc.TextOut (100,100,"画线");
	m_iPenSlct = LINE;


}

void CMy2DCADView::OnUpdateGraphPoint(CCmdUI* pCmdUI) 
{
	pCmdUI->SetRadio (m_iPenSlct==POINT);
	
}
void CMy2DCADView::OnUpdateGraphLine(CCmdUI* pCmdUI) 
{
	pCmdUI->SetRadio (m_iPenSlct==LINE);
	
}

LRESULT CMy2DCADView::OnMyMessage(WPARAM wParam, LPARAM lParam) 
{
	CClientDC dc(this);
	dc.TextOut (100,200,"自定义消息");
	m_iPenSlct = USER;
	return 1;
}

void CMy2DCADView::OnContextMenu(CWnd* pWnd, CPoint point)//书上P130,各个函数的意义
{
		// 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(100, 100);		}				CMenu menu;		VERIFY(menu.LoadMenu(CG_IDR_POPUP_MY2_DCADVIEW));				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);	}
}

void CMy2DCADView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CMy2DCADDoc * pDoc = GetDocument();
	CClientDC dc(this);
	
	////////////////////////////
	///		以下代码划点	///
	///////////////////////////
	if(m_iPenSlct == POINT)
	{
		dc.Ellipse (point.x-5, point.y-5, point.x+5, point.y+5);

		//修改文档中的记录点数的数据成员
		pDoc->m_Point[pDoc->m_nPointNum][0] = point.x ;
		pDoc->m_Point[pDoc->m_nPointNum][1] = point.y ;
		
		pDoc->m_nPointNum ++;//点数加1
		pDoc->SetModifiedFlag ();//脏标志
	}
	
	////////////////////////////
	///		以下代码划线	///
	///////////////////////////
	if(m_iPenSlct == LINE)
	{
		CPen newpen(m_nLineStyle,m_nLineWidth,RGB(0,0,0));
		CPen * poldpen = dc.SelectObject (&newpen);
		
		if(m_nStep == 0)
		{
			m_nStartX = point.x ;
			m_nStartY = point.y ;
			m_nStep ++;
			SetCapture();
		}
		else
		{
			dc.MoveTo (m_nStartX,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 ++;

 			m_nStep = 0;
			//	m_nStartX = m_nStartY =0;
			m_nEndX = m_nEndY =0;
			ReleaseCapture();
		}
		dc.SelectObject (poldpen);
		pDoc->SetModifiedFlag ();
		
	}
	
	
	CView::OnLButtonDown(nFlags, point);
}





//键盘响应例子,P185
void CMy2DCADView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	if(nChar == VK_ESCAPE)
	{
		m_iPenSlct = 0;
		m_nStep = 0;
		m_nStartX = m_nStartY =0;
		::ReleaseCapture ();//释放数表捕捉
	}
	
	CView::OnKeyDown(nChar, nRepCnt, nFlags);
}

void CMy2DCADView::OnMouseMove(UINT nFlags, CPoint point) 
{
	
	// TODO: Add your message handler code here and/or call default
	CClientDC dc(this);
	
	if((m_iPenSlct==LINE) && (m_nStep!=0))
	{
		CPen newpen(m_nLineStyle,m_nLineWidth,RGB(0,0,0));//设置线宽,建立画笔
		CPen * poldpen = dc.SelectObject (&newpen);//选择画笔	
		dc.SetROP2 (R2_NOTXORPEN);
		if(m_nEndX!=0)
		{
			dc.MoveTo (m_nStartX,m_nStartY);
			dc.LineTo (m_nEndX,m_nEndY);
		}
		m_nEndX = point.x ;
		m_nEndY = point.y ;
		
		dc.MoveTo (m_nStartX,m_nStartY);
		dc.LineTo (m_nEndX,m_nEndY);
		dc.SelectObject (poldpen);//选择老画笔
	}
	
	CView::OnMouseMove(nFlags, point);
}




void CMy2DCADView::OnSetLinewidth() 
{
	// TODO: Add your command handler code here
	CLineWidthDlg dlg;
	dlg.m_nLineWidth = m_nLineWidth;
	//按OK按钮自动进行数据交换,所以下句可以用上一句代替
	if(dlg.DoModal()==IDOK)
		m_nLineWidth = dlg.m_nLineWidth ;
	
}



void CMy2DCADView::OnSetLinestyle() 
{
	// TODO: Add your command handler code here
	CMainFrame *pframe = (CMainFrame *)GetParent();
	pframe->m_LineStyleDlg .ShowWindow (SW_SHOW);
	
	//注意:以下代码不对
	//CMainFrame::m_LineStyleDlg.ShowWindow (SW_SHOW);
}

⌨️ 快捷键说明

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