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

📄 gdrawview.cpp

📁 VS2005 MFC 实现的画图程序
💻 CPP
📖 第 1 页 / 共 4 页
字号:
	CSize size=rect.Size();
	MainFrame->m_wndSplitter.SetRowInfo(0,(int)rect.Height()*5.2/6.0,0);
	MainFrame->m_wndSplitter.RecalcLayout();
	hide=0;
  }
}

void CGDrawView::OnUpdateHide(CCmdUI *pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
	if( hide )
	{
	    CString s=L"显示";
		pCmdUI->SetText(s);	

	}
	else 
	{	
		CString s=L"隐藏";
		pCmdUI->SetText(s);
	}
}


void CGDrawView::Serialize(CArchive& ar)
{

}

BOOL CGDrawView::OnEraseBkgnd(CDC* pDC)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	   CBrush backBrush(bg_color);
      CBrush* pOldBrush = pDC->SelectObject(&backBrush);
	  CRect rect;
      pDC->GetClipBox(&rect);   
      pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(), PATCOPY);
      pDC->SelectObject(pOldBrush);
      return TRUE;
	return CScrollView::OnEraseBkgnd(pDC);
}

void CGDrawView::OnUpdate(CScrollView* /*pSender*/, LPARAM /*lHint*/, CObject* /*pHint*/)
{
	// TODO: 在此添加专用代码和/或调用基类
}

void CGDrawView::OnInitialUpdate()
{
	CScrollView::OnInitialUpdate();
	CMainFrame* MainFrm=(CMainFrame*)AfxGetMainWnd();
	BgInfos* bg=(BgInfos*)MainFrm->m_wndSplitter.GetPane(1,0);
    CSize   sizeTotal;   
	sizeTotal.cx=bg->m_width;
	sizeTotal.cy=bg->m_height;   
    SetScrollSizes(MM_TEXT,sizeTotal);   

	// TODO: 在此添加专用代码和/或调用基类
}

void CGDrawView::OnLine()
{
	fillset=0;
    dline=0;
	breakline=0;
	dline=1;
	select=0;
	type=0;
	// TODO: 在此添加专用代码和/或调用基类
}

void CGDrawView::On_BreakLS()
{
 	fillset=0;
	dline=0;
	breakline=0;  
	breakline=1;
	select=0;
	type=0;
	// TODO: 在此添加专用代码和/或调用基类
}
BOOL CGDrawView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
  HCURSOR   lhCursor;   
   if( dline )
	{ 
		lhCursor=AfxGetApp()->LoadCursor(IDC_SDL);   
		SetCursor(lhCursor);
		return true;
	}
   else if( breakline )
   { 
		lhCursor=AfxGetApp()->LoadCursor(IDC_SDL);   
		SetCursor(lhCursor);
		return true;
   }
   else if( select )
   { 
		lhCursor=AfxGetApp()->LoadCursor(IDC_MOVE);   
		SetCursor(lhCursor);
		return true;
   }
   else if( settop )
   {
		lhCursor=AfxGetApp()->LoadCursor(IDC_SETTOPS);   
		SetCursor(lhCursor);
		return true;
   }
   else if( fillset )
   {
		lhCursor=AfxGetApp()->LoadCursor(IDC_FILL);   
		SetCursor(lhCursor);
		return true;
   }
   else if( choice )
   {
		lhCursor=AfxGetApp()->LoadCursor(IDC_DRAP);   
		SetCursor(lhCursor);
		return true;
   }
	return CScrollView::OnSetCursor(pWnd, nHitTest, message);
}



void CGDrawView::drawcircle(CDC *pDC, int bx, int by, int br,COLORREF m_col,int size) 
{//Draw a circle by using Bresenhan Algorithm
	int x,y,r,delta,delta1,delta2,direction;//表示内层的圆
	int x2,y2,r2,deta,deta1,deta2,direct;//表示外层的圆
    int oldx,oldx2,oldy,oldy2;
	r=br;//内层半径
	r2=br+size-1;//外层半径
	x=0;
	x2=0;
	y=r;
	y2=r2;
	delta=2*(1-r);
	deta=2*(1-r2);
	oldx=x; 
	oldx2=x2;
	oldy=y;
	oldy2=y2;
	while(y2>=0)
	{
		//绘制内层圆
		if(y>=0)
		{
			do
			{
				oldx=x;
				oldy=y;
				pDC->SetPixel(bx+x,by-y,m_col);
				pDC->SetPixel(bx+x,by+y,m_col);
				pDC->SetPixel(bx-x,by+y,m_col);
				pDC->SetPixel(bx-x,by-y,m_col);
				if(delta<0)
				{
					delta1=2*(delta+y)-1;
					if(delta1<=0) direction=1;
					else direction=2;
				}
				else if(delta>0)
				{
					delta2 =2*(delta-x)-1;
					if(delta2<=0) direction=2;
					else direction=3;
				}
				else 
					direction=2;
				switch(direction)
				{
				case 1:
					x++;
					delta+=2*x+1;
					break;
				case 2:
					x++;
					y--;
					delta+=2*(x-y+1);
					break;
				case 3:
					y--;
					delta+=(-2*y+1);
					break;
				}
			}while(oldx==x&&y>0);
			if(size>1){//填充
				CPen mypen(PS_SOLID,0,m_col);
				pDC->SelectObject(&mypen);
				pDC->MoveTo(bx+oldx,by-oldy);
				pDC->LineTo(bx+oldx2,by-oldy2);
				pDC->MoveTo(bx+oldx,by+oldy);
				pDC->LineTo(bx+oldx2,by+oldy2);
				pDC->MoveTo(bx-oldx,by-oldy);
				pDC->LineTo(bx-oldx2,by-oldy2);
				pDC->MoveTo(bx-oldx,by+oldy);
				pDC->LineTo(bx-oldx2,by+oldy2);
			}
		}
		if(size>1&&y<=0)
		{
			pDC->MoveTo(bx+oldx2,by-oldy2);
			pDC->LineTo(bx+oldx2,by+oldy2);
			pDC->MoveTo(bx-oldx2,by-oldy2);
			pDC->LineTo(bx-oldx2,by+oldy2);
		}
		do
		{
			//开始绘制外层圆
			oldx2=x2;
			oldy2=y2;
			pDC->SetPixel(bx+x2,by-y2,m_col);
			pDC->SetPixel(bx+x2,by+y2,m_col);
			pDC->SetPixel(bx-x2,by+y2,m_col);
			pDC->SetPixel(bx-x2,by-y2,m_col);
			if(deta<0)//外层圆的增量判断
			{
				deta1=2*(deta+y2)-1;
				if(deta1<=0) direct=1;
				else direct=2;
			}
			else if(deta>0)
			{
				deta2 =2*(deta-x2)-1;
				if(deta2<=0) direct=2;
				else direct=3;
			}
			else 
				direct=2;
			switch(direct)
			{
			case 1:
				x2++;
				deta+=2*x2+1;
				break;
			case 2:
				x2++;
				y2--;
				deta+=2*(x2-y2+1);
				break;
			case 3:
				y2--;
				deta+=(-2*y2+1);
				break;
			}
		}while(oldx2==x2&&y2>0);
	}
}

void CGDrawView::OnCircle() 
{
	CCircle dlg;
	if(dlg.DoModal ()==IDOK)
	{
		CPoint p[1]={CPoint(dlg.m_x,dlg.m_y)};
		CClientDC dc(this);
		OnPrepareDC(&dc);
		dc.DPtoLP(&p[0]);
		//CPoint p[1]={CPoint(dlg.m_x,dlg.m_y)};
		Graph_Union	circle(1,1,dlg.m_size,dlg.m_r,p,dlg.color,dlg.color);
		m_graphlist.push_back(circle);
		//Invalidate();
        CPoint a(circle.pp[0].x,circle.pp[0].y);
		dc.LPtoDP(&a);
		//dc.DPtoLP(&p[0]);
	    drawcircle(GetDC(),a.x,a.y,circle.shape,circle.color,circle.wide) ;

	}
	dline=0;
	breakline=0;
    select=0;
	fillset=0;
}


void CGDrawView::OnSelectGraph() 
{ 
	if( !hide )
	{
			CDC *pdc=GetDC();
			if( select && type==0)
			    deletepoint(pdc, *tmppre);
			ReleaseDC(pdc);
			CMainFrame*   MainFrame   =   (CMainFrame*)AfxGetMainWnd();	
			MainFrame->m_wndSplitter.DeleteView(1,0);
			MainFrame->m_wndSplitter.CreateView(1,0,RUNTIME_CLASS(BgInfos),CSize(400,10),NULL);
			MainFrame->m_wndSplitter.RecalcLayout();
			BgInfos* bg=(BgInfos*)MainFrame->m_wndSplitter.GetPane(1,0);
			CWnd *pWnd=bg->GetDlgItem(IDC_BG);
			CDC *pDC=pWnd->GetDC ();
			pDC->FillRect (CRect(1,1,89,46),&CBrush(bg_color));
			ReleaseDC(pDC);
			whichshow=0; 
	}
	dline=0;
	breakline=0;
    select=0;
	type=0;
	m_ptrGL=NULL;
	select=0; 
	choice=true;
}
void CGDrawView::On32775()
{
	// TODO: 在此添加命令处理程序代码
}


//填充---------------------


void CGDrawView::FillPoly(CDC *pDC,CPoint *points,int num, COLORREF &fill_col, COLORREF &line_col,int size) 
{
	bool eq=true;
	if(fill_col==line_col)
		fill_col+=1;
	else
		eq=false;
//	pDC->SetMapMode (R2_XORPEN);
	DrawPoly(pDC,points,num,line_col,0,size);
// return ;
//	stack<CPoint >sp;
	queue<CPoint >sp;
	int x,y;
	COLORREF temp;
	GetSeed(points,num,pDC,x,y);
	CPoint SeedPoint(30,30);
	sp.push (SeedPoint);
	int count=10000;
	while(!sp.empty () && count!=0)
	{
//		count--;
		x=sp.front().x;
		y=sp.front ().y;
		sp.pop ();
		pDC->SetPixel (x,y,fill_col);
		temp=pDC->GetPixel (x-1,y);
		if(temp!=line_col && temp!=fill_col)
		{
			CPoint t(x-1,y);
			sp.push (t);
			//pDC->SetPixel (t,fill_col);
		}
		temp=pDC->GetPixel (x,y-1);
		if(temp!=line_col && temp!=fill_col)
		{
			CPoint t(x,y-1);
			sp.push (t);
			//pDC->SetPixel (t,fill_col);
		}
		temp=pDC->GetPixel (x+1,y);
		if(temp!=line_col && temp!=fill_col)
		{
			CPoint t(x+1,y);
			sp.push (t);
			//pDC->SetPixel (t,fill_col);
		}		
		temp=pDC->GetPixel (x,y+1);
		if(temp!=line_col && temp!=fill_col)
		{
			CPoint t(x,y+1);
			sp.push (t);
			//pDC->SetPixel (t,fill_col);
		}
	}
	DrawPoly(pDC,points,num,line_col,0,size);
}
void CGDrawView::DrawPoly(CDC *pDC,CPoint *points,int num, COLORREF &line_col, int lf, int size)
{
	CPen pen(0,size,line_col);
	for(int i=0;i<num;i++)
	{
		pDC->SelectObject(&pen);
		pDC->MoveTo (points[i].x,points[i].y);
		pDC->LineTo (points[(i+1)%num].x,points[(i+1)%num].y);
	}//DrawLine(pDC,points[i].x,points[i].y,points[(i+1)%num].x,points[(i+1)%num].y,line_col,lf,size);
}
void CGDrawView::GetSeed(CPoint *points,int num, CDC *pDC, int &x, int &y) 
{

	CRgn PolyRegion;
	x=-1;y=-1;
	PolyRegion.CreatePolygonRgn (points,num,ALTERNATE);
	Region region(PolyRegion);
	int j=points[0].y+points[1].y;
	j/=2;
ag:
	for(int i=0;i<1024;i+=3)
	{
		if(region.IsVisible (i,j))
		{
			x=i;y=j;
			return;
		}
	}
	j++;
	goto ag;

}

void CGDrawView::re_fill(CDC *pDC, int x, int y, COLORREF line_col, COLORREF fill_col) 
{
/*	COLORREF temp=pDC->GetPixel (x,y);
	if(temp==line_col) return;
	if(temp!=fill_col)*/
	if(pDC->GetPixel (x,y)==line_col) return ;
	if(pDC->GetPixel (x,y)!=fill_col)//RGB(255,255,255))
	{
		pDC->SetPixel (x,y,fill_col);
		re_fill(pDC,x+1,y,line_col,fill_col);
		re_fill(pDC,x,y-1,line_col,fill_col);
		re_fill(pDC,x,y+1,line_col,fill_col);
		re_fill(pDC,x-1,y,line_col,fill_col);
	}
}
void CGDrawView::SeedFillP(CDC *pDC, int x, int y, COLORREF &fill_col, COLORREF &line_col)
{
	CPoint tempPoint(x,y);
	
	#define XX tempPoint.x
	#define YY tempPoint.y

	stack<CPoint> sp;
	int Xl,Xr,Xt,saveX,blankX;
	bool Pflag;
	COLORREF temp_col;
	sp.push (tempPoint);
	while(sp.size ())
	{
		tempPoint=sp.top ();
		sp.pop ();
		pDC->SetPixel (XX,YY,fill_col);
		saveX=XX;
		while(pDC->GetPixel(++XX,YY)!=line_col)
			pDC->SetPixel(XX,YY,fill_col);
		Xr=XX-1;
		XX=saveX;
		while(pDC->GetPixel (--XX,YY)!=line_col)
			pDC->SetPixel(XX,YY,fill_col);
		Xl=++XX;

		--YY;
		for(int i=0;i<2;i++)
		{
			while(XX<=Xr)
			{
				Pflag=0;
				temp_col=pDC->GetPixel (XX,YY);
				while(temp_col!=fill_col && temp_col!=line_col && XX++<Xr)
				{
					if(!Pflag)
						Pflag=1;
					temp_col=pDC->GetPixel (XX,YY);
				}

				if(Pflag)
				{
					if(Xr==XX && temp_col!=fill_col && temp_col!=line_col)
						sp.push (tempPoint);
					else
					{
						--XX;
						sp.push (tempPoint);
						++XX;
					}
					Pflag=0;
				}

				blankX=XX;
				temp_col=pDC->GetPixel (XX,YY);
				while(temp_col==line_col || temp_col==fill_col && XX<=Xr)
				{
					++XX;
					temp_col=pDC->GetPixel (XX,YY);
				}
				if(blankX==XX) ++XX;
				//++YY end
			}
			XX=Xl;
			YY+=2;
		}
	}
}

