www.pudn.com > MyAssignment.rar > MyAssignmentView.cpp


// MyAssignmentView.cpp : implementation of the CMyAssignmentView class 
// 
 
#include "stdafx.h" 
#include "MyAssignment.h" 
#include "MainFrm.h" 
#include "MyAssignmentDoc.h" 
#include "MyAssignmentView.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
#define LEFT 1 
#define RIGHT 2 
#define BOTTOM 4 
#define  TOP 8 
 
#define XL 100 
#define XR 300 
#define YT 100 
#define YB 250 
///////////////////////////////////////////////////////////////////////////// 
// CMyAssignmentView 
 
IMPLEMENT_DYNCREATE(CMyAssignmentView, CView) 
 
BEGIN_MESSAGE_MAP(CMyAssignmentView, CView) 
	//{{AFX_MSG_MAP(CMyAssignmentView) 
	ON_COMMAND(ID_CLIPLINE, OnClipline) 
	ON_COMMAND(ID_CLIPPOLYGON, OnClippolygon) 
	ON_COMMAND(ID_CUT_EMER, OnCutEmer) 
	ON_WM_KEYDOWN() 
	//}}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() 
 
///////////////////////////////////////////////////////////////////////////// 
// CMyAssignmentView construction/destruction 
 
CMyAssignmentView::CMyAssignmentView() 
{ 
	// TODO: add construction code here 
 
} 
 
CMyAssignmentView::~CMyAssignmentView() 
{ 
} 
 
BOOL CMyAssignmentView::PreCreateWindow(CREATESTRUCT& cs) 
{ 
	// TODO: Modify the Window class or styles here by modifying 
	//  the CREATESTRUCT cs 
 
	return CView::PreCreateWindow(cs); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CMyAssignmentView drawing 
 
void CMyAssignmentView::OnDraw(CDC* pDC) 
{ 
	CMyAssignmentDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc); 
	if(m_cut==1)	 
{	CPen newpen(PS_SOLID,3,RGB(0,128,0)); 
	CPen *old=pDC->SelectObject(&newpen); 
	pDC->Rectangle(CRect(XL,YT,XR,YB));//剪切窗口 
    ptset[0]=CPoint(120,150); 
	ptset[1]=CPoint(170,110); 
    ptset[2]=CPoint(0,190); 
	ptset[3]=CPoint(350,150); 
	ptset[4]=CPoint(0,250); 
	ptset[5]=CPoint(150,230); 
	ptset[6]=CPoint(200,50); 
	ptset[7]=CPoint(120,150); 
	ptset1[0]=CPoint(20,150); 
	ptset1[1]=CPoint(170,110); 
    
	ptset1[2]=CPoint(250,150); 
 
	ptset1[3]=CPoint(200,230); 
 
	ptset1[4]=CPoint(20,150); 
	LOGFONT lf; 
	CFont *OldFont,NewFont; 
	pDC->GetCurrentFont()->GetLogFont(&lf); 
	lf.lfCharSet=DEFAULT_CHARSET; 
	lf.lfHeight=35; 
	lf.lfWidth=20; 
	strcpy(lf.lfFaceName,"隶书"); 
	NewFont.CreateFontIndirect(&lf); 
	OldFont=pDC->SelectObject(&NewFont); 
	pDC->SetBkMode(TRANSPARENT); 
	CBrush BrushNew,*BrushOld; 
	CBitmap bitmap; 
			bitmap.LoadBitmap(IDB_BITMAP1); 
			BrushNew.CreatePatternBrush(&bitmap); 
			BrushOld=pDC->SelectObject(&BrushNew); 
			pDC->BeginPath(); 
	pDC->TextOut(0,20,"先按键盘字母X,出现要剪切的线段"); 
	pDC->TextOut(0,50,"先按键盘字母D,出现要剪切的多边形"); 
	pDC->SelectObject(old); 
	pDC->EndPath(); 
			pDC->StrokeAndFillPath(); 
		 
} 
	// TODO: add draw code for native data here 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CMyAssignmentView printing 
 
BOOL CMyAssignmentView::OnPreparePrinting(CPrintInfo* pInfo) 
{ 
	// default preparation 
	return DoPreparePrinting(pInfo); 
} 
 
void CMyAssignmentView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add extra initialization before printing 
} 
 
void CMyAssignmentView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add cleanup after printing 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CMyAssignmentView diagnostics 
 
#ifdef _DEBUG 
void CMyAssignmentView::AssertValid() const 
{ 
	CView::AssertValid(); 
} 
 
void CMyAssignmentView::Dump(CDumpContext& dc) const 
{ 
	CView::Dump(dc); 
} 
 
CMyAssignmentDoc* CMyAssignmentView::GetDocument() // non-debug version is inline 
{ 
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyAssignmentDoc))); 
	return (CMyAssignmentDoc*)m_pDocument; 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CMyAssignmentView message handlers 
 
void CMyAssignmentView::OnClipline()  
{ 
	// TODO: Add your command handler code here 
	    
	 CDC* pDC=GetDC(); 
	CPen newpen(PS_SOLID,3,RGB(255,128,0)); 
	CPen *old=pDC->SelectObject(&newpen); 
	if(flag!=1)  
	{MessageBox("请先按键盘字母X","警告!");}	 
	else 
	{ 
	float x,y; 
	int i; 
	int code1,code2; 
			RedrawWindow(); 
	// 求两端点所在区号code 
	for(i=0;iXR)c=c|RIGHT; 
		if(ptset[i].y>YB) c=c|BOTTOM; 
		else if(ptset[i].yXR) c=c|RIGHT; 
		if(ptset[i+1].y>YB) c=c|BOTTOM; 
		else if(ptset[i+1].yMoveTo(ptset[i].x,ptset[i].y); 
		pDC->LineTo(ptset[i+1].x,ptset[i+1].y); 
	}	 
	if(code1==0&&code2==0) 
	{pDC->MoveTo(ptset[i].x,ptset[i].y); 
		pDC->LineTo(ptset[i+1].x,ptset[i+1].y); 
	} 
	if(code1==0&&code2!=0) 
	{ 
		pDC->MoveTo(ptset[0].x,ptset[0].y);	 
		if((LEFT&code2)!=0) 	        //线段与左边界相交 
		{ 
		x=XL; 
		y=(float)ptset[i].y+(ptset[i+1].y-ptset[i].y)*(XL-ptset[i].x)/(ptset[i+1].x-ptset[i].x); 
		} 
		else if((RIGHT&code2)!=0)          //线段与右边界相交 
		{ 
		x=XR; 
		y=(float)ptset[i].y+(ptset[i+1].y-ptset[i].y)*(XR-ptset[i].x)/(ptset[i+1].x-ptset[i].x); 
		} 
	 
		else if((BOTTOM&code2)!=0)          //线段与下边界相交 
		{ 
		y=YB; 
		x=(float)ptset[i].x+(ptset[i+1].x-ptset[i].x)*(YB-ptset[i].y)/(ptset[i+1].y-ptset[i+1].y); 
		} 
		else if((TOP&code2)!=0)          //线段与上边界相交 
		{ 
		y=YT; 
		x=(float)ptset[i].x+(ptset[i+1].x-ptset[i].x)*(YT-ptset[i].y)/(ptset[i+1].y-ptset[i].y); 
		} 
		 
		ptset[i+1].x=(long)x; 
		ptset[i+1].y=(long)y; 
		 
		pDC->LineTo(ptset[i+1].x,ptset[i+1].y); 
	} 
			 
	if(code1!=0&&code2==0) 
	{ 
		pDC->MoveTo(ptset[i+1].x,ptset[i+1].y); 
		if((LEFT&code1)!=0) 	        //线段与左边界相交 
		{ 
		x=XL; 
		y=(float)ptset[i].y+(ptset[i+1].y-ptset[i].y)*(XL-ptset[i].x)/(ptset[i+1].x-ptset[i].x); 
		} 
		else if((RIGHT&code1)!=0)          //线段与右边界相交 
		{ 
		x=XR; 
		y=(float)ptset[i].y+(ptset[i+1].y-ptset[i].y)*(XR-ptset[i].x)/(ptset[i+1].x-ptset[i].x); 
		} 
	 
		else if((BOTTOM&code1)!=0)          //线段与下边界相交 
		{ 
		y=YB; 
		x=(float)ptset[i].x+(ptset[i+1].x-ptset[i].x)*(YB-ptset[i].y)/(ptset[i+1].y-ptset[i+1].y); 
		} 
		else if((TOP&code1)!=0)          //线段与上边界相交 
		{ 
		y=YT; 
		x=(float)ptset[i].x+(ptset[i+1].x-ptset[i].x)*(YT-ptset[i].y)/(ptset[i+1].y-ptset[i].y); 
		} 
		 
		ptset[i].x=(long)x; 
		ptset[i].y=(long)y; 
		pDC->LineTo(ptset[i].x,ptset[i].y); 
	} 
	} 
	} 
} 
 
void CMyAssignmentView::OnClippolygon()  
{ 
	// TODO: Add your command handler code here 
	CDC* pDC=GetDC(); 
	CPen newpen(PS_SOLID,1,RGB(128,128,0)); 
	CPen *old=pDC->SelectObject(&newpen); 
if(flag!=2) 
{ MessageBox("请先按键盘字母D","警告!");} 
else{ 
//else	 
	int i,k; 
	int code1,code2; 
	int	M=5; 
	RedrawWindow(); 
	// 求两端点所在区号code 
	k=0; 
	for(i=0;iXL)c=0; 
		code1=c; 
		 c=0; 
		if(ptset1[i+1].xXL) c=0; 
		code2=c; 
		if(code1!=0&&code2==0) 
		{ 
			pt[k].x=XL; 
			pt[k].y=ptset1[i].y+(ptset1[i+1].y-ptset1[i].y)*(XL-ptset1[i].x)/(ptset1[i+1].x-ptset1[i].x); 
			pt[k+1].x=ptset1[i+1].x; 
			pt[k+1].y=ptset1[i+1].y; 
			k=k+2; 
		}				 
		if(code1==0&&code2==0) 
		{			 
			if(k==0) 
			{ 
				pt[k].x=ptset1[i].x; 
				pt[k].y=ptset1[i].y; 
				pt[k+1].x=ptset1[i+1].x; 
				pt[k+1].y=ptset1[i+1].y; 
				k=k+2; 
			} 
			if(k!=0) 
			{ 
				pt[k].x=ptset1[i+1].x; 
				pt[k].y=ptset1[i+1].y; 
				k=k+1; 
			} 
		} 
		if(code1==0&&code2!=0) 
		{ 
			pt[k].x=XL; 
			pt[k].y=ptset1[i].y+(ptset1[i+1].y-ptset1[i].y)*(XL-ptset1[i].x)/(ptset1[i+1].x-ptset1[i].x); 
			k++; 
		}						 
	} 
			pt[k].x=pt[0].x; 
			pt[k].y=pt[0].y; 
			M=k+1; 
	 
	k=0;		 
	for(i=0;iXR) c=2; 
		code1=c; 
		 c=0; 
		if(pt[i+1].xXR) c=2; 
		code2=c; 
	if(code1==0&&code2==0) 
	{ 
		if(k==0) 
		{ 
			pts[k].x=pt[i].x; 
			pts[k].y=pt[i].y; 
			pts[k+1].x=pt[i+1].x; 
			pts[k+1].y=pt[i+1].y; 
			k=k+2; 
		} 
		if(k!=0) 
		{ 
			pts[k].x=pt[i+1].x; 
			pts[k].y=pt[i+1].y; 
			k++; 
		} 
	} 
	if(code1!=0&&code2==0) 
		{ 
			 
			pts[k].x=XR; 
			pts[k].y=pt[i].y+(pt[i+1].y-pt[i].y)*(XR-pt[i].x)/(pt[i+1].x-pt[i].x); 
			pts[k+1].x=pt[i+1].x; 
			pts[k+1].y=pt[i+1].y; 
			k=k+2; 
		}	 
	 
		if(code1==0&&code2!=0) 
		{ 
			pts[k].x=XR; 
			pts[k].y=pt[i].y+(pt[i+1].y-pt[i].y)*(XR-pt[i].x)/(pt[i+1].x-pt[i].x); 
			k=k+1; 
		}		 
	} 
	//增加对最后一条边的判断 
	pts[k]=pts[0]; 
		M=k+1; 
 
			k=0; 
	for(i=0;iYB) c=4; 
		else if(pts[i].yYB) c=4; 
		else if(pts[i+1].yYT) c=0; 
		else if(ptse[i].yYT) c=0; 
		else if(ptse[i+1].yMoveTo(p[0]); 
	for(int j=1;j<=M;j++) 
	{ 
		pDC->LineTo(p[j]); 
	} 
	} 
} 
 
void CMyAssignmentView::OnCutEmer()  
{ 
	// TODO: Add your command handler code here 
m_cut=1; 
		CMainFrame *pMainFrame=(CMainFrame*)AfxGetMainWnd(); 
	pMainFrame->m_dbx=true; 
    pMainFrame->m_xainduan=true; 
	Invalidate();	 
} 
 
void CMyAssignmentView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)  
{ 
	// TODO: Add your message handler code here and/or call default 
	if(nChar=='X') 
	{ 
	CDC* pDC=GetDC(); 
	CPen newpen(PS_SOLID,3,RGB(255,128,0)); 
	CPen *old=pDC->SelectObject(&newpen); 
		flag=1;	 
	for(int i=0;iMoveTo(ptset[i]); 
	pDC->LineTo(ptset[i+1]); 
	i++; 
	} 
	} 
	else if(nChar=='D') 
	{ 
		CDC* pDC=GetDC(); 
	CPen newpen(PS_SOLID,3,RGB(128,128,0)); 
	CPen *old=pDC->SelectObject(&newpen); 
flag=2;	 
	pDC->MoveTo(ptset1[0]); 
	for(int i=1;i<5;i++) 
	{ 
		 
	pDC->LineTo(ptset1[i]); 
	 
	}} 
	CView::OnKeyDown(nChar, nRepCnt, nFlags); 
}