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

📄 2dcadview.cpp

📁 C++画图板+画图工具
💻 CPP
字号:
// 2DCADView.cpp : implementation of the CMy2DCADView class
//

#include "stdafx.h"
#include "2DCAD.h"

#include "2DCADDoc.h"
#include "2DCADView.h"
#include "resource.h"
#include "d_stack.h"

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

#define WM_MY_MESSAGE (WM_USER + 101)

typedef struct
{
		int x;
		int y;
}Seed;

/////////////////////////////////////////////////////////////////////////////
// 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_UPDATE_COMMAND_UI(ID_GRAPH_LINE, OnUpdateGraphLine)
	ON_WM_LBUTTONDOWN()
	ON_WM_SETCURSOR()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONUP()
	ON_UPDATE_COMMAND_UI(ID_GRAPH_FILL, OnUpdateGraphFill)
	ON_COMMAND(ID_GRAPH_POLYGON, OnGraphPolygon)
	ON_UPDATE_COMMAND_UI(ID_GRAPH_POLYGON, OnUpdateGraphPolygon)
	ON_COMMAND(ID_GRAPH_CUT, OnGraphCut)
	ON_UPDATE_COMMAND_UI(ID_GRAPH_CUT, OnUpdateGraphCut)
	//}}AFX_MSG_MAP
	// Standard printing commands
    ON_COMMAND(ID_GRAPH_LINE, OnGraphLine)
	ON_COMMAND(ID_GRAPH_FILL, OnGraphFill)
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
	ON_MESSAGE(WM_MY_MESSAGE,OnMyMessage)
END_MESSAGE_MAP()

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

CMy2DCADView::CMy2DCADView()
{
	// TODO: add construction code here
	m_bIsPoint=false;
	m_bIsLine=false;
	m_bIsFill=false;
	m_bIsPolygon=false;
	m_bIsCut = false;
	m_nStartX=0;
	m_nStartY=0;
	m_nStep=0;
	m_nEndX=0;
	m_nEndY=0;
	m_nMidX=0;
	m_nMidY=0;
	m_pointDown=0;
	m_movePixel=0;
	m_nFillColor= RGB(0,0,0);


}

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

	//CRect rect;
	//GetClientRect(&rect);
	//pDC->BitBlt(0,0,rect.Width(),rect.Height(),&m_dcCompatible,0,0,SRCCOPY);
	/*if(m_bIsPoint)
	{	
	/*pDC->SetPixel(100,40,RGB(255,0,255));//绘制一个彩色的点
	pDC->MoveTo(400,40);
	pDC->LineTo(500,40);//线段
	POINT polyline[4]={{240,240},{80,120},{240,120},{80,240}};
	pDC->Polyline(polyline,4);//折线
	pDC->Rectangle(390,110,600,230);//矩形
	pDC->Ellipse(80,260,280,380);//椭圆
	POINT polygon[3]={{380,330},{530,260},{500,360}};
	pDC->Polygon(polygon,3);//多边形
	pDC->SetPixel(LButtonDownPoint,RGB(255,0,0));
	}*/

}

/////////////////////////////////////////////////////////////////////////////
// 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() 
{
	// TODO: Add your command handler code here
	//CClientDC dc(this);
	//dc.TextOut(100,100,"画点");
	m_bIsPoint=true;
	m_bIsLine=false;
	m_bIsFill=false;
	m_bIsPolygon=false;
	m_bIsCut=false;
	//SendMessage(WM_MY_MESSAGE);
}

//填充消息处理
void CMy2DCADView::OnGraphFill() 
{
	// TODO: Add your command handler code here
CColorDialog dlg;
	m_bIsFill=true;
	m_bIsPoint=false;
	m_bIsLine=false;
	m_bIsPolygon=false;
	m_bIsCut=false;
	if(dlg.DoModal()==IDOK)
	{
		m_nFillColor=dlg.GetColor();
	}
	
}

//划线消息处理
void CMy2DCADView::OnGraphLine() 
{
	// TODO: Add your command handler code here
	//CClientDC dc(this);
	//dc.TextOut(100,100,"画线");
    m_bIsLine=true;
	m_bIsPoint=false;
	m_bIsFill=false;
	m_bIsPolygon=false;
	m_bIsCut=false;
}

//多边形消息处理
void CMy2DCADView::OnGraphPolygon() 
{
	// TODO: Add your command handler code here
	m_bIsLine=false;
	m_bIsPoint=false;
	m_bIsFill=false;
	m_bIsPolygon=true;
	m_bIsCut=false;
	
}


//菜单栏,画点功能选择
void CMy2DCADView::OnUpdateGraphFill(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetRadio(m_bIsFill);
}

//菜单栏,画点功能选择
void CMy2DCADView::OnUpdateGraphPoint(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetRadio(m_bIsPoint);
}

//菜单栏,画线功能选择
void CMy2DCADView::OnUpdateGraphLine(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetRadio(m_bIsLine);
	
}
//菜单栏,画多边形功能选择
void CMy2DCADView::OnUpdateGraphPolygon(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetRadio(m_bIsPolygon);
}

//自定义消息测试
LRESULT CMy2DCADView::OnMyMessage(WPARAM wParam,LPARAM lParam)
{
	CClientDC dc(this);
	dc.TextOut(100,50,"测试自定义消息");
	return 1;
}

//系统自定义,右键弹出菜单
void CMy2DCADView::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_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
	CClientDC dc(this);
	//CBrush*pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
	m_nEndX=m_nEndY=0;
	
	/*if(!m_dcCompatible.m_hDC)
	{
		m_dcCompatible.CreatCompatibleDC(&dc);
		Crect rect;
		GetClientRect(&rect);
		CBitmap bitmap;
		bitmap.CreateCompatibleBitmap(&dc,rect.Width(),rect.Height());
		m_dcCompatible.SelectObject(&bitmap);
		m_dcCompatible.SelectObject(pBrush);
	}*/
	LButtonDownPoint=point;

if(m_bIsPoint)
{
    dc.SetPixel(point,RGB(0,0,0));
	//CMy2DCADView::OnDraw(dc(this));
}


	if(m_bIsLine)
	{
		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);
			m_nStep=0;
			m_nStartX=m_nStartY=0;
			::ReleaseCapture();
		}
	}

	if(m_bIsPolygon)
	{
		if(m_nStep==0)
		{
			
			m_nMidX=point.x;
			m_nMidY=point.y;
			m_nStartX=point.x;
			m_nStartY=point.y;
			m_nStep++;
			//SetCapture();
		}
		else
		{
			if(point.x!=m_nStartX&&point.y!=m_nStartY)
			{
				
				dc.MoveTo(m_nMidX,m_nMidY);
			    dc.LineTo(point.x,point.y);
				m_nMidX=point.x;
				m_nMidY=point.y;
				//m_nStep++;
			}
		    else
			{
			dc.MoveTo(m_nMidX,m_nMidY);
			dc.LineTo(point.x,point.y);
			m_nStep=0;
			m_nMidX=0;
			m_nMidY=0;
			m_nStartX=m_nStartY=0;
			//::ReleaseCapture();
			}
		}
	}

    if(m_bIsFill)
	{
		SetCapture();
		CMy2DCADView::SeedFill(point.x,point.y,m_nFillColor,dc.GetPixel(point.x,point.y));
		::ReleaseCapture();
	}
	if(m_bIsCut)
	{

		
		if(m_nStep==0)
		{
			dc.TextOut(50,50,"裁剪时请拖动鼠标");
			m_pointDown = point;
			m_movePixel = point;
			m_nStep++;
			//SetCapture();
		}
		else
		{
			dc.Rectangle(CRect(m_pointDown,point));
			m_nStep=0;
			//m_nStartX=m_nStartY=0;
			//::ReleaseCapture();
		}
	}

   CView::OnLButtonDown(nFlags, point);
}

   
//功能切换时的光标变换
BOOL CMy2DCADView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{
	// TODO: Add your message handler code here and/or call default
	if(m_bIsPoint)
	{
		HCURSOR hCursor;
		hCursor=AfxGetApp()->LoadCursor(IDC_POINTER);
		SetCursor(hCursor);
		return true;
	}
	if(m_bIsLine)
	{
		HCURSOR hCursor;
		hCursor=AfxGetApp()->LoadStandardCursor(IDC_CROSS);
		SetCursor(hCursor);
		return true;
	}
	if(m_bIsFill)
	{
		HCURSOR hCursor;
		hCursor=AfxGetApp()->LoadCursor(IDC_FILLER);
		SetCursor(hCursor);
		return true;
	}

	return CView::OnSetCursor(pWnd, nHitTest, message);
}



//鼠标移动时的处理函数,橡皮线功能
void CMy2DCADView::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CClientDC dc(this);
	if(m_bIsLine)
	{
		if(m_nStep != 0)
		{
			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);
		}
	}
	if(m_bIsPolygon)
	{
		if(m_nStep != 0)
		{
			dc.SetROP2(R2_NOTXORPEN);
			if(m_nEndX!=0)
			{
				dc.MoveTo(m_nMidX,m_nMidY);
				dc.LineTo(m_nEndX,m_nEndY);
			}
			m_nEndX=point.x;
			m_nEndY=point.y;
			dc.MoveTo(m_nMidX,m_nMidY);
			dc.LineTo(m_nEndX,m_nEndY);
		}
	}
	if(m_bIsCut)//裁剪
	{
		if(m_nStep != 0)
		{
			dc.SetROP2(R2_NOTXORPEN);
			dc.Rectangle(CRect(m_pointDown,m_movePixel));
			m_movePixel = point;
			dc.Rectangle(CRect(m_pointDown,m_movePixel));
		}
	}

	
	CView::OnMouseMove(nFlags, point);
}



