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.Annotation.Measure; namespace PaintDotNet.Annotation.Label { /// /// 标注->手动标尺 /// public class DrawHandModeRuler : DrawObject { private static Cursor handleCursor = new Cursor(PdnResources.GetResourceStream("Cursors.AnnotationPolyHandle.cur")); /// /// 标注样式信息model /// public RulerModel rulerModel; double micronRatio = 1; double oldmicronRatio; /// /// 标尺长度集合 /// private int[] lengthEnum = new int[] { 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000, 50000, 1000000}; private bool isMoveForward = true; public DrawHandModeRuler(ISurfaceBox surfaceBox, int x1, int y1, int x2, int y2) : base() { this.objectType = DrawClass.Label; this.drawToolType = DrawToolType.DrawHandModeRuler; //Dictionary measurementUnitDictionary = surfaceBox.getMeasureInfo(); //micronRatio = measurementUnitDictionary[MeasurementUnit.Micron];//每像素多少微米长度 Dictionary measurementUnitDictionary = surfaceBox.getMeasureInfo(); MeasurementUnit unit = surfaceBox.GetMeasurementUnit(); micronRatio = measurementUnitDictionary[unit];//每像素多少微米长度 oldmicronRatio = micronRatio; rulerModel = surfaceBox.GetRulerStyleModel(); startPoint.X = x1; startPoint.Y = y1; endPoint.X = x2; endPoint.Y = y2; //change = false; int pixelLength = Math.Abs(Convert.ToInt32(endPoint.X - startPoint.X));//根据两点获取的标尺像素长度 length = pixelLength * micronRatio; } public DrawHandModeRuler(ISurfaceBox surfaceBox, List points, ParentStyleModel parentStyleModel, Object content) : base() { this.objectType = DrawClass.Label; this.drawToolType = DrawToolType.DrawHandModeRuler; this.ISurfaceBox = surfaceBox; rulerModel = (RulerModel)parentStyleModel; startPoint = points[0]; endPoint = points[1]; } /// /// Clone this instance /// public override DrawObject Clone() { DrawHandModeRuler handModeRulerDraw = new DrawHandModeRuler(ISurfaceBox, 0, 0, 1, 0); handModeRulerDraw.objectType = DrawClass.Label; handModeRulerDraw.drawToolType = DrawToolType.DrawHandModeRuler; handModeRulerDraw.ISurfaceBox = this.ISurfaceBox; handModeRulerDraw.startPoint = this.startPoint; handModeRulerDraw.endPoint = this.endPoint; handModeRulerDraw.length = this.length; //handModeRulerDraw.pixelLength = pixelLength; FillDrawObjectFields(handModeRulerDraw); return handModeRulerDraw; } public override DrawObject Clone(ISurfaceBox surfaceBox) { DrawHandModeRuler handModeRulerDraw = new DrawHandModeRuler(surfaceBox, 0, 0, 1, 0); handModeRulerDraw.objectType = DrawClass.Label; handModeRulerDraw.drawToolType = DrawToolType.DrawHandModeRuler; handModeRulerDraw.ISurfaceBox = surfaceBox; handModeRulerDraw.startPoint = this.startPoint; handModeRulerDraw.endPoint = this.endPoint; handModeRulerDraw.length = this.length; //handModeRulerDraw.pixelLength = pixelLength; FillDrawObjectFields(handModeRulerDraw); return handModeRulerDraw; } public override void Draw(Graphics g) { g.SmoothingMode = SmoothingMode.AntiAlias; Dictionary measurementUnitDictionary = ISurfaceBox.getMeasureInfo(); MeasurementUnit unit = ISurfaceBox.GetMeasurementUnit(); double micron = measurementUnitDictionary[MeasurementUnit.Micron];//每像素多少微米长度 micronRatio = measurementUnitDictionary[MeasurementUnit.Micron];//每像素多少微米长度 int pixelLength = Math.Abs(Convert.ToInt32(endPoint.X - startPoint.X));//根据两点获取的标尺像素长度 length = pixelLength * micronRatio; //length = pixelLength * micronRatio; //判断像素长度所属区间并重新赋值 if (length <= lengthEnum[0]) { length = lengthEnum[0]; if (startPoint.X <= endPoint.X) endPoint.X = startPoint.X + (int)(length / micronRatio); else endPoint.X = startPoint.X - (int)(length / micronRatio); } else if (length > lengthEnum[0] && length <= lengthEnum[1]) { length = lengthEnum[1]; if (startPoint.X <= endPoint.X) endPoint.X = startPoint.X + (int)(length / micronRatio); else endPoint.X = startPoint.X - (int)(length / micronRatio); } else if (length > lengthEnum[1] && length <= lengthEnum[2]) { length = lengthEnum[2]; if (startPoint.X <= endPoint.X) endPoint.X = startPoint.X + (int)(length / micronRatio); else endPoint.X = startPoint.X - (int)(length / micronRatio); } else if (length > lengthEnum[2] && length <= lengthEnum[3]) { length = lengthEnum[3]; if (startPoint.X <= endPoint.X) endPoint.X = startPoint.X + (int)(length / micronRatio); else endPoint.X = startPoint.X - (int)(length / micronRatio); } else if (length > lengthEnum[3] && length <= lengthEnum[4]) { length = lengthEnum[4]; if (startPoint.X <= endPoint.X) endPoint.X = startPoint.X + (int)(length / micronRatio); else endPoint.X = startPoint.X - (int)(length / micronRatio); } else if (length > lengthEnum[4] && length <= lengthEnum[5]) { length = lengthEnum[5]; if (startPoint.X <= endPoint.X) endPoint.X = startPoint.X + (int)(length / micronRatio); else endPoint.X = startPoint.X - (int)(length / micronRatio); } else if (length > lengthEnum[5] && length <= lengthEnum[6]) { length = lengthEnum[6]; if (startPoint.X <= endPoint.X) endPoint.X = startPoint.X + (int)(length / micronRatio); else endPoint.X = startPoint.X - (int)(length / micronRatio); } else if (length > lengthEnum[6] && length <= lengthEnum[7]) { length = lengthEnum[7]; if (startPoint.X <= endPoint.X) endPoint.X = startPoint.X + (int)(length / micronRatio); else endPoint.X = startPoint.X - (int)(length / micronRatio); } else if (length > lengthEnum[7] && length <= lengthEnum[8]) { length = lengthEnum[8]; if (startPoint.X <= endPoint.X) endPoint.X = startPoint.X + (int)(length / micronRatio); else endPoint.X = startPoint.X - (int)(length / micronRatio); } else if (length > lengthEnum[8] && length <= lengthEnum[9]) { length = lengthEnum[9]; if (startPoint.X <= endPoint.X) endPoint.X = startPoint.X + (int)(length / micronRatio); else endPoint.X = startPoint.X - (int)(length / micronRatio); } else if (length > lengthEnum[9] && length <= lengthEnum[10]) { length = lengthEnum[10]; if (startPoint.X <= endPoint.X) endPoint.X = startPoint.X + (int)(length / micronRatio); else endPoint.X = startPoint.X - (int)(length / micronRatio); } else if (length > lengthEnum[10] && length <= lengthEnum[11]) { length = lengthEnum[11]; if (startPoint.X <= endPoint.X) endPoint.X = startPoint.X + (int)(length / micronRatio); else endPoint.X = startPoint.X - (int)(length / micronRatio); } else if (length > lengthEnum[11] && length <= lengthEnum[12]) { length = lengthEnum[12]; if (startPoint.X <= endPoint.X) endPoint.X = startPoint.X + (int)(length / micronRatio); else endPoint.X = startPoint.X - (int)(length / micronRatio); } else if (length > lengthEnum[12] && length <= lengthEnum[13]) { length = lengthEnum[13]; if (startPoint.X <= endPoint.X) endPoint.X = startPoint.X + (int)(length / micronRatio); else endPoint.X = startPoint.X - (int)(length / micronRatio); } else { length = lengthEnum[14]; } #region [单位缩写] string unitAbbreviation = string.Empty; micronRatio = measurementUnitDictionary[unit];//每像素多少微米长度 switch (unit) { case MeasurementUnit.Inch: length = length * micronRatio / micron; unitAbbreviation = "in"; break; case MeasurementUnit.Centimeter: length = length * micronRatio / micron; unitAbbreviation = "cm"; break; case MeasurementUnit.Millimeter: length = length * micronRatio / micron; unitAbbreviation = "mm"; break; case MeasurementUnit.Micron: unitAbbreviation = "µm"; break; case MeasurementUnit.Nano: length = length * micronRatio / micron; unitAbbreviation = "nm"; break; case MeasurementUnit.Pixel: unitAbbreviation = "PX"; break; case MeasurementUnit.Mil: length = length * micronRatio / micron; unitAbbreviation = "mil"; break; } #endregion //else if (pixelLength > lengthEnum[7] / micronRatio && pixelLength <= lengthEnum[8] / micronRatio) // pixelLength = (int)(lengthEnum[8] / micronRatio); //else if (pixelLength > lengthEnum[8] / micronRatio && pixelLength <= lengthEnum[9] / micronRatio) // pixelLength = (int)(lengthEnum[9] / micronRatio); //else if (pixelLength > lengthEnum[9] / micronRatio && pixelLength <= lengthEnum[10] / micronRatio) // pixelLength = (int)(lengthEnum[10] / micronRatio); //else // pixelLength = lengthEnum[11]; ////判断像素长度所属区间并重新赋值 //if (pixelLength <= lengthEnum[0]/ micronRatio) // pixelLength = (int)(lengthEnum[0]/ micronRatio); //else if (pixelLength > lengthEnum[0]/ micronRatio && pixelLength <= lengthEnum[1]/ micronRatio) // pixelLength = (int)(lengthEnum[1] / micronRatio); //else if (pixelLength > lengthEnum[1]/ micronRatio && pixelLength <= lengthEnum[2]/ micronRatio) // pixelLength = (int)(lengthEnum[2] / micronRatio); //else if (pixelLength > lengthEnum[2] / micronRatio && pixelLength <= lengthEnum[3] / micronRatio) // pixelLength = (int)(lengthEnum[3] / micronRatio); //else if (pixelLength > lengthEnum[3] / micronRatio && pixelLength <= lengthEnum[4] / micronRatio) // pixelLength = (int)(lengthEnum[4] / micronRatio); //else if (pixelLength > lengthEnum[4] / micronRatio && pixelLength <= lengthEnum[5] / micronRatio) // pixelLength = (int)(lengthEnum[5] / micronRatio); //else if (pixelLength > lengthEnum[5] / micronRatio && pixelLength <= lengthEnum[6] / micronRatio) // pixelLength = (int)(lengthEnum[6] / micronRatio); //else if (pixelLength > lengthEnum[6] / micronRatio && pixelLength <= lengthEnum[7] / micronRatio) // pixelLength = (int)(lengthEnum[7] / micronRatio); //else if (pixelLength > lengthEnum[7] / micronRatio && pixelLength <= lengthEnum[8] / micronRatio) // pixelLength = (int)(lengthEnum[8] / micronRatio); //else if (pixelLength > lengthEnum[8] / micronRatio && pixelLength <= lengthEnum[9] / micronRatio) // pixelLength = (int)(lengthEnum[9] / micronRatio); //else if (pixelLength > lengthEnum[9] / micronRatio && pixelLength <= lengthEnum[10] / micronRatio) // pixelLength = (int)(lengthEnum[10] / micronRatio); //else // pixelLength = lengthEnum[11]; //if (isMoveForward) //{ // if (startPoint.X <= endPoint.X) // endPoint.X = startPoint.X + pixelLength; // else // endPoint.X = startPoint.X - pixelLength; //} //else //{ // if (startPoint.X <= endPoint.X) // startPoint.X = endPoint.X - pixelLength; // else // startPoint.X = endPoint.X + pixelLength; //} //int pixelLength = Math.Abs(Convert.ToInt32(endPoint.X - startPoint.X)); //length = pixelLength * micronRatio; //double lineLength = length/* Math.Round(length)*/;//物理长度 string text = length + unitAbbreviation;//文字 //string text = Math.Round(length, MeasureDrawObject.decimalPlaces).ToString() + unitAbbreviation;//小数保留 if (text != null && text != "") { Pen linePen = new Pen(Color.FromArgb(this.rulerModel.lineColor)); linePen.Width = (int)this.rulerModel.lineWidth; Pen borderPen = new Pen(Color.FromArgb(this.rulerModel.borderColor)); borderPen.Width = (int)this.rulerModel.borderWidth; PointF lineL = new PointF(0, 0); PointF lineR = new PointF(0, 0); if (isMoveForward) { if (startPoint.X <= endPoint.X) { lineL.X = startPoint.X; lineL.Y = startPoint.Y; lineR.X = startPoint.X + (int)(length / micronRatio); lineR.Y = startPoint.Y; } else { lineL.X = startPoint.X - (int)(length / micronRatio); lineL.Y = startPoint.Y; lineR.X = startPoint.X; lineR.Y = startPoint.Y; } } else { if (startPoint.X <= endPoint.X) { lineL.X = endPoint.X - (int)(length / micronRatio); lineL.Y = endPoint.Y; lineR.X = endPoint.X; lineR.Y = endPoint.Y; } else { lineL.X = endPoint.X; lineL.Y = endPoint.Y; lineR.X = endPoint.X + (int)(length / micronRatio); lineR.Y = endPoint.Y; } } int lineWidthOffset = (int)rulerModel.lineWidth / 2 - 1; //垂线 decimal verticalLength = (int)(length / micronRatio) * this.rulerModel.verticalLineLength / 100; PointF verticalLT = new PointF(lineL.X, lineL.Y - (int)(verticalLength / 2)); PointF verticalLB = new PointF(lineL.X, lineL.Y + (int)(verticalLength / 2)); PointF verticalRT = new PointF(lineR.X, lineR.Y - (int)(verticalLength / 2)); PointF verticalRB = new PointF(lineR.X, lineR.Y + (int)(verticalLength / 2)); // 文字 decimal textHeight = (int)(length / micronRatio) * this.rulerModel.textHeight / 100; Font textFont; if (this.rulerModel.textBold == 0) textFont = new Font(this.rulerModel.textFont, (float)this.rulerModel.textFontSize,FontStyle.Regular); else textFont = new Font(this.rulerModel.textFont, (float)this.rulerModel.textFontSize, FontStyle.Bold); float textSizes = textFont.SizeInPoints * text.Length; float textX = lineL.X; if (rulerModel.textPosition == 1) { textX = (lineR.X - lineL.X) / 2 - (int)(textSizes / 2) + lineL.X; } else if (rulerModel.textPosition == 2) { textX = lineR.X - (int)(textSizes); } PointF textLT = new PointF(textX, lineL.Y - lineWidthOffset - (int)(textHeight + new Decimal(textFont.Height))); // 背景参照Y坐标,垂线高,使用垂线Y值,文字高使用文字Y值。 float backRectangleReferY = textLT.Y < verticalLT.Y - 2 ? textLT.Y : verticalLT.Y - 2; // 背景大小 decimal backLength = (int)(length / micronRatio) * this.rulerModel.backgroundSize / 100; float backRectangleX = verticalLT.X - (int)backLength - (int)linePen.Width; float backRectangleY = backRectangleReferY - (int)(backLength); float backRectangleW = (verticalRT.X - verticalLT.X) + (int)(backLength * 2) + (int)(linePen.Width * 2); float backRectangleH = (verticalLB.Y - backRectangleReferY) + (int)(backLength * 2) + lineWidthOffset + 2; RectangleF backRectangle = new RectangleF(backRectangleX, backRectangleY, backRectangleW, backRectangleH); // 边框 int offset = borderPen.Width > 0 ? (int)(borderPen.Width / 2) - 2 : 0; RectangleF borderPenRectangle = new RectangleF(backRectangle.X - offset, backRectangle.Y - offset, backRectangle.Width + offset * 2, backRectangle.Height + offset * 2); // 绘制 g.FillRectangle(new SolidBrush(Color.FromArgb(this.rulerModel.backColor)), backRectangle); g.DrawRectangle(borderPen, borderPenRectangle.X, borderPenRectangle.Y, borderPenRectangle.Width, borderPenRectangle.Height); g.DrawLine(linePen, lineL, lineR); g.DrawLine(linePen, verticalLT, verticalLB); g.DrawLine(linePen, verticalRT, verticalRB); g.DrawString(text, textFont, new SolidBrush(Color.FromArgb(this.rulerModel.textColor)), textLT.X, textLT.Y); rectangle = borderPenRectangle; linePen.Dispose(); borderPen.Dispose(); } } public override int HandleCount { get { return 2; } } /// /// Get handle pointscroll by 1-based number /// /// /// public override PointF GetHandle(int handleNumber) { float x = 0, y = 0; switch (handleNumber) { case 1: x = startPoint.X; y = startPoint.Y; break; case 2: x = endPoint.X; y = endPoint.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 (PointInObject(point)) return 0; return -1; } protected override bool PointInObject(Point point) { return rectangle.Contains(point); } public override bool IntersectsWith(Rectangle rectangle) { return Rectangle.IntersectsWith(rectangle); } public override Cursor GetHandleCursor(int handleNumber) { switch (handleNumber) { case 1: return Cursors.SizeWE; case 2: return Cursors.SizeWE; default: return Cursors.Default; } } public override void MoveHandleTo(Point point, int handleNumber) { switch (handleNumber) { case 1: isMoveForward = false; startPoint.X = point.X; break; case 2: isMoveForward = true; endPoint.X = point.X; break; } int pixelLength = Math.Abs(Convert.ToInt32(endPoint.X - startPoint.X));//根据两点获取的标尺像素长度 length = pixelLength * micronRatio; } 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; rectangle.X += x; rectangle.Y += y; int pixelLength = Math.Abs(Convert.ToInt32(endPoint.X - startPoint.X));//根据两点获取的标尺像素长度 length = pixelLength * micronRatio; } public override RectangleF GetBoundingBox() { return rectangle; } public override List GetPoints() { List points = new List(); points.Add(startPoint); points.Add(endPoint); return points; } public override ParentStyleModel GetStyle() { return rulerModel; } } }