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

📄 crectangle.cs

📁 苏金明编写的《用VB.NET和VC#.NET开发交互式CAD系统》一书的源代码
💻 CS
字号:
using System;
using System.Drawing;

namespace VCSharp
{
	/// <summary>
	/// CRectangle 的摘要说明。
	/// </summary>
	public class CRectangle:CGElement
	{
		protected PointF m_basePos;
		protected PointF m_desPos;
		Module m=new Module();

		//绘矩形时的起点
		public PointF basePos
		{
			get{return m_basePos;}
			set{m_basePos=value;}
		}

		//绘矩形时的终点
		public PointF desPos
		{
			get{return m_desPos;}
			set{m_desPos=value;}
		}

		//矩形的左上角点
		public PointF LT
		{
			get
			{
			     return (new PointF(Math.Min(m_basePos.X, m_desPos.X),
                  Math.Max(m_basePos.Y, m_desPos.Y)));
			}
		}

		//矩形的右下角点
		public PointF RB
		{
			get
			{
				return (new PointF(Math.Max(m_basePos.X, m_desPos.X),
					Math.Min(m_basePos.Y, m_desPos.Y)));
			}
		}

		public CRectangle()
		{
			Init();
		}

		public CRectangle(PointF pBase,PointF pDes)
		{
			Init();
			m_basePos=pBase;
			m_desPos=pDes;
		}

		public CRectangle(CRectangle aRect)
		{
			m_basePos=aRect.basePos ;
			m_desPos=aRect.desPos ;
		}

		private new void Init()
		{
			base.Init();
			m_basePos=new PointF(0,0);
			m_desPos=new PointF(0,0);
		}

		//绘矩形
		override  public void Draw(Graphics g,DrawMode aDrawMode)
		{
			long aPen,oldP;

			//将控制点的坐标由世界坐标转换为页面坐标
			PointF eb=m.WorldtoPage(m_basePos);
			PointF ed=m.WorldtoPage(m_desPos);

			int minX, minY,maxX, maxY;
			minX = (int)(Math.Min(eb.X, ed.X));
			minY = (int)(Math.Min(eb.Y, ed.Y));
			maxX = (int)(Math.Max(eb.X, ed.X));
			maxY = (int)(Math.Max(eb.Y, ed.Y));

			//获得当前绘图环境的句柄
			IntPtr hdc=g.GetHdc();

			//设置画笔参数
			int[] penPara=DrawSettings(hdc,aDrawMode);
			//创建画笔
			aPen=Win32API.CreatePen(penPara[0],penPara[1],penPara[2]);
			//把画笔选入绘图环境,并返回原来的画笔
			oldP=Win32API.SelectObject(hdc,aPen);
			Win32API.LPPOINT prePos=new Win32API.LPPOINT();
			//把空刷子选入绘图环境
			Win32API.SelectObject(hdc, Win32API.GetStockObject(5));
			//绘制矩形
			Win32API.Rectangle(hdc, minX, maxY, maxX, minY);
			//把原来的画笔选入绘图环境
			Win32API.SelectObject(hdc, oldP);
			//删除新创建的画笔
			Win32API.DeleteObject(aPen);
			//释放绘图环境句柄
			g.ReleaseHdc(hdc);
		}

		//计算包围矩形
		override public CBox GetBox()
		{
			CBox aBox=new CBox();
			aBox.minX = this.LT.X;
			aBox.minY = this.RB.Y;
			aBox.maxX = this.RB.X;
			aBox.maxY = this.LT.Y;

			return aBox;
		}

		//拾取矩形
		override public bool Pick(PointF aPos) 
		{
			//首先把要拾取的矩形分解为四条直线段
			CLine line0=new CLine();
			CLine line1=new CLine();
			CLine line2=new CLine();
			CLine line3=new CLine();
			line0 = new CLine(this.LT, new PointF(this.LT.X, this.RB.Y));
			line1 = new CLine(new PointF(this.LT.X, this.RB.Y), this.RB);
			line2 = new CLine(this.RB, new PointF(this.RB.X, this.LT.Y));
			line3 = new CLine(new PointF(this.RB.X, this.LT.Y), this.LT);

			//如果有一条直线段被拾取,则整个矩形被拾取
			if (line0.Pick(aPos) ||
				line1.Pick(aPos) || 
				line2.Pick(aPos) || 
				line3.Pick(aPos))
			{
				return true;
			}
			else
			{
				return false;
			}
		}
	}
}

⌨️ 快捷键说明

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