void CMy2DCADView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if(m_bIsCut)
	{
		CClientDC dcCut(this);
		CRect clientRect;
		this->GetClientRect(clientRect);
		int xMin = 0, yMin = 0, xMax = 0, yMax = 0;
			m_bIsCut = false;
			if(m_pointDown.x < point.x)
			{
				xMin = m_pointDown.x;
				xMax = point.x;
			}
			else
			{
				xMax = m_pointDown.x;
				xMin = point.x;
			}
			if(m_pointDown.y < point.y)
			{
				yMin = m_pointDown.y;
				yMax = point.y;
			}
			else
			{
				yMax = m_pointDown.y;
				yMin = point.y;
			}
	
			//将裁剪区域外的部分刷成白色 
			dcCut.FillSolidRect(0, 0, clientRect.Width(), yMin, RGB(255,255,255));
			dcCut.FillSolidRect(0, yMin, xMin, yMax, RGB(255,255,255));
			dcCut.FillSolidRect(xMax, yMin, clientRect.Width(), yMax, RGB(255,255,255));
			dcCut.FillSolidRect(0, yMax, clientRect.Width (), clientRect.Height(), RGB(255,255,255));
		
		//dcCut.SetROP2(R2_NOT);
		//dcCut.SelectStockObject(NULL_BRUSH);
		//dcCut.Rectangle(CRect(m_pointDown, point));
	}

	
CView::OnLButtonUp(nFlags, point);
}

void CMy2DCADView::SeedFill(int x,int y, COLORREF newcolor,COLORREF oldcolor)
{
	CClientDC dc(this);//声明一个对象
     /*if(dc.GetPixel(x,y)==oldcolor)//种子点为背景色
    {
       dc.SetPixel(x,y,newcolor);
        SeedFill(x-1,y,newcolor,oldcolor);//左
        SeedFill(x,y+1,newcolor,oldcolor);//上
        SeedFill(x+1,y,newcolor,oldcolor);//右
        SeedFill(x,y-1,newcolor,oldcolor);//下
    }//递归实现种子填充法
		*/

    miniStack<Seed> seedStack;
    int xl,xr;
    bool spanNeedFill;
    Seed pt;
    pt.x=x;
    pt.y=y;
	seedStack.push(pt);
	while(!seedStack.empty())
	{
		pt=seedStack.top();
		seedStack.pop();
        y=pt.y;
		x=pt.x;
		while(dc.GetPixel(x,y)==oldcolor)
		{
			dc.SetPixel(x,y,newcolor);
			x++;
		}
		xr=x-1;
		x=pt.x-1;
				 
		while(dc.GetPixel(x,y)==oldcolor)
		{
			dc.SetPixel(x,y,newcolor);
			x--;
		}
		xl=x+1;
		x=xl;
		y=y+1;
		while(x<xr)
		{
			spanNeedFill=false;
			while(dc.GetPixel(x,y)==oldcolor)
			{
				spanNeedFill=true;
				x++;
			}
			if(spanNeedFill)
			{
				pt.x=x-1;
				pt.y=y;
			    seedStack.push(pt);
				spanNeedFill=false;
			}
			while(dc.GetPixel(x,y)!=oldcolor&&x<xr)
			{
				x++;
			}
		}
		x=xl;
		y=y-1-1;
		 while(x<xr)
		 {
			 spanNeedFill=false;
			 while(dc.GetPixel(x,y)==oldcolor)
			 {
				 spanNeedFill=true;
				 x++;
			 }
			 if(spanNeedFill)
			 {
				 pt.x=x-1;
				 pt.y=y;
				 seedStack.push(pt);
				 spanNeedFill=false;
			 }
			 while(dc.GetPixel(x,y)!=oldcolor&&x<xr)
			 {
				 x++;
			 }
		 }
	 }
}







void CMy2DCADView::OnGraphCut() 
{
	// TODO: Add your command handler code here
	m_bIsPoint=false;
	m_bIsLine=false;
	m_bIsFill=false;
	m_bIsPolygon=false;
	m_bIsCut=true;
	
}

void CMy2DCADView::OnUpdateGraphCut(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetRadio(m_bIsCut);
	
}

⌨️ 快捷键说明

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