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

📄 gdrawview.cpp

📁 VS2005 MFC 实现的画图程序
💻 CPP
📖 第 1 页 / 共 4 页
字号:
				 p0=p[(i+1)%count];
				 p1=p[i];
			}
			double r=(p1.y-p0.y)*1.0/(p1.x-p0.x);			
			double x;
			int y;
			for(int si=1,y=p0.y;si<=p1.y-p0.y;si++)
			{
				if(p1.x!=p0.x)
					x=p0.x*1.0+si*1.0/r;
				else
					x=p0.x;
				if(x-int(x)>0.49999) x+=1;
				pDC->SelectObject(&pen);
			    pDC->MoveTo((int)x,y);
				pDC->LineTo(rect.right,y);
				y++;
			}
	}
     pDC->SetROP2(R2_COPYPEN);
}

*/
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);
	int dx,dy;
	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);
	dx=rect.right-rect.left ;
	dy=rect.bottom-rect.top ;
	bool **pp=new bool *[dy ];
//	int v=sizeof( int);
	for(int k=0;k<=dy ;k++)
	{
		pp[k]=new bool [dx ];
	//	int s=dx*sizeof(bool);
		memset(pp[k],0,dx*sizeof(bool) );
	}
	for(int i=0;i<count;i++)
	{
		FillTra(pp,p[i],p[(i+1)%count],rect,fill_col);
	}
	for(int ii=0;ii<dy;ii++ )
		for(int jj=0;jj<dx ;jj++)
			if(pp[ii][jj])
			pDC->SetPixel (jj+rect.left,ii+rect.top ,fill_col); 
	for(int k=0;k<dy ;k++)
		delete []pp[k];
}

void CGDrawView::DrawLine(CDC *pDC,int bx,int by,int ex,int ey,COLORREF m_col,int lf,int size)
{
	if(size<=1)
		DrawLine(pDC, bx, by, ex, ey, m_col, lf);
	else
	{
		double w=(double)size,dx0=(double)(bx-ex),dy0=(double)(by-ey);
		double sin_a=(dy0)/sqrt(dx0*dx0+dy0*dy0);
		double cos_a=dx0/sqrt(dx0*dx0+dy0*dy0);
		double len=sqrt(dx0*dx0+dy0*dy0);
		double dx=sin_a*w/2.0;
		double dy=cos_a*w/2.0;
		Color col;
		col.SetFromCOLORREF (m_col);
		Graphics graph(pDC->m_hDC );
		PointF p[4]={PointF((REAL)bx-dx,(REAL)by+dy),PointF((REAL)bx+dx,(REAL)by-dy),
			PointF((REAL)ex+dx,(REAL)ey-dy),PointF((REAL)ex-dx,(REAL)ey+dy)};
		graph.FillPolygon (&SolidBrush(Color(col)),p,4);
		graph.FillEllipse  (&SolidBrush(Color(col)),RectF((REAL)bx-w/2,(REAL)by-w/2,w,w));
		graph.FillEllipse  (&SolidBrush(Color(col)),RectF((REAL)ex-w/2,(REAL)ey-w/2,w,w));
	}
}

void CGDrawView::DrawLine(CDC *pDC, int bx, int by, int ex, int ey, COLORREF m_col, int lf) 
{

	float x1=bx,x2=ex;float y1=by,y2=ey;	
	int count=0;
	if(bx==ex)
	{
		if(by>ey)
			for(int i=ey;i<=by;i++)
			{
				switch(lf)
				{
				case 1: count++;count%=20;
					if(count>9)
						continue;
					break;
				case 2: count++;count%=80;
					if(count>29 && count<40 || count>49 && count<60 || count>69)
						continue;
					break;
				}
				pDC->SetPixel (bx,i,m_col);
			}
		else
			for(int i=by;i<=ey;i++)
			{
				switch(lf)
				{
				case 1: count++;count%=20;
					if(count>9)
						continue;
					break;
				case 2: count++;count%=80;
					if(count>29 && count<40 || count>49 && count<60 || count>69)
						continue;
					break;
				}
				pDC->SetPixel (bx,i,m_col);	
			}
		return ;
	}
	if(by==ey)
	{
		if(bx<ex)
			for(int i=bx;i<=ex;i++)
			{
				switch(lf)
				{
				case 1: count++;count%=20;
					if(count>9)
						continue;
					break;
				case 2: count++;count%=80;
					if(count>29 && count<40 || count>49 && count<60 || count>69)
						continue;
					break;
				}
				pDC->SetPixel (i,by,m_col);
			}
		else
			for(int i=ex;i<=bx;i++)
			{
				switch(lf)
				{
				case 1: count++;count%=20;
					if(count>9)
						continue;
					break;
				case 2: count++;count%=80;
					if(count>29 && count<40 || count>49 && count<60 || count>69)
						continue;
					break;
				}
				pDC->SetPixel (i,by,m_col);
			}
		return ;
	}

	float tan_a=(y1-y2)/(x1-x2);
	float e,old_x,old_y;
	
	int dy,dx;
	if(tan_a <=1 && tan_a >=-1)
	{
		if(bx>ex)
		{
			x1=ex;x2=bx;
			y1=ey;y2=by;
		}
		if(tan_a<0)
			dy=-1;
		else
			dy=1;
		old_y=y1;
		for(int i=x1;i<x2;i++)
		{
			e=fabs(tan_a)*(i-x1)-fabs(old_y-y1);
			if(e>0.5)
			{
				old_y+=dy;
				switch(lf)
				{
				case 1: count++;count%=20;
					if(count>9)
						continue;
					break;
				case 2: count++;count%=80;
					if(count>29 && count<40 || count>49 && count<60 || count>69)
						continue;
					break;
				}
				pDC->SetPixel (i,old_y,m_col);
			}
			else
			{
				switch(lf)
				{
				case 1: count++;count%=20;
					if(count>9)
						continue;
					break;
				case 2: count++;count%=80;
					if(count>29 && count<40 || count>49 && count<60 || count>69)
						continue;
					break;
				}
				pDC->SetPixel (i,old_y,m_col);
			}
			
		}
	}
	else
	{
		if(by>ey)
		{
			x1=ex;x2=bx;
			y1=ey;y2=by;
		}
		if(tan_a<0)
			dx=-1;
		else
			dx=1;
		old_x=x1;
		for(int i=y1;i<=y2;i++)
		{
			e=((float)i-y1)/fabs(tan_a)-fabs(old_x-x1);
			if(e>=0.5)
			{
				old_x+=dx;
				switch(lf)
				{
				case 1: count++;count%=20;
					if(count>9)
						continue;
					break;
				case 2: count++;count%=80;
					if(count>29 && count<40 || count>49 && count<60 || count>69)
						continue;
					break;
				}
				pDC->SetPixel (old_x,i,m_col);
			}
			else
			{
				switch(lf)
				{
				case 1: count++;count%=20;
					if(count>9)
						continue;
					break;
				case 2: count++;count%=80;
					if(count>29 && count<40 || count>49 && count<60 || count>69)
						continue;
					break;
				}
				pDC->SetPixel (old_x,i,m_col);
			}
		}
	}
}

void CGDrawView::DrawLine11(CDC *pDC,int bx,int by,int ex,int ey,COLORREF m_col,int lf,int size)
{
	if(size<=1)
		DrawLine(pDC, bx, by, ex, ey, m_col, lf);
	else
	{
	    CPoint a=CPoint(bx,by);
		CPoint b=CPoint(ex,ey);
		CClientDC dc(this);
		OnPrepareDC(&dc);
		dc.LPtoDP(&a);
		dc.LPtoDP(&b);
		bx=a.x;by=a.y;
		ex=b.x;ey=b.y;
		double w=(double)size,dx0=(double)(bx-ex),dy0=(double)(by-ey);
		double sin_a=(dy0)/sqrt(dx0*dx0+dy0*dy0);
		double cos_a=dx0/sqrt(dx0*dx0+dy0*dy0);
		double len=sqrt(dx0*dx0+dy0*dy0);
		double dx=sin_a*w/2.0;
		double dy=cos_a*w/2.0;
		Color col;
		col.SetFromCOLORREF (m_col);
		Graphics graph(pDC->m_hDC );
		PointF p[4]={PointF((REAL)bx-dx,(REAL)by+dy),PointF((REAL)bx+dx,(REAL)by-dy),
			PointF((REAL)ex+dx,(REAL)ey-dy),PointF((REAL)ex-dx,(REAL)ey+dy)};
		graph.FillPolygon (&SolidBrush(Color(col)),p,4);
		graph.FillEllipse  (&SolidBrush(Color(col)),RectF((REAL)bx-w/2,(REAL)by-w/2,w,w));
		graph.FillEllipse  (&SolidBrush(Color(col)),RectF((REAL)ex-w/2,(REAL)ey-w/2,w,w));
	}
}

void CGDrawView::DrawLine11(CDC *pDC, int bx, int by, int ex, int ey, COLORREF m_col, int lf) 
{

	CPoint a=CPoint(bx,by);
	CPoint b=CPoint(ex,ey);
	CClientDC dc(this);
	OnPrepareDC(&dc);
	dc.LPtoDP(&a);
	dc.LPtoDP(&b);
	bx=a.x;by=a.y;
	ex=b.x;ey=b.y;

	float x1=bx,x2=ex;float y1=by,y2=ey;	
	int count=0;
	if(bx==ex)
	{
		if(by>ey)
			for(int i=ey;i<=by;i++)
			{
				switch(lf)
				{
				case 1: count++;count%=20;
					if(count>9)
						continue;
					break;
				case 2: count++;count%=80;
					if(count>29 && count<40 || count>49 && count<60 || count>69)
						continue;
					break;
				}
				pDC->SetPixel (bx,i,m_col);
			}
		else
			for(int i=by;i<=ey;i++)
			{
				switch(lf)
				{
				case 1: count++;count%=20;
					if(count>9)
						continue;
					break;
				case 2: count++;count%=80;
					if(count>29 && count<40 || count>49 && count<60 || count>69)
						continue;
					break;
				}
				pDC->SetPixel (bx,i,m_col);	
			}
		return ;
	}
	if(by==ey)
	{
		if(bx<ex)
			for(int i=bx;i<=ex;i++)
			{
				switch(lf)
				{
				case 1: count++;count%=20;
					if(count>9)
						continue;
					break;
				case 2: count++;count%=80;
					if(count>29 && count<40 || count>49 && count<60 || count>69)
						continue;
					break;
				}
				pDC->SetPixel (i,by,m_col);
			}
		else
			for(int i=ex;i<=bx;i++)
			{
				switch(lf)
				{
				case 1: count++;count%=20;
					if(count>9)
						continue;
					break;
				case 2: count++;count%=80;
					if(count>29 && count<40 || count>49 && count<60 || count>69)
						continue;
					break;
				}
				pDC->SetPixel (i,by,m_col);
			}
		return ;
	}

	float tan_a=(y1-y2)/(x1-x2);
	float e,old_x,old_y;
	
	int dy,dx;
	if(tan_a <=1 && tan_a >=-1)
	{
		if(bx>ex)
		{
			x1=ex;x2=bx;
			y1=ey;y2=by;
		}
		if(tan_a<0)
			dy=-1;
		else
			dy=1;
		old_y=y1;
		for(int i=x1;i<x2;i++)
		{
			e=fabs(tan_a)*(i-x1)-fabs(old_y-y1);
			if(e>0.5)
			{
				old_y+=dy;
				switch(lf)
				{
				case 1: count++;count%=20;
					if(count>9)
						continue;
					break;
				case 2: count++;count%=80;
					if(count>29 && count<40 || count>49 && count<60 || count>69)
						continue;
					break;
				}
				pDC->SetPixel (i,old_y,m_col);
			}
			else
			{
				switch(lf)
				{
				case 1: count++;count%=20;
					if(count>9)
						continue;
					break;
				case 2: count++;count%=80;
					if(count>29 && count<40 || count>49 && count<60 || count>69)
						continue;
					break;
				}
				pDC->SetPixel (i,old_y,m_col);
			}
			
		}
	}
	else
	{
		if(by>ey)
		{
			x1=ex;x2=bx;
			y1=ey;y2=by;
		}
		if(tan_a<0)
			dx=-1;
		else
			dx=1;
		old_x=x1;
		for(int i=y1;i<=y2;i++)
		{
			e=((float)i-y1)/fabs(tan_a)-fabs(old_x-x1);
			if(e>=0.5)
			{
				old_x+=dx;
				switch(lf)
				{
				case 1: count++;count%=20;
					if(count>9)
						continue;
					break;
				case 2: count++;count%=80;
					if(count>29 && count<40 || count>49 && count<60 || count>69)
						continue;
					break;
				}
				pDC->SetPixel (old_x,i,m_col);
			}
			else
			{
				switch(lf)
				{
				case 1: count++;count%=20;
					if(count>9)
						continue;
					break;
				case 2: count++;count%=80;
					if(count>29 && count<40 || count>49 && count<60 || count>69)
						continue;
					break;
				}
				pDC->SetPixel (old_x,i,m_col);
			}
		}
	}
}

BOOL CGDrawView::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
   if( zDelta<0)
    CScrollView::ScrollToPosition(pt);
   else 
	  CScrollView::ScrollToPosition(-pt);
	return CScrollView::OnMouseWheel(nFlags, zDelta, pt);
}

void CGDrawView::DrawTracks(CDC *pDC, Gdiplus::Point *p, int count, Gdiplus::Color col1, Gdiplus::Color col2) 
{
	Graphics gp(pDC->m_hDC );
	GraphicsPath path;
	REAL dash[2]={5.0f,5.0f};
	Pen pen(col1,5.0f),outpen(col2);
	pen.SetDashPattern (dash,2);
	
	//int tmpcount=count++;
	Point *pp=new Point[count+1];
	for(int ii=0;ii<count;ii++)
	{
		pp[ii]=p[ii];
	}
	pp[count] = p[0];
	//if(settop) 
		//path.AddCurve (pp,count+1,0.7);
	//else 
		path.AddCurve (p,count,0.7);

	gp.DrawPath (&pen,&path);
	path.Widen (&Pen(Color::Black ,5.0));
	path.Outline ();
	gp.DrawPath (&outpen,&path);
	//if(settop) 
		//gp.DrawLines(&Pen(Color::Blue),pp,count+1);
	//else 
		gp.DrawLines(&Pen(Color::Blue),p,count);
}

void CGDrawView::DrawBZ(CDC *pDC, Gdiplus::Point *p, int count, Gdiplus::Color col1, Gdiplus::Color col2)
{
	float t = 0.0;
	float x0, y0;
	DrawLine(pDC, p[0].X, p[0].Y, p[1].X, p[1].Y, RGB(255,255,255), 0);
	DrawLine(pDC, p[1].X, p[1].Y, p[2].X, p[2].Y, RGB(255,255,255), 0);
	for(t; t<=1; t+=0.01)
	{
		x0 = (1-t)*(1-t)*p[0].X+2*t*(1-t)*p[1].X+t*t*p[2].X;
		y0 = (1-t)*(1-t)*p[0].Y+2*t*(1-t)*p[1].Y+t*t*p[2].Y;
		pDC->SetPixel((int)x0, (int)y0, RGB(255,0,0));
	}
}

void CGDrawView::DrawBSample(CDC *pDC, Gdiplus::Point *p, int count, Gdiplus::Color col1, Gdiplus::Color col2)
{
	float u = 0.0;
	float x, y;
	int ii;
	POINT tpt;
	CRect rt;

	CBrush br(RGB(0,0,0));
	pDC->SelectObject(&br);
	//pDC->SetROP2(R2_XORPEN);
	//pDC->Ellipse(200,200,300,300);
	//pDC->Ellipse(220,220,280,280);

	//int **Regpts;
	for(ii=0; ii<count-1; ii++)
	{
		DrawLine(pDC, p[ii].X, p[ii].Y, p[ii+1].X, p[ii+1].Y, RGB(0,255,255), 0);
		if(ii>=1)
		{
			for(u=0.0; u<=1; u+=0.002)
			{
				x = 0.5*((u*u-2*u+1)*p[ii-1].X+(-2*u*u+2*u+1)*p[ii].X+u*u*p[ii+1].X);
				y = 0.5*((u*u-2*u+1)*p[ii-1].Y+(-2*u*u+2*u+1)*p[ii].Y+u*u*p[ii+1].Y);
				tpt.x = x;
				tpt.y = y;
				if(pDC->GetPixel((int)x, (int)y)!=RGB(0,0,0)||pDC->GetPixel((int)x, (int)y)!=RGB(0,0,0))
				{
					pDC->SetPixel(tpt, RGB(0,0,0));
					rt.left = x-5;
					rt.right = x+5;
					rt.bottom = y-5;
					rt.top = y+5;
					pDC->Ellipse(rt);
					/*rt.left = x-3;
					rt.right = x+3;
					rt.bottom = y-3;
					rt.top = y+3;
					pDC->Ellipse(rt);*/
					//DrawLine11(pDC, x, y, x, y, RGB(0,0,0), 0, 1);
					//DrawLine11(pDC, x, y, x, y, RGB(0,0,0), 0, 3);

					//DrawLine11(pDC, 300, 300, 400, 400, RGB(0,0,0), 0, 7);
					//DrawLine11(pDC, 300, 300, 400, 400, RGB(0,0,0), 0, 7);
				}
			}
		}
	}
}
void CGDrawView::OnRailway()
{
	// TODO: 在此添加命令处理程序代码
	rail=1;
	ctl=2;
	dline=0;
	breakline=0;
    select=0;
	fillset=0;
	type=3;
	if( ::MessageBox(NULL,L"是否手动输入铁路控制多边形顶点?(Y/N)",L"确定",1)!=IDOK)
	{
		fillset=1;
		point_num=0;
	}else{
        SetBuf();
	}
}

⌨️ 快捷键说明

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