📄 graphicsalgorithmview.cpp
字号:
// GraphicsAlgorithmView.cpp : implementation of the CGraphicsAlgorithmView class
//
#include "stdafx.h"
#include "GraphicsAlgorithm.h"
#include "GraphicsAlgorithmDoc.h"
#include "GraphicsAlgorithmView.h"
#include "LWidth.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CGraphicsAlgorithmView
IMPLEMENT_DYNCREATE(CGraphicsAlgorithmView, CView)
BEGIN_MESSAGE_MAP(CGraphicsAlgorithmView, CView)
//{{AFX_MSG_MAP(CGraphicsAlgorithmView)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_COMMAND(idCurColor, OnidCurColor)
ON_COMMAND(idClear, OnidClear)
ON_COMMAND(idPolygon, OnidPolygon)
ON_COMMAND(idSetPolygon, OnidSetPolygon)
ON_COMMAND(IDCLOSE, OnClose)
ON_COMMAND(idFillColor, OnidFillColor)
ON_COMMAND(idLineWidth, OnidLineWidth)
ON_COMMAND(idabout, Onidabout)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CGraphicsAlgorithmView construction/destruction
CGraphicsAlgorithmView::CGraphicsAlgorithmView()
{
// TODO: add construction code here
// tBreLine=0;
// tDDALine=1;
}
CGraphicsAlgorithmView::~CGraphicsAlgorithmView()
{
}
BOOL CGraphicsAlgorithmView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CGraphicsAlgorithmView drawing
void CGraphicsAlgorithmView::OnDraw(CDC* Gra)
{
CGraphicsAlgorithmDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
Gra->SaveDC();
}
/////////////////////////////////////////////////////////////////////////////
// CGraphicsAlgorithmView printing
BOOL CGraphicsAlgorithmView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CGraphicsAlgorithmView::OnBeginPrinting(CDC* /*Gra*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CGraphicsAlgorithmView::OnEndPrinting(CDC* /*Gra*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CGraphicsAlgorithmView diagnostics
#ifdef _DEBUG
void CGraphicsAlgorithmView::AssertValid() const
{
CView::AssertValid();
}
void CGraphicsAlgorithmView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CGraphicsAlgorithmDoc* CGraphicsAlgorithmView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CGraphicsAlgorithmDoc)));
return (CGraphicsAlgorithmDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CGraphicsAlgorithmView message handlers
void CGraphicsAlgorithmView::OnInitialUpdate()
{
CView::OnInitialUpdate();
// TODO: Add your specialized code here and/or call the base class
Gra=new CClientDC(this);
Curcolor=RGB(255,0,255);
FillColor=RGB(255,0,255);
CurPen.CreatePen(PS_SOLID,3,Curcolor);
OrgPen.CreatePen(PS_SOLID,3,RGB(255,0,255));
MouDown=false;
FunSel=-1;
}
void CGraphicsAlgorithmView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
MouDown=true;
switch(FunSel)
{
case 0:
break;
case 1:
BegP=point;
if(PNum==0)
{
ContP[PNum++]=point;
}else{
BegP=ContP[PNum-1];
}
PreP=BegP;
// pen1.CreatePen(PS_SOLID,1,RGB(255,0,0));
Gra->SetROP2(R2_XORPEN);
Gra->SelectObject(&OrgPen);
Gra->MoveTo(BegP.x-10,BegP.y);
Gra->LineTo(BegP.x+10,BegP.y);
Gra->MoveTo(BegP.x,BegP.y-10);
Gra->LineTo(BegP.x,BegP.y+10);
Gra->MoveTo(BegP);
Gra->LineTo(PreP);
break;
case 2:
break;
case 3:
// pen1.CreatePen(PS_SOLID,1,RGB(255,0,0));
Gra->SetROP2(R2_XORPEN);
Gra->SelectObject(&CurPen);
BegP=point;PreP=point;
Gra->MoveTo(BegP.x-10,BegP.y);
Gra->LineTo(BegP.x+10,BegP.y);
Gra->MoveTo(BegP.x,BegP.y-10);
Gra->LineTo(BegP.x,BegP.y+10);
Gra->MoveTo(BegP);
Gra->LineTo(PreP);
break;
}
CView::OnLButtonDown(nFlags, point);
}
void CGraphicsAlgorithmView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
MouDown=false;
switch(FunSel)
{
case 0:
break;
case 1:
Gra->SetROP2(R2_XORPEN);
Gra->MoveTo(BegP.x-10,BegP.y);
Gra->LineTo(BegP.x+10,BegP.y);
Gra->MoveTo(BegP.x,BegP.y-10);
Gra->LineTo(BegP.x,BegP.y+10);
Gra->MoveTo(BegP);
Gra->LineTo(point);
// pen1.CreatePen(PS_SOLID,1,RGB(255,0,0));
Gra->SetROP2(R2_COPYPEN);
Gra->SelectObject(&CurPen);
Gra->MoveTo(BegP);
Gra->LineTo(point);
ContP[PNum++]=point;
// Gra->Ellipse(point.x-10,point.y-10,point.x+10,point.y+10);
break;
case 2:
FloodFill4(point,FillColor,Curcolor);
break;
case 3:
Gra->SetROP2(R2_XORPEN);
Gra->MoveTo(BegP.x-10,BegP.y);
Gra->LineTo(BegP.x+10,BegP.y);
Gra->MoveTo(BegP.x,BegP.y-10);
Gra->LineTo(BegP.x,BegP.y+10);
Gra->MoveTo(BegP);
Gra->LineTo(PreP);
// pen1.CreatePen(PS_SOLID,1,RGB(255,0,0));
Gra->SetROP2(R2_COPYPEN);
Gra->SelectObject(&OrgPen);
Gra->MoveTo(BegP);
Gra->LineTo(point);
break;
}
CView::OnLButtonUp(nFlags, point);
}
void CGraphicsAlgorithmView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(MouDown){
switch(FunSel)
{
case 0:
break;
case 1:
Gra->MoveTo(BegP);
Gra->LineTo(PreP);
PreP=point;
Gra->MoveTo(BegP);
Gra->LineTo(PreP);
break;
case 2:
break;
case 3:
break;
}
}
CView::OnMouseMove(nFlags, point);
}
void CGraphicsAlgorithmView::OnidFloodFilll4()
{
// TODO: Add your command handler code here
FunSel=1;//四连通区域填充
}
void CGraphicsAlgorithmView::FloodFill4(CPoint SeedP, COLORREF FillColor, COLORREF BdColor)
{
j=0;
CPoint temp; //当线条比较细时,往往在边界处会出错,
int xl,xr,y; //原因不是算法本身造成的,是由于画曲线多边形
int x,z; //的算法本身有问题,需进一步改进!
int xsave;
int i=0;
temp=SeedP+CPoint(1,0);
if(Gra->GetPixel(SeedP)!=BdColor)
{
while(Gra->GetPixel(SeedP)!=BdColor)
{
Gra->SetPixel(SeedP,FillColor);
SeedP=SeedP-CPoint(1,0);
}
Three[0][0]=SeedP.x+1;
while(Gra->GetPixel(temp)!=BdColor)
{
Gra->SetPixel(temp,FillColor);
temp=temp+CPoint(1,0);
}
Three[0][1]=temp.x-1;
Three[0][2]=temp.y;
}
while(j>=0)
{
xl=Three[j][0];
xr=Three[j][1];
y=Three[j][2];
j--;
for(i=0;i<=1;i++)
{
x=xl;
z=xr;
switch(i)
{
case 0: y=y+1;
break;
case 1: y=y-2;
break;
}
if(Gra->GetPixel(x,y)!=BdColor && Gra->GetPixel(x,y)!=FillColor)
{
while(Gra->GetPixel(x,y)!=BdColor && Gra->GetPixel(x,y)!=FillColor)
{
Gra->SetPixel(CPoint(x,y),FillColor);
x--;
}
j++;
Three[j][0]=x+1;
x=xl+1;
while(Gra->GetPixel(x,y)!=BdColor && Gra->GetPixel(x,y)!=FillColor)
{
Gra->SetPixel(CPoint(x,y),FillColor);
x++;
}
Three[j][1]=x-1;
Three[j][2]=y;
Right(x-1,z,y,FillColor,BdColor);
}
else
{
while(Gra->GetPixel(x,y)==BdColor||Gra->GetPixel(x,y)==FillColor)
{
x++;
}
if(x<=z)
{
j++;
xsave=x;
Three[j][0]=xsave;
while(Gra->GetPixel(x,y)!=BdColor && Gra->GetPixel(x,y)!=FillColor)
{
Gra->SetPixel(CPoint(x,y),FillColor);
x++;
}
Three[j][1]=x-1;
Three[j][2]=y;
Right(x-1,z,y,FillColor,BdColor);
}
}
}
}
}
void CGraphicsAlgorithmView::OnidCurColor()
{
// TODO: Add your command handler code here
CColorDialog cdlg;
if(cdlg.DoModal()==IDOK)
{
Curcolor=cdlg.GetColor();
CurPen.DeleteObject();
CurPen.CreatePen(PS_SOLID,3,Curcolor);
}
}
void CGraphicsAlgorithmView::OnidClear()
{
// TODO: Add your command handler code here
FunSel=0;
CRect rc;
GetClientRect(rc);
Gra->FillSolidRect(rc,Gra->GetBkColor());
}
void CGraphicsAlgorithmView::OnidPolygon()
{
// TODO: Add your command handler code here
FunSel=1;PNum=0;
}
void CGraphicsAlgorithmView::OnidSetPolygon()
{
// TODO: Add your command handler code here
FunSel=2;
static bool First=1;
if(First)
{
First=0;
MessageBox("放心点吧,理论上都是能够填充的!!!");
}
}
void CGraphicsAlgorithmView::OnClose()
{
// TODO: Add your command handler code here
FunSel=5;
Gra->MoveTo(ContP[PNum-1]);
Gra->LineTo(ContP[0]);
}
void CGraphicsAlgorithmView::OnidFillColor()
{
// TODO: Add your command handler code here
CColorDialog cdlg;
if(cdlg.DoModal()==IDOK)
{
FillColor=cdlg.GetColor();
}
}
void CGraphicsAlgorithmView::OnidLineWidth()
{
// TODO: Add your command handler code here
LWidth ldlg;
ldlg.DoModal();
CurPen.DeleteObject();
CurPen.CreatePen(PS_SOLID,ldlg.m_LWidth,Curcolor);
}
void CGraphicsAlgorithmView::Onidabout()
{
// TODO: Add your command handler code here
MessageBox(" 本算法可以填充比较大的区域!");
}
void CGraphicsAlgorithmView::Right(int nxr, int xr, int y,COLORREF FillColor, COLORREF BdColor)
{
/* int xsave;
xsave=nxr;
while(nxr<xr)
{
while(Gra->GetPixel(nxr,y)!=BdColor && Gra->GetPixel(nxr,y)!=FillColor)
{
if(!span_need_fill)
{
span_need_fill=true;
if(!left_need_fill)
{
xsave=nxr+1;
}
while(Gra->GetPixel(nxr,y)!=BdColor && Gra->GetPixel(nxr,y)!=FillColor)
{
Gra->SetPixel(CPoint(nxr,y),FillColor);
nxr++;
}
}
j++;
Three[j][0]=xsave;
Three[j][1]=nxr;
Three[j][2]=y;
left_need_fill=false;
while((Gra->GetPixel(nxr+1,y)==BdColor || Gra->GetPixel(nxr+1,y)!=FillColor) && (nxr<xr))
{
nxr++;
}
} */
nxr=nxr+2;
while(nxr<xr)
{
if(Gra->GetPixel(nxr,y)==BdColor)
{
nxr=nxr+1;
while(Gra->GetPixel(nxr,y)==BdColor)
{
nxr++;
}
if(nxr<xr)
{
j++;
Three[j][0]=nxr;
while(Gra->GetPixel(nxr,y)!=BdColor && Gra->GetPixel(nxr,y)!=FillColor)
{
Gra->SetPixel(CPoint(nxr,y),FillColor);
nxr++;
}
nxr=nxr-1;
Three[j][1]=nxr;
Three[j][2]=y;
nxr=nxr+2;
}
}
else
{
j++;
Three[j][0]=nxr;
while(Gra->GetPixel(nxr,y)!=BdColor && Gra->GetPixel(nxr+1,y)!=FillColor)
{
Gra->SetPixel(CPoint(nxr,y),FillColor);
nxr++;
}
nxr--;
Three[j][1]=nxr;
Three[j][2]=y;
nxr=nxr+2;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -