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

📄 caijianview.cpp

📁 用Visual C++编写的一个实现矩形窗口对多边形的裁剪!
💻 CPP
字号:
// caijianView.cpp : implementation of the CCaijianView class
//

#include "stdafx.h"
#include "caijian.h"

#include "caijianDoc.h"
#include "caijianView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CCaijianView

IMPLEMENT_DYNCREATE(CCaijianView, CView)

BEGIN_MESSAGE_MAP(CCaijianView, CView)
	//{{AFX_MSG_MAP(CCaijianView)
	ON_COMMAND(ID_EDIT_CLEAR_ALL, OnEditClearAll)
	ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
	ON_WM_LBUTTONDOWN()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONUP()
	ON_WM_RBUTTONUP()
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CCaijianView construction/destruction

CCaijianView::CCaijianView()
{
	// TODO: add construction code here
    m_bCaptured=false;
	m_bDefineRect=false;
	m_bDefinePointV=false;
}

CCaijianView::~CCaijianView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CCaijianView drawing

void CCaijianView::OnDraw(CDC* pDC)
{
	CCaijianDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	int l,t,r,b;
	l=min(TopLeft.x,BottomRight.x);
	r=max(TopLeft.x,BottomRight.x);
	t=min(TopLeft.y,BottomRight.y);
	b=max(TopLeft.y,BottomRight.y);
	CRect rect(l,t,r,b);
	//定义裁减矩形区域,并赋值

	CPen penDot,penSolid;
    penSolid.CreatePen(PS_SOLID,1,RGB(0,0,0));
	pDC->SelectObject(penSolid);
	pDC->TextOut(10,10,"重新定义多边形的点坐标,(鼠标左键定义点坐标,右键结束)");
	pDC->TextOut(10,35,"重新定义裁减的区域,(按住鼠标左键不放,移动鼠标,画出一个矩形框)");
	//屏幕上的提示信息

	if(PointArray.GetSize())
	{
		//当自定义多边形点坐标时,在各个点坐标处画一个小圆,以显示点的位置
		int i;
		for(i=0;i<PointArray.GetSize();i++)
		{
			pDC->MoveTo(PointArray.GetAt(i));
			CRect ellipseRect;
			ellipseRect.top = PointArray.GetAt(i).y - 2;
			ellipseRect.bottom = PointArray.GetAt(i).y + 2;
			ellipseRect.left = PointArray.GetAt(i).x - 2;
			ellipseRect.right = PointArray.GetAt(i).x + 2;
			pDC->Ellipse(ellipseRect);
		}
	}

	if(pDoc->bCutRect) 
	{
		//判断是否裁减,若是,则根据裁减区域进行裁减
		pDoc->m_grahics.CutRect(rect);
		pDoc->bCutRect=false;
	}
	pDoc->m_grahics.DrawPloyon(pDC);
	//画出多边形
	
}

void CCaijianView::OnInitialUpdate() 
{
	CView::OnInitialUpdate();
	CSize sizeTotal;
	sizeTotal.cx = sizeTotal.cy = 100;
	
	CCaijianDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	//设置初始的裁减区域
	int minX = pDoc->m_grahics.Point[0].x , minY = pDoc->m_grahics.Point[0].y;
	int maxX = pDoc->m_grahics.Point[0].x , maxY = pDoc->m_grahics.Point[0].y;
	for(int i=1;i<pDoc->m_grahics.PointCount;i++)
	{
		if(minX > pDoc->m_grahics.Point[i].x)
			minX = pDoc->m_grahics.Point[i].x;
		if(minY > pDoc->m_grahics.Point[i].y)
			minY = pDoc->m_grahics.Point[i].y;
		if(maxX < pDoc->m_grahics.Point[i].x)
			maxX = pDoc->m_grahics.Point[i].x;
		if(maxY < pDoc->m_grahics.Point[i].y)
			maxY = pDoc->m_grahics.Point[i].y;
	}
	TopLeft = CPoint(minX,minY);
	BottomRight = CPoint(maxX,maxY);
	// TODO: Add your specialized code here and/or call the base class
	
}

/////////////////////////////////////////////////////////////////////////////
// CCaijianView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CCaijianView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CCaijianView message handlers