void CGDrawView::OnSetfil()
{
	// TODO: 在此添加命令处理程序代码
	rail=0;
	dline=0;
	breakline=0;
    select=0;
	fillset=0;
	//rail=0;

	CDC *pDC = GetDC();
	if( ::MessageBox(NULL,L"是否手动输入填充的多边形顶点?(Y/N)",L"确定",1)!=IDOK)
	{
		fillset=1;
		point_num=0;
	}
	else
	{
		SetBuf();	
	}
	ReleaseDC(pDC);
	
}

void CGDrawView::SetBuf()
{
	FilSet fil;
		int k;
	aa:
		if(fil.DoModal() == IDOK)
		{	
			if(fil.i<3)
			{
				MessageBox(L"点数不够!");
				fil.i = 0;

				goto aa;
			}else{
			    wchar_t buf[15];
				py.color = fil.col1;
				py.fill_color = fil.col;
				py.count = fil.i;
				py.pp = new CPoint [py.count];
				if(rail) py.type=3;
				else py.type=2;
				py.wide=1;
				int si;
				for(si=0,fil.w=fil.pc.begin();fil.w!=fil.pc.end();fil.w++)
					  py.pp[si++]=*fil.w;
				m_graphlist.push_back(py);
				Invalidate();
				//FillPoly_(pDC,py.pp,py.count,py.fill_color,py.color);
			}
		}
}

void CGDrawView::GetOutRect(CPoint *p, CPoint *pRect, int count) 
{
	int min_x=p[0].x,max_x=p[0].x,min_y=p[0].y,max_y=p[0].y;
	for(int i=1;i<count;i++)
	{
		if(min_x>p[i].x) min_x=p[i].x;
		if(max_x<p[i].x) max_x=p[i].x;
		if(min_y>p[i].y) min_y=p[i].y;
		if(max_y<p[i].y) max_y=p[i].y;
	}
	pRect[0].x=min_x;pRect[0].y=min_y;
	pRect[1].x=max_x;pRect[1].y=min_y;
	pRect[3].x=min_x;pRect[2].y=max_y;
	pRect[2].x=max_x;pRect[3].y=max_y;
}
void CGDrawView::FillTra(bool ** p, CPoint p0,CPoint p1,CRect &rect,COLORREF fill_col) 
{
	if(p0.y>p1.y)
	{
		CPoint temp(p0);
		p0=p1;p1=temp;
	}
	double dx=(double)p1.x-(double)p0.x;
	double dy=(double)p1.y-(double)p0.y;
	double r=sqrt(dx*dx+dy*dy);
	double sin_a=dy/r;
	double cos_a=dx/r;
	
	double x,y;
	int x0,y0;
	for(int i=1,y=(double)p0.y;i<=abs((int)dy);i++)
	{
		y+=1.0;
		if(p1.x!=p0.x)
			x=(double)p0.x+(double)i*cos_a/sin_a;
		else
			x=p0.x;
		int kkk=(int)x;
		if(x-kkk>0.49999) x+=1;
		y0=(int)y-rect.top ;
		x0=(int)x-rect.left  ;
		for(int j=x0;j<rect.right -rect.left ;j++)
				p[y0][j]=!p[y0][j];
	}
	
}
/*
void CGDrawView::FillPoly_(CDC *pDC, CPoint *p, int count, COLORREF fill_col, COLORREF line_col) 
{
	CPoint pRect[4];
	DrawPoly(pDC,p,count,line_col,fill_col,0);
	GetOutRect(p,pRect,count);
//	for(int j=0;j<4;j++)
	//DrawLine(pDC,pRect[j].x,pRect[j].y,pRect[(j+1)%4].x,pRect[(j+1)%4].y,line_col,0);
	CRect rect(pRect[0].x,pRect[0].y,pRect[2].x,pRect[2].y);
   pDC->SetROP2(R2_XORPEN);
	
	int i;
	CPen pen(0,1,fill_col);
	for( i=0;i<count;i++)
	 {	     CPoint p0=p[i],p1=p[(i+1)%count];
		    if(p[i].y>p[(i+1)%count].y)
			{

⌨️ 快捷键说明

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