/// <summary> /// Measures an instance during the first layout pass prior to arranging it. /// </summary> /// <param name="availableSize">A maximum Size to not exceed.</param> /// <returns>The maximum Size for the instance.</returns> protected override Size MeasureOverride(Size availableSize) { if (Unit == Unit.Cm) { return(new Size(DipHelper.CmToDip(Length), Height)); } else if (Unit == Unit.Inch) { return(new Size(DipHelper.InchToDip(Length), Height)); } else { return(base.MeasureOverride(availableSize)); } }
/// <summary> /// Participates in rendering operations. /// </summary> /// <param name="drawingContext">The drawing instructions for a specific element. This context is provided to the layout system.</param> protected override void OnRender(DrawingContext drawingContext) { base.OnRender(drawingContext); double xDest = this.ActualWidth; this.VisualEdgeMode = EdgeMode.Aliased; if (Marks == MarksLocation.Down)//hruler { drawingContext.DrawLine(BorderPen, new Point(0.0, Height), new Point(xDest, Height)); } else//vruler { drawingContext.DrawLine(BorderPen, new Point(0.0, 0), new Point(xDest, 0)); } double offset = 1; double DrawLength = Length + 2; double start = Math.Max(0, Convert.ToInt32(CountShift) / 100); double unit = 100 / this.Zoom; double n = start; for (double dUnit = start; dUnit <= DrawLength * this.Zoom + start; dUnit++, n++) { double d = dUnit * 100; for (int i = 1; i <= 9; i++) { if (i != 5) {//mark:10,20...110,120.. double nDip10 = 100 * (dUnit + 0.1 * i) - CountShift + offset; if (Marks == MarksLocation.Up) { drawingContext.DrawLine(ThinPen, new Point(nDip10, 0), new Point(nDip10, SegmentHeight / 3.0)); } else { drawingContext.DrawLine(ThinPen, new Point(nDip10, Height), new Point(nDip10, Height - SegmentHeight / 3.0)); } } }//mark:50,150,250... double dMiddle = 100 * (dUnit + 0.5) - CountShift + offset; if (Marks == MarksLocation.Up) { drawingContext.DrawLine(p, new Point(dMiddle, 0), new Point(dMiddle, SegmentHeight * 2.0 / 3.0)); } else { drawingContext.DrawLine(p, new Point(dMiddle, Height), new Point(dMiddle, Height - SegmentHeight * 2.0 / 3.0)); } //mark:100,200,300... double dShift = d - CountShift + offset; if (dShift != offset) { if (Marks == MarksLocation.Up) { drawingContext.DrawLine(p, new Point(dShift, 0), new Point(dShift, Height)); } else { drawingContext.DrawLine(p, new Point(dShift, Height), new Point(dShift, 0)); } } if ((dUnit < DrawLength * this.Zoom + CountShift / 100))//(dUnit != 0.0) && { double sNumber = Math.Round(n * 100 / this.Zoom); string szRulerNumber = (sNumber).ToString(CultureInfo.CurrentCulture); FormattedText ft = new FormattedText( szRulerNumber, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("Arial"), DipHelper.PtToDip(7), new SolidColorBrush(Color.FromRgb(0x55, 0x55, 0x55))); ft.SetFontWeight(FontWeights.Normal); ft.TextAlignment = TextAlignment.Center; //Draw text if (Marks == MarksLocation.Up) { drawingContext.DrawText(ft, new Point(dShift + ft.Width / 2 + 2, Height - ft.Height)); } else { drawingContext.DrawText(ft, new Point(dShift + ft.Width / 2 + 2, Height - SegmentHeight / 3.0 - ft.Height));//- SegmentHeight } } } }