using System.Drawing;
using System.Drawing.Drawing2D;
namespace RevitTools.DrawTool
{
///
/// 基本绘图对象
///
public abstract class DrawBase
{
internal Color m_BackColor;
internal Color m_ForeColor;
///
/// 背景色
///
public Color BackColor
{
get { return m_BackColor; }
set { m_BackColor = value; }
}
///
/// 前景色
///
public Color ForeColor
{
get { return m_ForeColor; }
set { m_ForeColor = value; }
}
///
/// 获取图形外接矩形
///
/// 图形的外接矩形
public abstract Rectangle GetBound();
///
/// 在指定的Graphics上绘制图形
///
/// 指定的 Graphics
public abstract void Draw(Graphics g);
///
/// 命中测试
///
///
public virtual bool HitTest(Point point)
{
var bound = GetBound();
if (bound.Left > point.X || bound.Right < point.X
|| bound.Top > point.Y || bound.Bottom > point.Y)
return false;
using (var bmp = new Bitmap(bound.Width, bound.Height))
using (var g = Graphics.FromImage(bmp))
{
var dx = -bound.X;
var dy = -bound.Y;
var obj = this.Clone();
obj.Draw(g);
return bmp.GetPixel(point.X + dx, point.Y + dy).A == 0;
}
}
///
/// 移动对象
///
/// X 偏移量
/// Y 偏移量
public abstract void Move(int dx, int dy);
public virtual DrawBase Clone()
{
return this.MemberwiseClone() as DrawBase;
}
}
public static class ExtensionMethods
{
public static Matrix SetTranslate(this Matrix matrix, float offsetX, float offsetY)
{
matrix.Translate(offsetX, offsetY);
return matrix;
}
public static Matrix SetTranslate(this Matrix matrix, float offsetX, float offsetY, MatrixOrder order)
{
matrix.Translate(offsetX, offsetY);
return matrix;
}
}
}