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

📄 drawingview.cpp

📁 软件参考Windows操作系统下的画图程序
💻 CPP
字号:
// DrawingView.cpp : implementation of the CDrawingView class
//

#include "stdafx.h"
#include "Drawing.h"

#include "DrawingDoc.h"
#include "DrawingView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDrawingView

IMPLEMENT_DYNCREATE(CDrawingView, CView)

BEGIN_MESSAGE_MAP(CDrawingView, CView)
	//{{AFX_MSG_MAP(CDrawingView)
	ON_WM_SIZE()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_NCMOUSEMOVE()
	//}}AFX_MSG_MAP
	// 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()

/////////////////////////////////////////////////////////////////////////////
// CDrawingView construction/destruction

CDrawingView::CDrawingView()
{
	// TODO: add construction code here
//	AfxGetApp()->LoadCursor(IDC_CURSOR1);
	m_ptOld.x = m_ptOld.y = 0;
	m_ptCur.x = m_ptCur.y = 0;
	m_bLBtn = FALSE;
	m_preline.SetTwoPoint(0, 0, 0, 0);
	m_prerect.SetTwoPoint(0, 0, 0, 0);
	m_color = RGB(0, 0, 0);
	m_nMode = 0;
	m_nWidth = 0;
	m_nPickGeo = 0;
}

CDrawingView::~CDrawingView()
{

}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CDrawingView drawing

void CDrawingView::OnDraw(CDC* pDC)
{
	CDrawingDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	Draw(*pDC);
}

/////////////////////////////////////////////////////////////////////////////
// CDrawingView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CDrawingView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CDrawingView message handlers

void CDrawingView::OnSize(UINT nType, int cx, int cy) 
{
	CView::OnSize(nType, cx, cy);
	
	// TODO: Add your message handler code here


}
//////////
BOOL CDrawingView::Draw(CDC &dc)
{
	CDC dcmem;
	dcmem.CreateCompatibleDC(&dc);

	CRect rc;
	GetClientRect(&rc);
	CBitmap bitmap;
	bitmap.LoadBitmap(IDB_BITMAP1);
	CBitmap* pOldBitmap = dcmem.SelectObject(&bitmap);

	BITMAP bmpInfo;
	bitmap.GetBitmap(&bmpInfo);

	CDrawingDoc* pDoc = GetDocument();
	BOOL m_isContinue = TRUE;

	for(int count=0; count<pDoc->m_slSum; count++)
	{
		m_isContinue = TRUE;
		for(int i=0; i<pDoc->point.size(); i++)
		{
			if(pDoc->point[i].GetOrderId()==count)
			{
				pDoc->point[i].Draw(dcmem);
				m_isContinue = FALSE;
				break;
			}
		}
		if(!m_isContinue)
			continue;
		for(i=0; i<pDoc->line.size(); i++)
		{
			if(pDoc->line[i].GetOrderId()==count)
			{
				pDoc->line[i].Draw(dcmem);
				m_isContinue = FALSE;
				break;
			}
		}
		if(!m_isContinue)
			continue;
		for(i=0; i<pDoc->rect.size(); i++)
		{
			if(pDoc->rect[i].GetOrderId()==count)
			{
				pDoc->rect[i].Draw(dcmem);
				m_isContinue = FALSE;
				break;
			}
		}
		if(!m_isContinue)
			continue;
		for(i=0; i<pDoc->circle.size(); i++)
		{
			if(pDoc->circle[i].GetOrderId()==count)
			{
				pDoc->circle[i].Draw(dcmem);
				m_isContinue = FALSE;
				break;
			}
		}
		if(!m_isContinue)
			continue;
		for(i=0; i<pDoc->polygon.size(); i++)
		{
			if(pDoc->polygon[i].GetOrderId()==count)
			{
				pDoc->polygon[i].Draw(dcmem);
				break;
			}
		}
	}
	switch(m_nPickGeo)
	{
	case 1:
		m_preline.Draw(dcmem);
		break;
	case 2:
		m_prerect.Draw(dcmem);
		break;
	case 3:
		m_precircle.Draw(dcmem);
		break;
	case 4:
		if(m_polygonTmp.GetSize()>=1)
			m_polygonTmp.Draw(dcmem);
		break;
	}

	dc.BitBlt(0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight, &dcmem, 0, 0, SRCCOPY);
	dcmem.SelectObject(pOldBitmap);
	return TRUE;
}

void CDrawingView::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CMainFrame* pFrame;
	pFrame=(CMainFrame*)AfxGetApp()->m_pMainWnd;
	CStatusBar *pStatus = &pFrame->m_wndStatusBar;
	CString strx;
	strx.Format("X = %d Y = %d", m_ptCur.x, m_ptCur.y);
	pStatus->SetPaneText(1, strx, true);

	CDrawingDoc* pDoc = GetDocument();
	CClientDC dc(this);

	SetGeoProperty();
	GeoLine ln;
	if(m_bLBtn)
	{
		switch(m_nPickGeo)
		{
		case 1:	//画线
			m_preline.SetTwoPoint(m_ptOld, point);
			m_preline.SetColor(m_color);
			m_preline.SetWidth(m_nWidth);
			m_preline.SetMode(m_nMode);
			Invalidate(FALSE);
			break;
		case 2:
			m_prerect.SetTwoPoint(m_ptOld, point);
			m_prerect.SetColor(m_color);
			m_prerect.SetWidth(m_nWidth);
			m_prerect.SetMode(m_nMode);
			Invalidate(FALSE);
			break;
		case 3:	
			m_precircle.SetTwoPoint(m_ptOld, point);
			m_precircle.SetColor(m_color);
			m_precircle.SetWidth(m_nWidth);
			m_precircle.SetMode(m_nMode);
			Invalidate(FALSE);
			break;
		}
	}

	m_ptCur = point;
	CView::OnMouseMove(nFlags, point);
}

