?? 填充view.cpp
字號:
// 填充View.cpp : implementation of the CMyView class
//
#include "stdafx.h"
#include "填充.h"
#include "填充Doc.h"
#include "填充View.h"
typedef struct CNewPoint
{
//CPoint point;
int x;
int y;
int isflled;
//bool isfilled;
}CNewPoint;
#include <stack>
using namespace std;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMyView
IMPLEMENT_DYNCREATE(CMyView, CView)
BEGIN_MESSAGE_MAP(CMyView, CView)
//{{AFX_MSG_MAP(CMyView)
ON_WM_LBUTTONDOWN()
ON_WM_RBUTTONDOWN()
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CMyView construction/destruction
CMyView::CMyView()
{
// TODO: add construction code here
}
CMyView::~CMyView()
{
}
BOOL CMyView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMyView drawing
void CMyView::OnDraw(CDC* pDC)
{
CMyDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CMyView printing
BOOL CMyView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMyView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMyView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMyView diagnostics
#ifdef _DEBUG
void CMyView::AssertValid() const
{
CView::AssertValid();
}
void CMyView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMyDoc* CMyView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyDoc)));
return (CMyDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMyView message handlers
void CMyView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CDC *pDC;
pDC = GetDC();
int x,y;
x = point.x;
y = point.y;
COLORREF fill_color = RGB(0,0,255);
COLORREF boundaryvalue;//底板顏色
int x0,xl,xr,y0,xid;
bool flag;//
stack <CNewPoint> s;//操作數棧
CNewPoint point_fill ;//= new CNewPoint();
CNewPoint p;//定義一個點
point_fill.isflled = 1;//表示點未被畫過
point_fill.x = x;
point_fill.y = y;
boundaryvalue = pDC->GetPixel( x - 1, y);
s.push(point_fill);//將右鍵取得點(x,y)入棧
//********************當棧非空時循環
while(!s.empty())
{
p=s.top(); //將棧頂元素傳值給p
s.pop(); //彈出棧頂元素
x=p.x; //記錄點的x值
y=p.y; //記錄點的y值
// TRACE("pop x=%d,y=%d\n",x,y); //其功能相當與c中的printf,僅在MFC調試中有用
pDC->SetPixel(x,y,fill_color);//將點(x,y)填充
x0=x+1; //向右移一個像素
while(pDC->GetPixel(x0,y)==boundaryvalue&& pDC->GetPixel(x0,y) != fill_color)//當點(x0,y)的像素值等于底板像素值時,循環
{
pDC->SetPixel(x0 ,y ,fill_color); //將點(x0,y)填充
x0++; //向右移一個像素,循環--繼續填充,直至到邊界點
}
xr=x0-1;//xr為右邊界點的左邊一個點的x值,記錄用
x0=x-1; //x0變成原種子點p左一個點的x值
while(pDC->GetPixel(x0,y)==boundaryvalue && pDC->GetPixel(x0,y) != fill_color)//當點(x0,y)的像素值等于底板像素值時,循環
{
pDC->SetPixel(x0 ,y ,fill_color);//將點(x0,y)填充
x0--;//向左移一個像素,循環--繼續填充,直至到邊界點
}
xl=x0+1; //xl為左邊界點的右邊一個點的x值,記錄用
y0=y; //為y0賦值為y,與種子點的y值相等
for(int i=1;i>=-1;i-=2)//做兩次循環,之所以這樣定義賦值,因為下面有變量要用到i的值
{
// MessageBox("");
x0=xr; //將右邊界左邊一點的xr的值傳給x0
y=y0+i;//y的值先加1,再減1,表示點下移一行,在下次循環時上移一行
while(x0>=xl)//當x0>=xl時循環
{
flag=FALSE;
while((pDC->GetPixel(x0,y)==boundaryvalue)
&& (pDC->GetPixel(x0,y)!=fill_color)
&& (x0>=xl)) //當點(x0,y)沒被填充且不是邊界 且 x0>=xl時 ---循環
{
if(!flag) //如果flag為false
{
flag=TRUE; //將flag改為ture
xid=x0; //xid賦值為x0,xid作為記錄x0用
}
x0--; //點(x0,y) 每次循環左移一點
}
if(flag)//當flag為ture時
{
point_fill.y = y;
point_fill.x = xid;
s.push(point_fill); //將點(xid,y)入棧
// TRACE("push x=%d,y=%d\n",xid,y); //其功能相當與c中的printf,僅在MFC調試中有用
flag=FALSE; //然后在將flag置為false
}
while(((pDC->GetPixel(x0,y)!=boundaryvalue)&&(pDC->GetPixel(x0,y)!=fill_color)) ||
(pDC->GetPixel(x0,y) == fill_color))
{
if (x0 < 0)
{
break;
}
x0--;
}
//當點(x0,y)是邊界 或者 填充過時,(x0,y) 向左移一點
}//end of while(x0>=xl)
}//end of for(int i=1;i>=-1;i-=2)
}//end of while(!s.empty())
//**********************************
CView::OnLButtonDown(nFlags, point);
}
void CMyView::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CClientDC dc(this);
dc.Rectangle(CRect(10,10,100,100));
dc.Rectangle(CRect(15,15,125,125));
dc.Rectangle(CRect(30,30,130,130));
CView::OnRButtonDown(nFlags, point);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -