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

📄 txx4_9view.cpp

📁 给定四个点绘制图4-30所示的不同转角的两个正方形
💻 CPP
字号:
// txx4_9View.cpp : implementation of the CTxx4_9View class
//

#include "stdafx.h"
#include "txx4_9.h"

#include "txx4_9Doc.h"
#include "txx4_9View.h"

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

/////////////////////////////////////////////////////////////////////////////
// CTxx4_9View

IMPLEMENT_DYNCREATE(CTxx4_9View, CView)

BEGIN_MESSAGE_MAP(CTxx4_9View, CView)
	//{{AFX_MSG_MAP(CTxx4_9View)
	ON_WM_LBUTTONDOWN()
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CTxx4_9View construction/destruction

CTxx4_9View::CTxx4_9View()
{
	// TODO: add construction code here
	BkColor = RGB(255, 255, 255);
}

CTxx4_9View::~CTxx4_9View()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CTxx4_9View drawing

void CTxx4_9View::OnDraw(CDC* pDC)
{
	CTxx4_9Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	pDC->TextOut(0, 0, "点击鼠标左键选定种子点");
	DrawEdge(pDC, pDoc->plg, RGB(0, 0, 0));
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CTxx4_9View printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CTxx4_9View diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CTxx4_9View message handlers
void CTxx4_9View::DrawEdge(CDC *pDC, list<CPoint> &polygon, int color)
{
	list<CPoint>::iterator p_ite;

	p_ite= polygon.begin();
	pDC->MoveTo(p_ite->x, p_ite->y);
	for (++p_ite; p_ite != polygon.end(); ++p_ite)
		pDC->LineTo(p_ite->x, p_ite->y);
	p_ite = polygon.begin();
	pDC->LineTo(p_ite->x, p_ite->y);
}
bool CTxx4_9View::IsInside(CPoint point)
{
	CRect rect;
	CClientDC dc(this);
	int i, judge = 0;
	GetClientRect(&rect);
	for (i = point.x+1; i < rect.right; ++i)
		if (dc.GetPixel(i, point.y) != BkColor)
		{
			++judge;
			break;
		}

	for (i = point.x-1; i > rect.left; --i)
		if (dc.GetPixel(i, point.y) != BkColor)
		{
			++judge;
			break;
		}

	for (i = point.y+1; i < rect.bottom; ++i)
		if (dc.GetPixel(point.x, i) != BkColor)
		{
			++judge;
			break;
		}

	for (i = point.y-1; i > rect.top; ++i)
		if (dc.GetPixel(point.x, i) != BkColor)
		{
			++judge;
			break;
		}

	if (judge == 4) return true;
	else return false;
}
void CTxx4_9View::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	int drt[8][2] = {{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
	int i, x, y;
	CPoint cur;
	CClientDC dc(this);	
	if (IsInside(point))
	{
		stk.push(point);
		while (!stk.empty())
		{
			cur = stk.top();
			dc.SetPixel(cur.x, cur.y, RGB(0,0,255));
			stk.pop();
			for (i = 0; i < 8; ++i)
			{
				x = cur.x + drt[i][0];
				y = cur.y + drt[i][1];
				if (dc.GetPixel(x, y) == BkColor)
					stk.push(CPoint(x, y));
			}
		}
	}
}

⌨️ 快捷键说明

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