void CDrawingView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	m_ptOld = point;
	m_ptCur = point;
	m_bLBtn = TRUE;
	SetCapture();
	CView::OnLButtonDown(nFlags, point);
}

void CDrawingView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	m_bLBtn = FALSE;
	m_ptCur = point;
	ReleaseCapture();
	CDrawingDoc* pDoc = GetDocument();
	SetGeoProperty();
	CRect r;
	GeoPoint pt;
	GeoLine  ln;
	GeoRect  rc;
	GeoCircle cl;
	CMainFrame *pFrame = (CMainFrame *)AfxGetApp()->GetMainWnd()->GetActiveWindow();
	int oldpick = pFrame->m_wndPolyBar.GetOldPick();
	if(oldpick==4 && m_nPickGeo!=4 && m_polygonTmp.GetSize()!=0)
	{
		m_polygonTmp.SetClose();
		m_polygonTmp.SetOrderId(pDoc->m_slSum);
		pDoc->m_slSum++;
		pDoc->Add(m_polygonTmp);
		m_polygonTmp.Reset();
		Invalidate(FALSE);
	}
	switch(m_nPickGeo)
	{
	case 0://画点
		pt = point;
		pt.SetColor(m_color);
		pt.SetWidth(m_nWidth);
		pt.SetMode(m_nMode);
		pt.SetOrderId(pDoc->m_slSum);
		pDoc->m_slSum++;
		pDoc->Add(pt);
		r.SetRect(point.x-m_nWidth-1, point.y-m_nWidth-1, point.x+m_nWidth+1, point.y+m_nWidth+1);
		InvalidateRect(&r, FALSE);
		break;
	case 1://画线
		ln.SetTwoPoint(m_ptOld, m_ptCur);
		ln.SetColor(m_color);
		ln.SetWidth(m_nWidth);
		ln.SetMode(m_nMode);
		ln.SetOrderId(pDoc->m_slSum);
		pDoc->m_slSum++;
		pDoc->Add(ln);
		m_preline.SetTwoPoint(0, 0, 0, 0);
		break;
	case 2://画矩形
		rc.SetTwoPoint(m_ptOld, m_ptCur);
		rc.SetColor(m_color);
		rc.SetWidth(m_nWidth);
		rc.SetMode(m_nMode);
		rc.SetOrderId(pDoc->m_slSum);
		pDoc->m_slSum++;
		pDoc->Add(rc);
		m_prerect.SetTwoPoint(0, 0, 0, 0);
		break;
	case 3:
		cl.SetTwoPoint(m_ptOld, m_ptCur);
		cl.SetColor(m_color);
		cl.SetWidth(m_nWidth);
		cl.SetMode(m_nMode);
		cl.SetOrderId(pDoc->m_slSum);
		pDoc->m_slSum++;
		pDoc->Add(cl);
		m_prerect.SetTwoPoint(0, 0, 0, 0);
		break;
	case 4:
		m_polygonTmp.SetColor(m_color);
		m_polygonTmp.SetMode(m_nMode);
		m_polygonTmp.SetWidth(m_nWidth);
		m_polygonTmp.AddPoint(point);
		if(m_polygonTmp.IsClose())
		{
			m_polygonTmp.SetOrderId(pDoc->m_slSum);
			pDoc->m_slSum++;
			pDoc->Add(m_polygonTmp);
			m_polygonTmp.Reset();
		}
		CClientDC dc(this);
		if(m_polygonTmp.GetSize()>=3)
		{
			m_polygonTmp.UndoAdd();
			COLORREF c = m_polygonTmp.GetColor();
			m_polygonTmp.SetColor(RGB(255, 255, 255));
			m_polygonTmp.Draw(dc);
			m_polygonTmp.AddPoint(point);
			m_polygonTmp.SetColor(c);
		}
		Invalidate(FALSE);
		break;
	}
	m_ptOld = m_ptCur;
	CView::OnLButtonUp(nFlags, point);
}

void CDrawingView::SetGeoProperty()
{
	CMainFrame *pFrame = (CMainFrame *)AfxGetApp()->GetMainWnd()->GetActiveWindow();
	if(pFrame->GetSafeHwnd())
	{
		m_color = pFrame->m_wndColorPane.GetColor();
		m_nMode  = pFrame->m_wndPolyBar.GetMode();
		m_nWidth = pFrame->m_wndPolyBar.GetWidth();
		m_nPickGeo = pFrame->m_wndPolyBar.GetPick();
	}
}

void CDrawingView::OnNcMouseMove(UINT nHitTest, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default

	CMainFrame* pFrame;
	pFrame=(CMainFrame*)AfxGetApp()->m_pMainWnd;
	CStatusBar *pStatus = &pFrame->m_wndStatusBar;
	CString strx = "";
	pStatus->SetPaneText(1, strx, true);

	CView::OnNcMouseMove(nHitTest, point);
}

⌨️ 快捷键说明

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