using PaintDotNet.Base.SettingModel;
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using PaintDotNet.Annotation.Enum;
using System.Collections.Generic;
using PaintDotNet.Base.CommTool;
using System.Linq;
namespace PaintDotNet.Annotation.Measure
{
///
/// 测量->多边形测量->任意正方形
///
public class MeasureRandSquare : MeasureDrawObject
{
///
/// 标注样式信息model
///
public MeasureStyleModel.MeasureRandSquare measureRandSquare;
///
/// 坐标点集合
///
public List pointArray;
///
/// 第三个点
///
PointF linePoint;
///
/// 第四个点
///
PointF linePoint1;
///
/// 中心点
///
PointF centerPoint;
///
/// 旋转时的最后一个点记录,用于计算角度
///
PointF lastPoint;
///
/// 旋转时的标记
///
public bool isPress = false;
///
/// 重新计算过的外接矩形
///
RectangleF largerRect;
///
/// 周长
///
//private double length = 0.0;
///
/// 面积
///
private double area = 0.0;
///
/// 测量信息矩形定义
///
private RectangleF rectangleF1 = new RectangleF();
private RectangleF rectangleF2 = new RectangleF();
private RectangleF rectangleF3 = new RectangleF();
private RectangleF rectangleF4 = new RectangleF();
private RectangleF rectangleF5 = new RectangleF();
private RectangleF rectangleF6 = new RectangleF();
private RectangleF rectangleF7 = new RectangleF();
private RectangleF rectangleF8 = new RectangleF();
private RectangleF rectangleF9 = new RectangleF();
private RectangleF rectangleF10 = new RectangleF();
private RectangleF rectangleF11 = new RectangleF();
///
/// 文本上用于拖动的点
///
private Point pointL = new Point();
///
/// 绘制限制(第一次绘制时)
///
public bool pointChange = true;
///
/// 区分移动文本
///
private int moveKb;
///
/// 绘制限制(从配置文件加载)
///
public bool configurationFile = false;
///
/// 绘制限制(更改属性时)
///
private bool SavePointChange;
///
/// 绘制属性
///
private string[] drawingPropertiesList;
///
/// 绘制属性(克隆)
///
private string[] drawingPropertiesListClone;
private bool drawing;
///
/// 旋转角度
///
private double angle;
///
/// 绘制点
///
private PointF PointF = new PointF();
///
/// 是否移动了鼠标
///
public bool moved = false;
///
/// 限制绘制(绘制中)
///
public bool mouseUpPointChange = false;
public MeasureRandSquare(ISurfaceBox surfaceBox, int x1, int y1, int x2, int y2, bool clone) : base()
{
this.objectType = DrawClass.Measure;
this.drawToolType = DrawToolType.MeasureRandSquare;
this.pointArray = new List();
this.measureRandSquare = surfaceBox.GetMeasureStyleModel().measureRandSquare;
this.startPoint.X = x1;
this.startPoint.Y = y1;
this.endPoint.X = x2;
this.endPoint.Y = y2;
if (clone)
{
drawing = true;
this.pointArray.Add(this.startPoint);
this.pointArray.Add(this.endPoint);
}
this.measurementUnit = (MeasurementUnit)System.Enum.Parse(typeof(MeasurementUnit), surfaceBox.GetPxPerUnit()[0]);
this.unitString = surfaceBox.GetPxPerUnit()[1];
this.unit = surfaceBox.GetPxPerUnit()[2];
}
///
/// 测量属性
///
///
public override Dictionary GetData()
{
if (data.ContainsKey(MeasureAttributes.MeasureMethod))
data[MeasureAttributes.MeasureMethod] = PdnResources.GetString("Menu.MeasureAction.PolygonMeasurement.Text");
else
data.Add(MeasureAttributes.MeasureMethod, PdnResources.GetString("Menu.MeasureAction.PolygonMeasurement.Text"));
if (data.ContainsKey(MeasureAttributes.MeasureUnitCN))
data[MeasureAttributes.MeasureUnitCN] = this.unitString;
else
data.Add(MeasureAttributes.MeasureUnitCN, this.unitString);
if (data.ContainsKey(MeasureAttributes.MeasureUnitEN))
data[MeasureAttributes.MeasureUnitEN] = this.unit;
else
data.Add(MeasureAttributes.MeasureUnitEN, this.unit);
if (data.ContainsKey(MeasureAttributes.PixelStartX))
data[MeasureAttributes.PixelStartX] = startPoint.X;
else
data.Add(MeasureAttributes.PixelStartX, startPoint.X);
if (data.ContainsKey(MeasureAttributes.PixelStartY))
data[MeasureAttributes.PixelStartY] = startPoint.Y;
else
data.Add(MeasureAttributes.PixelStartY, startPoint.Y);
if (data.ContainsKey(MeasureAttributes.PhysicalStartX))
data[MeasureAttributes.PhysicalStartX] = Math.Round(startPoint.X * unitLength, decimalPlaces);
else
data.Add(MeasureAttributes.PhysicalStartX, Math.Round(startPoint.X * unitLength, decimalPlaces));
if (data.ContainsKey(MeasureAttributes.PhysicalStartY))
data[MeasureAttributes.PhysicalStartY] = Math.Round(startPoint.Y * unitLength, decimalPlaces);
else
data.Add(MeasureAttributes.PhysicalStartY, Math.Round(startPoint.Y * unitLength, decimalPlaces));
if (data.ContainsKey(MeasureAttributes.PixelArea))
data[MeasureAttributes.PixelArea] = Math.Round(area, decimalPlaces);
else
data.Add(MeasureAttributes.PixelArea, Math.Round(area, decimalPlaces));
string s = Math.Round(this.area * unitLength, decimalPlaces).ToString();
if (s.IndexOf(".") == -1)
{
for (int i = 0; i < decimalPlaces; i++)
{
if (i == 0)
s += ".";
s += "0";
}
}
else
{
int a = s.Length - s.IndexOf(".") - 1;
if (a < decimalPlaces)
{
for (int i = 0; i < decimalPlaces - a; i++)
{
s += "0";
}
}
}
if (data.ContainsKey(MeasureAttributes.PhysicalArea))
data[MeasureAttributes.PhysicalArea] = s;
else
data.Add(MeasureAttributes.PhysicalArea, s);
if (data.ContainsKey(MeasureAttributes.PixelCircumference))
data[MeasureAttributes.PixelCircumference] = Math.Round(length, decimalPlaces);
else
data.Add(MeasureAttributes.PixelCircumference, Math.Round(length, decimalPlaces));
s = Math.Round(this.length * unitLength, decimalPlaces).ToString();
if (s.IndexOf(".") == -1)
{
for (int i = 0; i < decimalPlaces; i++)
{
if (i == 0)
s += ".";
s += "0";
}
}
else
{
int a = s.Length - s.IndexOf(".") - 1;
if (a < decimalPlaces)
{
for (int i = 0; i < decimalPlaces - a; i++)
{
s += "0";
}
}
}
if (data.ContainsKey(MeasureAttributes.PhysicalCircumference))
data[MeasureAttributes.PhysicalCircumference] = s;
else
data.Add(MeasureAttributes.PhysicalCircumference, s);
return data;
}
public MeasureRandSquare(ISurfaceBox surfaceBox, List points, ParentStyleModel parentStyleModel, Object content) : base()
{
this.objectType = DrawClass.Measure;
this.drawToolType = DrawToolType.MeasureRandSquare;
this.ISurfaceBox = surfaceBox;
measureRandSquare = (MeasureStyleModel.MeasureRandSquare)parentStyleModel;
pointArray = DrawRulerHelper.DeepCopyListByReflect(points);
startPoint.X = points[0].X;
startPoint.Y = points[0].Y;
endPoint.X = points[1].X;
endPoint.Y = points[1].Y;
linePoint.X = points[2].X;
linePoint.Y = points[2].Y;
linePoint1.X = points[3].X;
linePoint1.Y = points[3].Y;
CalcOtherRectangle();
CalcCenter();
largerRect = GetBoundingBox();
this.measurementUnit = (MeasurementUnit)System.Enum.Parse(typeof(MeasurementUnit), surfaceBox.GetPxPerUnit()[0]);
this.unitString = surfaceBox.GetPxPerUnit()[1];
this.unit = surfaceBox.GetPxPerUnit()[2];
this.configurationFile = true;
this.moved = true;
}
///
/// Clone this instance
///
public override DrawObject Clone()
{
MeasureRandSquare measureRandSquare = new MeasureRandSquare(ISurfaceBox, 0, 0, 0, 1, false);
measureRandSquare.objectType = DrawClass.Measure;
measureRandSquare.drawToolType = DrawToolType.MeasureRandSquare;
measureRandSquare.ISurfaceBox = this.ISurfaceBox;
foreach (PointF p in this.pointArray)
{
measureRandSquare.pointArray.Add(p);
}
measureRandSquare.startPoint = this.startPoint;
measureRandSquare.endPoint = this.endPoint;
measureRandSquare.linePoint = this.linePoint;
measureRandSquare.linePoint1 = this.linePoint1;
if (drawingPropertiesList != null)
measureRandSquare.drawingPropertiesListClone = drawingPropertiesList;
measureRandSquare.moved = true;
FillDrawObjectFields(measureRandSquare);
return measureRandSquare;
}
public override DrawObject Clone(ISurfaceBox surfaceBox)
{
MeasureRandSquare measureRandSquare = new MeasureRandSquare(surfaceBox, this.GetPoints(), this.measureRandSquare, null);
if (drawingPropertiesList != null)
measureRandSquare.drawingPropertiesListClone = drawingPropertiesList;
measureRandSquare.moved = true;
FillDrawObjectFields(measureRandSquare);
return measureRandSquare;
}
public override void Draw(Graphics g)
{
if(this.moved)
{
this.measureRandSquare = this.ISurfaceBox.GetMeasureStyleModel().measureRandSquare;
drawingProperties.TryGetValue(this.drawToolType, out drawingPropertiesList);
if (drawingPropertiesList == null)
{
drawingPropertiesList = drawingPropertiesListClone;
if (!this.drawing)
this.configurationFile = true;
}
pointChangeObject.TryGetValue(this.drawToolType, out SavePointChange);
g.SmoothingMode = SmoothingMode.AntiAlias;
Font textfont = new Font(this.measureRandSquare.font, this.measureRandSquare.fontSize);
Brush textbrush = new SolidBrush(Color.FromArgb(this.measureRandSquare.textColor));
Color color = Color.FromArgb(this.measureRandSquare.lineColor);
Pen pen = new Pen(color, this.measureRandSquare.lineWidth);
//Pen pen = new Pen(color, 1);
pen.DashStyle = (DashStyle)this.measureRandSquare.lineStyle;
if (this.Selected)
{
if (measureRandSquare.chooseStyle != null)
{
textfont = new Font(measureRandSquare.chooseStyle.font, measureRandSquare.chooseStyle.fontSize);
textbrush = new SolidBrush(Color.FromArgb(measureRandSquare.chooseStyle.textColor));
pen = new Pen(Color.FromArgb(measureRandSquare.chooseStyle.lineColor), measureRandSquare.chooseStyle.lineWidth);
pen.DashStyle = (DashStyle)measureRandSquare.chooseStyle.lineStyle;
}
}
if (!this.Selected)
{
this.pointChange = true;
rectangleF1 = new RectangleF();
rectangleF2 = new RectangleF();
rectangleF3 = new RectangleF();
rectangleF4 = new RectangleF();
rectangleF5 = new RectangleF();
rectangleF6 = new RectangleF();
rectangleF7 = new RectangleF();
rectangleF8 = new RectangleF();
rectangleF9 = new RectangleF();
rectangleF10 = new RectangleF();
rectangleF11 = new RectangleF();
}
Matrix mtxSave = g.Transform;
Matrix matrix = g.Transform;
string frontStr = string.Empty;
if (this.measureRandSquare.showSuffix)
{
frontStr = this.measureRandSquare.suffixName;
}
if (this.measureRandSquare.showSerial)
{
#region [获取序号]
List newGraphicsList = new List();
int n = ISurfaceBox.GraphicsList.Count;
int num = 0;
for (int i = n - 1; i >= 0; i--)
{
newGraphicsList.Add(ISurfaceBox.GraphicsList[i].Clone());
}
var newGraphics = newGraphicsList.Where(m => m.ID.Equals(this.ID)).FirstOrDefault();
if (newGraphics != null)
{
num = newGraphicsList.IndexOf(newGraphics) + 1;
}
#endregion
frontStr = $"[{ num }]{frontStr}";
}
if (this.measureRandSquare.showAlias)
{
frontStr = this.measureRandSquare.aliasName + frontStr;
}
if (pointArray.Count > 3)
{
SizeF sizeF = new SizeF();
g.DrawLine(pen, pointArray[0].X, pointArray[0].Y, pointArray[2].X, pointArray[2].Y);
g.DrawLine(pen, pointArray[2].X, pointArray[2].Y, pointArray[1].X, pointArray[1].Y);
g.DrawLine(pen, pointArray[1].X, pointArray[1].Y, pointArray[3].X, pointArray[3].Y);
g.DrawLine(pen, pointArray[3].X, pointArray[3].Y, pointArray[0].X, pointArray[0].Y);
// 计算矩形长和宽
double h = BasicCalculationHelper.GetDistance(pointArray[0], pointArray[2], 10);
double w = BasicCalculationHelper.GetDistance(pointArray[0], pointArray[3], 10);
length = (h + w) * 2;
area = h * w;
// 是否绘制
if (this.pointChange || this.SavePointChange || this.mouseUpPointChange || this.mouseUpAttribute)
{
// 属性文本的间隔定义
int offsetValue = measureRandSquare.fontSize + measureRandSquare.fontSize / 2;
int offsetValue1 = measureRandSquare.lineWidth * 2 / 3;
int offset = offsetValue1;
this.pointL = new Point((int)pointArray[0].X, (int)pointArray[0].Y);
if (drawingPropertiesList != null)
{
// 像素周长
if (drawingPropertiesList.Contains(MeasureAttributes.PixelCircumference.ToString()))
{
sizeF = g.MeasureString(frontStr + "" + Math.Round(this.length, decimalPlaces) + "px", textfont);
rectangleF10.Location = new Point((int)this.PointF.X + offsetValue1 - (int)sizeF.Width / 2, (int)this.PointF.Y + offset);
offset += offsetValue;
}
// 物理周长
if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalCircumference.ToString()))
{
string s = Math.Round(this.length * unitLength, decimalPlaces).ToString();
if (s.IndexOf(".") == -1)
{
for (int i = 0; i < decimalPlaces; i++)
{
if (i == 0)
s += ".";
s += "0";
}
}
else
{
int a = s.Length - s.IndexOf(".") - 1;
if (a < decimalPlaces)
{
for (int i = 0; i < decimalPlaces - a; i++)
{
s += "0";
}
}
}
sizeF = g.MeasureString(frontStr + s + this.unit, textfont);
rectangleF11.Location = new Point((int)this.PointF.X + offsetValue1 - (int)sizeF.Width / 2, (int)this.PointF.Y + offset);
offset += offsetValue;
}
// 像素面积
if (drawingPropertiesList.Contains(MeasureAttributes.PixelArea.ToString()))
{
sizeF = g.MeasureString(frontStr + "" + Math.Round(this.area, decimalPlaces) + "px²", textfont);
rectangleF8.Location = new Point((int)this.PointF.X + offsetValue1 - (int)sizeF.Width / 2, (int)this.PointF.Y + offset);
offset += offsetValue;
}
// 物理面积
if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalArea.ToString()))
{
string s = Math.Round(this.area * unitLength, decimalPlaces).ToString();
if (s.IndexOf(".") == -1)
{
for (int i = 0; i < decimalPlaces; i++)
{
if (i == 0)
s += ".";
s += "0";
}
}
else
{
int a = s.Length - s.IndexOf(".") - 1;
if (a < decimalPlaces)
{
for (int i = 0; i < decimalPlaces - a; i++)
{
s += "0";
}
}
}
sizeF = g.MeasureString(frontStr + s + this.unit + "²", textfont);
rectangleF9.Location = new Point((int)this.PointF.X + offsetValue1 - (int)sizeF.Width / 2, (int)this.PointF.Y + offset);
offset += offsetValue;
}
// 测量方式
if (drawingPropertiesList.Contains(MeasureAttributes.MeasureMethod.ToString()))
{
sizeF = g.MeasureString(frontStr + "" + PdnResources.GetString("Menu.MeasureAction.PolygonMeasurement.Text"), textfont);
rectangleF1.Location = new Point((int)this.PointF.X + offsetValue1 - (int)sizeF.Width / 2, (int)this.PointF.Y + offset);
offset += offsetValue;
}
// 测量单位(中文)
if (drawingPropertiesList.Contains(MeasureAttributes.MeasureUnitCN.ToString()))
{
sizeF = g.MeasureString(frontStr + "" + this.unitString, textfont);
rectangleF2.Location = new Point((int)this.PointF.X + offsetValue1 - (int)sizeF.Width / 2, (int)this.PointF.Y + offset);
offset += offsetValue;
}
// 测量单位(英文)
if (drawingPropertiesList.Contains(MeasureAttributes.MeasureUnitEN.ToString()))
{
sizeF = g.MeasureString(frontStr + "" + this.unit, textfont);
rectangleF3.Location = new Point((int)this.PointF.X + offsetValue1 - (int)sizeF.Width / 2, (int)this.PointF.Y + offset);
offset += offsetValue;
}
// 像素起始点X
if (drawingPropertiesList.Contains(MeasureAttributes.PixelStartX.ToString()))
{
sizeF = g.MeasureString(frontStr + "" + startPoint.X, textfont);
rectangleF4.Location = new Point((int)this.PointF.X + offsetValue1 - (int)sizeF.Width / 2, (int)this.PointF.Y + offset);
offset += offsetValue;
}
// 像素起始点Y
if (drawingPropertiesList.Contains(MeasureAttributes.PixelStartY.ToString()))
{
sizeF = g.MeasureString(frontStr + "" + startPoint.Y, textfont);
rectangleF5.Location = new Point((int)this.PointF.X + offsetValue1 - (int)sizeF.Width / 2, (int)this.PointF.Y + offset);
offset += offsetValue;
}
// 物理起始点X
if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalStartX.ToString()))
{
sizeF = g.MeasureString(frontStr + "" + Math.Round(startPoint.X * unitLength, decimalPlaces), textfont);
rectangleF6.Location = new Point((int)this.PointF.X + offsetValue1 - (int)sizeF.Width / 2, (int)this.PointF.Y + offset);
offset += offsetValue;
}
// 物理起始点Y
if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalStartY.ToString()))
{
sizeF = g.MeasureString(frontStr + "" + Math.Round(startPoint.Y * unitLength, decimalPlaces), textfont);
rectangleF7.Location = new Point((int)this.PointF.X + offsetValue1 - (int)sizeF.Width / 2, (int)this.PointF.Y + offset);
offset += offsetValue;
}
}
}
if (drawingPropertiesList != null)
{
// 测量方式
if (drawingPropertiesList.Contains(MeasureAttributes.MeasureMethod.ToString()))
{
sizeF = g.MeasureString(frontStr + "" + PdnResources.GetString("Menu.MeasureAction.PolygonMeasurement.Text"), textfont);
rectangleF1.Width = sizeF.Width;
rectangleF1.Height = sizeF.Height;
g.DrawString(frontStr + "" + PdnResources.GetString("Menu.MeasureAction.PolygonMeasurement.Text"), textfont, textbrush, rectangleF1);
}
else
{
rectangleF1 = new RectangleF();
}
// 测量单位(中文)
if (drawingPropertiesList.Contains(MeasureAttributes.MeasureUnitCN.ToString()))
{
sizeF = g.MeasureString(frontStr + "" + this.unitString, textfont);
rectangleF2.Width = sizeF.Width;
rectangleF2.Height = sizeF.Height;
g.DrawString(frontStr + "" + this.unitString, textfont, textbrush, rectangleF2);
}
else
{
rectangleF2 = new RectangleF();
}
// 测量单位(英文)
if (drawingPropertiesList.Contains(MeasureAttributes.MeasureUnitEN.ToString()))
{
sizeF = g.MeasureString(frontStr + "" + this.unit, textfont);
rectangleF3.Width = sizeF.Width;
rectangleF3.Height = sizeF.Height;
g.DrawString(frontStr + "" + this.unit, textfont, textbrush, rectangleF3);
}
else
{
rectangleF3 = new RectangleF();
}
// 像素起始点X
if (drawingPropertiesList.Contains(MeasureAttributes.PixelStartX.ToString()))
{
sizeF = g.MeasureString(frontStr + "" + startPoint.X, textfont);
rectangleF4.Width = sizeF.Width;
rectangleF4.Height = sizeF.Height;
g.DrawString(frontStr + "" + startPoint.X, textfont, textbrush, rectangleF4);
}
else
{
rectangleF4 = new RectangleF();
}
// 像素起始点Y
if (drawingPropertiesList.Contains(MeasureAttributes.PixelStartY.ToString()))
{
sizeF = g.MeasureString(frontStr + "" + startPoint.Y, textfont);
rectangleF5.Width = sizeF.Width;
rectangleF5.Height = sizeF.Height;
g.DrawString(frontStr + "" + startPoint.Y, textfont, textbrush, rectangleF5);
}
else
{
rectangleF5 = new RectangleF();
}
// 物理起始点X
if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalStartX.ToString()))
{
sizeF = g.MeasureString(frontStr + "" + Math.Round(startPoint.X * unitLength, decimalPlaces), textfont);
rectangleF6.Width = sizeF.Width;
rectangleF6.Height = sizeF.Height;
g.DrawString(frontStr + "" + Math.Round(startPoint.X * unitLength, decimalPlaces), textfont, textbrush, rectangleF6);
}
else
{
rectangleF6 = new RectangleF();
}
// 物理起始点Y
if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalStartY.ToString()))
{
sizeF = g.MeasureString(frontStr + "" + Math.Round(startPoint.Y * unitLength, decimalPlaces), textfont);
rectangleF7.Width = sizeF.Width;
rectangleF7.Height = sizeF.Height;
g.DrawString(frontStr + "" + Math.Round(startPoint.Y * unitLength, decimalPlaces), textfont, textbrush, rectangleF7);
}
else
{
rectangleF7 = new RectangleF();
}
// 像素面积
if (drawingPropertiesList.Contains(MeasureAttributes.PixelArea.ToString()))
{
sizeF = g.MeasureString("" + Math.Round(this.area, decimalPlaces) + "px²", textfont);
rectangleF8.Width = sizeF.Width;
rectangleF8.Height = sizeF.Height;
g.DrawString(frontStr + "" + Math.Round(this.area, decimalPlaces) + "px²", textfont, textbrush, rectangleF8);
}
else
{
rectangleF8 = new RectangleF();
}
// 物理面积
if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalArea.ToString()))
{
string s = Math.Round(this.area * unitLength, decimalPlaces).ToString();
if (s.IndexOf(".") == -1)
{
for (int i = 0; i < decimalPlaces; i++)
{
if (i == 0)
s += ".";
s += "0";
}
}
else
{
int a = s.Length - s.IndexOf(".") - 1;
if (a < decimalPlaces)
{
for (int i = 0; i < decimalPlaces - a; i++)
{
s += "0";
}
}
}
sizeF = g.MeasureString(frontStr + s + this.unit + "²", textfont);
rectangleF9.Width = sizeF.Width;
rectangleF9.Height = sizeF.Height;
g.DrawString(frontStr + s + this.unit + "²", textfont, textbrush, rectangleF9);
}
else
{
rectangleF9 = new RectangleF();
}
// 像素周长
if (drawingPropertiesList.Contains(MeasureAttributes.PixelCircumference.ToString()))
{
sizeF = g.MeasureString(frontStr + "" + Math.Round(this.length, decimalPlaces) + "px", textfont);
rectangleF10.Width = sizeF.Width;
rectangleF10.Height = sizeF.Height;
g.DrawString(frontStr + "" + Math.Round(this.length, decimalPlaces) + "px", textfont, textbrush, rectangleF10);
}
else
{
rectangleF10 = new RectangleF();
}
// 物理周长
if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalCircumference.ToString()))
{
string s = Math.Round(this.length * unitLength, decimalPlaces).ToString();
if (s.IndexOf(".") == -1)
{
for (int i = 0; i < decimalPlaces; i++)
{
if (i == 0)
s += ".";
s += "0";
}
}
else
{
int a = s.Length - s.IndexOf(".") - 1;
if (a < decimalPlaces)
{
for (int i = 0; i < decimalPlaces - a; i++)
{
s += "0";
}
}
}
sizeF = g.MeasureString(frontStr + s + this.unit, textfont);
rectangleF11.Width = sizeF.Width;
rectangleF11.Height = sizeF.Height;
g.DrawString(frontStr + s + this.unit, textfont, textbrush, rectangleF11);
}
else
{
rectangleF11 = new RectangleF();
}
//还原为原始旋转矩阵
g.Transform = mtxSave;
}
}
matrix.Dispose();
if (this.configurationFile)
this.pointChange = false;
this.mouseUpAttribute = false;
pointChangeObject.Remove(this.drawToolType);
}
}
///
/// 停止绘制时
///
///
public override void MouseUp(bool up)
{
mouseUpPointChange = up;
}
public void CalcCenter()
{
centerPoint = new PointF(rectangle.X + rectangle.Width / 2, rectangle.Y + rectangle.Height / 2);//中心点
}
public override int HandleCount
{
get
{
return 7;
}
}
///
/// Get handle pointscroll by 1-based number
///
///
///
public override PointF GetHandle(int handleNumber)
{
float x = 0;
float y = 0;
switch (handleNumber)
{
case 1:
x = (int)pointArray[0].X;
y = (int)pointArray[0].Y;
break;
case 2:
x = (int)pointArray[1].X;
y = (int)pointArray[1].Y;
break;
case 3:
x = largerRect.X;
y = largerRect.Y;
break;
case 4:
x = largerRect.Right;
y = largerRect.Y;
break;
case 5:
x = largerRect.Right;
y = largerRect.Bottom;
break;
case 6:
x = largerRect.X;
y = largerRect.Bottom;
break;
case 7:
x = this.pointL.X;
y = this.pointL.Y;
break;
}
return new PointF(x, y);
}
///
/// Hit test.
/// Return value: -1 - no hit
/// 0 - hit anywhere
/// > 1 - handle number
///
///
///
public override int HitTest(Point point)
{
if (Selected)
{
for (int i = 1; i <= HandleCount; i++)
{
if (GetHandleRectangle(i).Contains(point))
return i;
}
if (GetRectanglePosition(this.rectangleF1, point, this.PointF , angle))
{
this.pointL = SetOffsetAfterRotation(point, this.PointF , angle);
moveKb = 1;
return 7;
}
else if (GetRectanglePosition(this.rectangleF2, point, this.PointF , angle))
{
this.pointL = SetOffsetAfterRotation(point, this.PointF , angle);
moveKb = 2;
return 7;
}
else if (GetRectanglePosition(this.rectangleF3, point, this.PointF , angle))
{
this.pointL = SetOffsetAfterRotation(point, this.PointF , angle);
moveKb = 3;
return 7;
}
else if (GetRectanglePosition(this.rectangleF4, point, this.PointF , angle))
{
this.pointL = SetOffsetAfterRotation(point, this.PointF , angle);
moveKb = 4;
return 7;
}
else if (GetRectanglePosition(this.rectangleF5, point, this.PointF , angle))
{
this.pointL = SetOffsetAfterRotation(point, this.PointF , angle);
moveKb = 5;
return 7;
}
else if (GetRectanglePosition(this.rectangleF6, point, this.PointF , angle))
{
this.pointL = SetOffsetAfterRotation(point, this.PointF , angle);
moveKb = 6;
return 7;
}
else if (GetRectanglePosition(this.rectangleF7, point, this.PointF , angle))
{
this.pointL = SetOffsetAfterRotation(point, this.PointF , angle);
moveKb = 7;
return 7;
}
else if (GetRectanglePosition(this.rectangleF8, point, this.PointF , angle))
{
this.pointL = SetOffsetAfterRotation(point, this.PointF , angle);
moveKb = 8;
return 7;
}
else if (GetRectanglePosition(this.rectangleF9, point, this.PointF , angle))
{
this.pointL = SetOffsetAfterRotation(point, this.PointF , angle);
moveKb = 9;
return 7;
}
else if (GetRectanglePosition(this.rectangleF10, point, this.PointF , angle))
{
this.pointL = SetOffsetAfterRotation(point, this.PointF , angle);
moveKb = 10;
return 7;
}
else if (GetRectanglePosition(this.rectangleF11, point, this.PointF , angle))
{
this.pointL = SetOffsetAfterRotation(point, this.PointF , angle);
moveKb = 11;
return 7;
}
}
if (PointInObject(point))
return 0;
return -1;
}
protected override bool PointInObject(Point point)
{
return largerRect.Contains(point);
}
public override bool IntersectsWith(Rectangle rectangle)
{
return largerRect.IntersectsWith(rectangle);
}
public override Cursor GetHandleCursor(int handleNumber)
{
return handleCursor;
}
public override void MoveHandleTo(Point point, int handleNumber)
{
if (handleNumber < 3)
{
this.mouseUpPointChange = true;
switch (handleNumber)
{
case 1:
this.rectangleF1.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
this.rectangleF2.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
this.rectangleF3.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
this.rectangleF4.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
this.rectangleF5.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
this.rectangleF6.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
this.rectangleF7.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
this.rectangleF8.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
this.rectangleF9.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
this.rectangleF10.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
this.rectangleF11.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
//更新第一个点
startPoint = point;
this.pointArray[0] = startPoint;
//计算第三个点
linePoint.X = (startPoint.X - startPoint.Y + endPoint.X + endPoint.Y) / 2;
linePoint.Y = (startPoint.X + startPoint.Y - endPoint.X + endPoint.Y) / 2;
this.pointArray[2] = linePoint;
//计算第四个点
linePoint1.X = (startPoint.X + startPoint.Y + endPoint.X - endPoint.Y) / 2;
linePoint1.Y = (-(startPoint.X) + startPoint.Y + endPoint.X + endPoint.Y) / 2;
this.pointArray[3] = linePoint1;
break;
case 2:
endPoint = point;
this.pointArray[1] = endPoint;
//计算第三个点
linePoint.X = (startPoint.X - startPoint.Y + endPoint.X + endPoint.Y) / 2;
linePoint.Y = (startPoint.X + startPoint.Y - endPoint.X + endPoint.Y) / 2;
if (this.pointArray.Count == 2)
{
this.pointArray.Add(linePoint);
}
else
{
this.pointArray[2] = linePoint;
}
//计算第四个点
linePoint1.X = (startPoint.X + startPoint.Y + endPoint.X - endPoint.Y) / 2;
linePoint1.Y = (-(startPoint.X) + startPoint.Y + endPoint.X + endPoint.Y) / 2;
if (this.pointArray.Count == 3)
{
this.pointArray.Add(linePoint1);
}
else
{
this.pointArray[3] = linePoint1;
}
break;
}
if(this.pointArray.Count == 4)
{
CalcOtherRectangle();
CalcCenter();
}
}
else
{
float x = 0, y = 0;
switch (handleNumber)
{
case 3:
this.mouseUpPointChange = true;
x = largerRect.X;
y = largerRect.Y;
break;
case 4:
this.mouseUpPointChange = true;
x = largerRect.Right;
y = largerRect.Y;
break;
case 5:
this.mouseUpPointChange = true;
x = largerRect.Right;
y = largerRect.Bottom;
break;
case 6:
this.mouseUpPointChange = true;
x = largerRect.X;
y = largerRect.Bottom;
break;
case 7:
point = SetOffsetAfterRotation(point, this.PointF, angle);
if (this.moveKb == 1)
this.rectangleF1.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
else if (this.moveKb == 2)
this.rectangleF2.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
else if (this.moveKb == 3)
this.rectangleF3.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
else if (this.moveKb == 4)
this.rectangleF4.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
else if (this.moveKb == 5)
this.rectangleF5.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
else if (this.moveKb == 6)
this.rectangleF6.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
else if (this.moveKb == 7)
this.rectangleF7.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
else if (this.moveKb == 8)
this.rectangleF8.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
else if (this.moveKb == 9)
this.rectangleF9.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
else if (this.moveKb == 10)
this.rectangleF10.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
else if (this.moveKb == 11)
this.rectangleF11.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
this.pointL = point;
break;
}
if(handleNumber != 7)
{
this.mouseUpPointChange = true;
if (!this.isPress)
{
this.isPress = true;
lastPoint = new PointF(x, y);
}
double angle = BasicCalculationHelper.CalculateAngle(centerPoint.X, centerPoint.Y, lastPoint.X, lastPoint.Y, point.X, point.Y);
lastPoint = point;
for (int l = 0; l < pointArray.Count; l++)
{
pointArray[l] = BasicCalculationHelper.GetAnglePoint(pointArray[l], centerPoint, angle);
}
startPoint = BasicCalculationHelper.GetAnglePoint(startPoint, centerPoint, angle);
endPoint = BasicCalculationHelper.GetAnglePoint(endPoint, centerPoint, angle);
linePoint = BasicCalculationHelper.GetAnglePoint(linePoint, centerPoint, angle);
linePoint1 = BasicCalculationHelper.GetAnglePoint(linePoint1, centerPoint, angle);
CalcOtherRectangle();
}
}
}
///
/// 计算外接矩形
///
private void CalcOtherRectangle()
{
float[] pointX = new float[] { pointArray[0].X, pointArray[1].X, pointArray[2].X, pointArray[3].X };
rectangle.X = (int)pointX.Min();
float[] pointY = new float[] { pointArray[0].Y, pointArray[1].Y, pointArray[2].Y, pointArray[3].Y };
rectangle.Y = (int)pointY.Min();
rectangle.Width = (int)Math.Abs(pointX.Max() - pointX.Min());
rectangle.Height = (int)Math.Abs(pointY.Max() - pointY.Min());
}
///
/// 对象移动
///
///
///
public override void Move(int deltaX, int deltaY)
{
int x = ISurfaceBox.UnscaleScalar(deltaX);
int y = ISurfaceBox.UnscaleScalar(deltaY);
startPoint.X += x;
startPoint.Y += y;
endPoint.X += x;
endPoint.Y += y;
linePoint.X += x;
linePoint.Y += y;
linePoint1.X += x;
linePoint1.Y += y;
for (int i = 0; i < pointArray.Count; i++)
{
pointArray[i] = new PointF(pointArray[i].X + ISurfaceBox.UnscaleScalar(deltaX), pointArray[i].Y + ISurfaceBox.UnscaleScalar(deltaY));
}
this.startPoint = pointArray[0];
pointL.X += x;
pointL.Y += y;
this.rectangleF1.Offset(x, y);
this.rectangleF2.Offset(x, y);
this.rectangleF3.Offset(x, y);
this.rectangleF4.Offset(x, y);
this.rectangleF5.Offset(x, y);
this.rectangleF6.Offset(x, y);
this.rectangleF7.Offset(x, y);
this.rectangleF8.Offset(x, y);
this.rectangleF9.Offset(x, y);
this.rectangleF10.Offset(x, y);
this.rectangleF11.Offset(x, y);
CalcOtherRectangle();
CalcCenter();
}
public override RectangleF GetBoundingBox()
{
RectangleF temp = new RectangleF();
temp.X = rectangle.X - 10;
temp.Y = rectangle.Y - 10;
temp.Width = rectangle.Width + 20;
temp.Height = rectangle.Height + 20;
return temp;
}
///
/// Draw tracker for selected object
///
///
public override void DrawTracker(Graphics g)
{
if (!Selected)
return;
if (pointArray.Count >= 4)
{
SolidBrush brush = new SolidBrush(Color.FromArgb(MarkpointAreaColor));
Pen pen = new Pen(Color.FromArgb(MarkpointLineColor), MarkpointLineWidth);
largerRect = GetBoundingBox();
for (int i = 1; i <= HandleCount; i++)
{
if (i == HandleCount)
{
brush = new SolidBrush(Color.Transparent);
pen = new Pen(Color.Transparent);
}
switch (MarkpointStyle)
{
case 0:
g.FillRectangle(brush, GetHandleRectangle(i));
g.DrawRectangle(pen, GetHandleRectangle(i));
break;
case 1:
g.FillEllipse(brush, GetHandleRectangle(i));
g.DrawEllipse(pen, GetHandleRectangle(i));
break;
case 2:
g.FillPolygon(brush, GetHandlePoint(i));
g.DrawPolygon(pen, GetHandlePoint(i));
break;
}
}
brush.Dispose();
pen.Dispose();
}
}
///
/// 获取点集合
///
///
public override List GetPoints()
{
return pointArray;
}
///
/// 获取样式
///
///
public override ParentStyleModel GetStyle()
{
return measureRandSquare;
}
}
}