void CCaijianView::OnEditClearAll() 
{
	// TODO: Add your command handler code here
	m_bDefinePointV = true;
	
}

void CCaijianView::OnEditCopy() 
{
	// TODO: Add your command handler code here
	m_bDefineRect = true;
	
}

void CCaijianView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	//对鼠标按下左键的相应
	if(m_bDefineRect)
    {	
		//如果是自定义裁减的区域的操作
		SetCapture();//捕获鼠标
		m_bCaptured = TRUE;
		CDC *dc=GetDC();
		CRect rect(TopLeft,BottomRight);
		dc->SelectStockObject(WHITE_PEN);

		
		dc->Rectangle(rect);
		InvalidateRect(rect,false);
		TopLeft = point;

		::SetCursor(::LoadCursor(NULL, IDC_CROSS));//设置鼠标样子为十字形的
	}
	if(m_bDefinePointV)
	{
		//如果是自定义点坐标的操作
		PointArray.Add(point);
		CRect ellipseRect;
		ellipseRect.top = point.y - 2;
		ellipseRect.bottom = point.y + 2;
		ellipseRect.left = point.x - 2;
		ellipseRect.right = point.x + 2;
		InvalidateRect(ellipseRect,true);
	}
	CView::OnLButtonDown(nFlags, point);
}

void CCaijianView::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	//对鼠标移动时的相应
	if (m_bCaptured) 
	{
		//画出相应的矩形裁减边框
		CDC *dc=GetDC();
		CRect rect(TopLeft,BottomRight);
		dc->SelectStockObject(WHITE_PEN);

		
		dc->Rectangle(rect);//用白色擦除原先的矩形边框
		InvalidateRect(rect,false);
		BottomRight=point;
		
		CRect newrect(TopLeft,BottomRight);
		CPen pen;
		pen.CreatePen(PS_DOT,1,RGB(0,0,0));
		dc->SelectObject(pen);
		dc->Rectangle(newrect);//用虚线画出新的矩形边框
    }
	CView::OnMouseMove(nFlags, point);
}

void CCaijianView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	//对鼠标放开左键的相应
    if (m_bCaptured) 
	{
		::ReleaseCapture();
        m_bCaptured = false;
		m_bDefineRect = false;
	}
	CView::OnLButtonUp(nFlags, point);
}

void CCaijianView::OnRButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	//对鼠标放开右键的相应
	if(m_bDefinePointV)
	{
		CCaijianDoc* pDoc = GetDocument();
		ASSERT_VALID(pDoc);
		pDoc->m_grahics.PointCount=PointArray.GetSize();
		if(pDoc->m_grahics.Point)
			delete pDoc->m_grahics.Point;
		pDoc->m_grahics.Point = new CPoint[pDoc->m_grahics.PointCount];
		for(int i=0;i<pDoc->m_grahics.PointCount;i++)
			pDoc->m_grahics.Point[i]=PointArray.GetAt(i);//对Point点坐标重新赋值
		PointArray.RemoveAll();
		m_bDefinePointV=false;
		CRect rect;
		this->GetClientRect(rect);
		InvalidateRect(rect);
		
		//获取新的初始裁减矩形范围
		int minX = pDoc->m_grahics.Point[0].x , minY = pDoc->m_grahics.Point[0].y;
		int maxX = pDoc->m_grahics.Point[0].x , maxY = pDoc->m_grahics.Point[0].y;
		for(i=1;i<pDoc->m_grahics.PointCount;i++)
		{
			if(minX > pDoc->m_grahics.Point[i].x)
				minX = pDoc->m_grahics.Point[i].x;
			if(minY > pDoc->m_grahics.Point[i].y)
				minY = pDoc->m_grahics.Point[i].y;
			if(maxX < pDoc->m_grahics.Point[i].x)
				maxX = pDoc->m_grahics.Point[i].x;
			if(maxY < pDoc->m_grahics.Point[i].y)
				maxY = pDoc->m_grahics.Point[i].y;
		}
		TopLeft = CPoint(minX,minY);
		BottomRight = CPoint(maxX,maxY);
	}
	CView::OnRButtonUp(nFlags, point);
}


⌨️ 快捷键说明

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