📄 scanlineseedfillingview.cpp
字号:
// ScanLineSeedFillingView.cpp : implementation of the CScanLineSeedFillingView class
//
#include "stdafx.h"
#include "ScanLineSeedFilling.h"
#include "ScanLineSeedFillingDoc.h"
#include "ScanLineSeedFillingView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CScanLineSeedFillingView
IMPLEMENT_DYNCREATE(CScanLineSeedFillingView, CView)
BEGIN_MESSAGE_MAP(CScanLineSeedFillingView, CView)
//{{AFX_MSG_MAP(CScanLineSeedFillingView)
ON_COMMAND(IDC_DRAWREGION, OnDrawregion)
ON_WM_LBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONUP()
ON_COMMAND(IDC_SEED, OnSeed)
ON_COMMAND(IDC_SCANNINGLINESEEDFILLING, OnScanninglineseedfilling)
ON_WM_RBUTTONDOWN()
ON_COMMAND(IDM_COLOR, OnColor)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CScanLineSeedFillingView construction/destruction
CScanLineSeedFillingView::CScanLineSeedFillingView()
{
// TODO: add construction code here
sign=0;top=-1;
m_bTracking=FALSE;
m_bCaptureEnabled=TRUE;
m_clr=RGB(255,0,0);
}
CScanLineSeedFillingView::~CScanLineSeedFillingView()
{
}
BOOL CScanLineSeedFillingView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CScanLineSeedFillingView drawing
void CScanLineSeedFillingView::OnDraw(CDC* pDC)
{
CScanLineSeedFillingDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CScanLineSeedFillingView printing
BOOL CScanLineSeedFillingView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CScanLineSeedFillingView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CScanLineSeedFillingView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CScanLineSeedFillingView diagnostics
#ifdef _DEBUG
void CScanLineSeedFillingView::AssertValid() const
{
CView::AssertValid();
}
void CScanLineSeedFillingView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CScanLineSeedFillingDoc* CScanLineSeedFillingView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CScanLineSeedFillingDoc)));
return (CScanLineSeedFillingDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CScanLineSeedFillingView message handlers
void CScanLineSeedFillingView::OnDrawregion()
{
// TODO: Add your command handler code here
sign=1;Invalidate(1);
}
void CScanLineSeedFillingView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (sign==1)
{
m_ptFrom=point;
m_ptTo=point;
m_bTracking=TRUE;
if (m_bCaptureEnabled)
{
SetCapture();
}
}
if (sign==2)
{
P=point;
CClientDC dc(this);
SetPixel(dc,P.x,P.y,RGB(255,0,0));
}
CView::OnLButtonDown(nFlags, point);
}
void CScanLineSeedFillingView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (m_bTracking&&sign==1)
{
CClientDC dc(this);
CPen pen(PS_SOLID,1,RGB(0,0,0));
CPen*pOldPen=dc.SelectObject(&pen);
dc.MoveTo(m_ptFrom);
dc.LineTo(point);
m_ptFrom=point;
}
CView::OnMouseMove(nFlags, point);
}
void CScanLineSeedFillingView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (m_bTracking)
{
m_bTracking=FALSE;
if (GetCapture()==this)
{
::ReleaseCapture();
}
// sign=0;
}
CView::OnLButtonUp(nFlags, point);
}
void CScanLineSeedFillingView::OnSeed()
{
// TODO: Add your command handler code here
sign=2;
}
void CScanLineSeedFillingView::OnScanninglineseedfilling()
{
// TODO: Add your command handler code here
sign=3;
CPoint ptFrom;
int x0,x,y,y0,xid,xleft,xright,flag,xnextspan;
Push(P);
while (Empty())
{
ptFrom=Pop();
CClientDC dc(this);
SetPixel(dc,ptFrom.x,ptFrom.y,m_clr);
x=ptFrom.x;y=ptFrom.y;
x0=x+1;
while(GetPixel(dc,x0,y)!=RGB(0,0,0)) //填充右方像素
{
SetPixel(dc,x0,y,m_clr);
x0++;
}
xright=x0-1; //最右边像素
x0=x-1;
while(GetPixel(dc,x0,y)!=RGB(0,0,0)) //填充左方像素
{
SetPixel(dc,x0,y,m_clr);
x0--;
}
xleft=x0+1; //最左边像素
//检查上一条扫描线和下一条扫描线,若存在非边界且未填充的像素,
//则选取代表各连续区间的种子像素入栈。
y0=y;
for(int i=1;i>=-1;i-=2)
{
x0=xright;
y=y0+i;
while(x0>=xleft)
{
flag=0;
while((GetPixel(dc,x0,y)!=RGB(0,0,0))
&& (GetPixel(dc,x0,y)!=m_clr)
&& (x0>xleft))
{
if(flag==0)
{
flag=1;
xid=x0;
}
x0--;
}
if(flag==1)
{
ptFrom.x=xid;ptFrom.y=y;
Push(ptFrom);
flag=0;
}
xnextspan=x0;
while((GetPixel(dc,x0,y)==RGB(0,0,0)) || (GetPixel(dc,x0,y)==m_clr) && (x0>=xleft))
x0--;
if(xnextspan==x0) x0--;
}
}
}
}
void CScanLineSeedFillingView::Push(CPoint e)
{
top++;
elem[top]=e;
}
CPoint CScanLineSeedFillingView::Pop()
{
CPoint P0;
if (top<=-1)
{
P0=0;
}
else
{
P0=elem[top];
top--;
}
return P0;
}
void CScanLineSeedFillingView::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
sign=0;
CView::OnRButtonDown(nFlags, point);
}
int CScanLineSeedFillingView::Empty()
{
if (top==-1)
{
return 0;
}
else
return 1;
}
void CScanLineSeedFillingView::OnColor()
{
// TODO: Add your command handler code here
CColorDialog dlg;
dlg.m_cc.Flags |=CC_RGBINIT|CC_FULLOPEN;
dlg.m_cc.rgbResult=m_clr;
if(IDOK==dlg.DoModal())
{
m_clr=dlg.m_cc.rgbResult;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -