实例介绍
【实例简介】
【实例截图】
【核心代码】
using System; using System.Globalization; using System.Windows; using System.Windows.Media; namespace WpfRuler { /// <summary> /// A ruler control which displays ruler in pixels. /// In order to use it vertically, change the <see cref="Marks">Marks</see> property to <c>Up</c> and rotate it ninety degrees. /// </summary> /// <remarks> /// Rewritten by: Sebestyen Murancsik /// /// Contributions from <see /// cref="http://www.orbifold.net/default/?p=2295&cpage=1#comment-61500">Raf /// Lenfers</see> /// <seealso>http://visualizationtools.net/default/wpf-ruler/</seealso> /// </remarks> public class PixelRuler : FrameworkElement { #region Fields private double SegmentHeight; private readonly Pen p = new Pen(Brushes.Black, 1.0); private readonly Pen ThinPen = new Pen(Brushes.Black, 0.5); private readonly Pen BorderPen = new Pen(Brushes.Gray, 1.0); private readonly Pen RedPen = new Pen(Brushes.Red, 2.0); #endregion #region Properties #region Length /// <summary> /// Gets or sets the length of the ruler. If the <see cref="AutoSize"/> property is set to false (default) this /// is a fixed length. Otherwise the length is calculated based on the actual width of the ruler. /// </summary> public double Length { get { if (this.AutoSize) { return ActualWidth / Zoom; } else { return (double)GetValue(LengthProperty); } } set { SetValue(LengthProperty, value); } } /// <summary> /// Identifies the Length dependency property. /// </summary> public static readonly DependencyProperty LengthProperty = DependencyProperty.Register( "Length", typeof(double), typeof(PixelRuler), new FrameworkPropertyMetadata(20D, FrameworkPropertyMetadataOptions.AffectsRender)); #endregion #region AutoSize /// <summary> /// Gets or sets the AutoSize behavior of the ruler. /// false (default): the lenght of the ruler results from the <see cref="Length"/> property. If the window size is changed, e.g. wider /// than the rulers length, free space is shown at the end of the ruler. No rescaling is done. /// true : the length of the ruler is always adjusted to its actual width. This ensures that the ruler is shown /// for the actual width of the window. /// </summary> public bool AutoSize { get { return (bool)GetValue(AutoSizeProperty); } set { SetValue(AutoSizeProperty, value); this.InvalidateVisual(); } } /// <summary> /// Identifies the AutoSize dependency property. /// </summary> public static readonly DependencyProperty AutoSizeProperty = DependencyProperty.Register( "AutoSize", typeof(bool), typeof(PixelRuler), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender)); #endregion #region Zoom /// <summary> /// Gets or sets the zoom factor for the ruler. The default value is 1.0. /// </summary> public double Zoom { get { return (double)GetValue(ZoomProperty); } set { SetValue(ZoomProperty, value); this.InvalidateVisual(); } } /// <summary> /// Identifies the Zoom dependency property. /// </summary> public static readonly DependencyProperty ZoomProperty = DependencyProperty.Register("Zoom", typeof(double), typeof(PixelRuler), new FrameworkPropertyMetadata((double)1.0, FrameworkPropertyMetadataOptions.AffectsRender)); #endregion #region SmallStep /// <summary> /// Gets or sets the small step for the ruler. The default value is 25.0. /// </summary> public double SmallStep { get { return (double)GetValue(SmallStepProperty); } set { SetValue(SmallStepProperty, value); this.InvalidateVisual(); } } /// <summary> /// Identifies the Zoom dependency property. /// </summary> public static readonly DependencyProperty SmallStepProperty = DependencyProperty.Register("SmallStep", typeof(double), typeof(PixelRuler), new FrameworkPropertyMetadata((double)25.0, FrameworkPropertyMetadataOptions.AffectsRender)); #endregion #region Step /// <summary> /// Gets or sets the step for the ruler. The default value is 100.0. /// </summary> public double Step { get { return (double)GetValue(StepProperty); } set { SetValue(StepProperty, value); this.InvalidateVisual(); } } /// <summary> /// Identifies the Zoom dependency property. /// </summary> public static readonly DependencyProperty StepProperty = DependencyProperty.Register("Step", typeof(double), typeof(PixelRuler), new FrameworkPropertyMetadata((double)100.0, FrameworkPropertyMetadataOptions.AffectsRender)); #endregion #region Chip /// <summary> /// Chip Dependency Property /// </summary> public static readonly DependencyProperty ChipProperty = DependencyProperty.Register("Chip", typeof(double), typeof(PixelRuler), new FrameworkPropertyMetadata((double)-1000, FrameworkPropertyMetadataOptions.AffectsRender)); /// <summary> /// Sets the location of the chip in the units of the ruler. /// So, to set the chip to 100px units the chip needs to be set to 100. /// Use the <see cref="DipHelper"/> class for conversions. /// </summary> public double Chip { get { return (double)GetValue(ChipProperty); } set { SetValue(ChipProperty, value); } } #endregion #region CountShift /// <summary> /// CountShift Dependency Property /// </summary> public static readonly DependencyProperty CountShiftProperty = DependencyProperty.Register("CountShift", typeof(int), typeof(PixelRuler), new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.AffectsRender)); /// <summary> /// By default the counting of numbers starts at zero, this property allows you to shift /// the counting. /// </summary> public int CountShift { get { return (int)GetValue(CountShiftProperty); } set { SetValue(CountShiftProperty, value); } } #endregion #region Marks /// <summary> /// Marks Dependency Property /// </summary> public static readonly DependencyProperty MarksProperty = DependencyProperty.Register("Marks", typeof(MarksLocation), typeof(PixelRuler), new FrameworkPropertyMetadata(MarksLocation.Up, FrameworkPropertyMetadataOptions.AffectsRender)); /// <summary> /// Gets or sets where the marks are shown in the ruler. /// </summary> public MarksLocation Marks { get { return (MarksLocation)GetValue(MarksProperty); } set { SetValue(MarksProperty, value); } } #endregion #endregion #region Constructor static PixelRuler() { HeightProperty.OverrideMetadata(typeof(PixelRuler), new FrameworkPropertyMetadata(20.0)); } public PixelRuler() { SegmentHeight = this.Height - 10; } #endregion #region Methods /// <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 = Length * Zoom; drawingContext.DrawRectangle(null, BorderPen, new Rect(new Point(0.0, 0.0), new Point(xDest, Height))); drawingContext.DrawLine(RedPen, new Point(Chip, 0), new Point(Chip, Height)); for (double dUnit = 0; dUnit < Length; dUnit = SmallStep) { double d = dUnit * this.Zoom; double startHeight; double endHeight; if (Marks == MarksLocation.Up) { startHeight = 0; // Main step or small step? endHeight = ((dUnit % Step == 0) ? SegmentHeight : SegmentHeight / 2); } else { startHeight = Height; // Main step or small step? endHeight = ((dUnit % Step == 0) ? SegmentHeight : SegmentHeight * 1.5); } drawingContext.DrawLine(ThinPen, new Point(d, startHeight), new Point(d, endHeight)); if ((dUnit != 0.0) && (dUnit % Step == 0) && (dUnit < Length)) { FormattedText ft = new FormattedText( (dUnit CountShift).ToString(CultureInfo.CurrentCulture), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("Arial"), DipHelper.PtToDip(6), Brushes.DimGray); ft.SetFontWeight(FontWeights.Regular); ft.TextAlignment = TextAlignment.Center; if (Marks == MarksLocation.Up) drawingContext.DrawText(ft, new Point(d, Height - ft.Height)); else drawingContext.DrawText(ft, new Point(d, Height - SegmentHeight - ft.Height)); } } } #endregion } public enum MarksLocation { Up, Down } /// <summary> /// A helper class for DIP (Device Independent Pixels) conversion and scaling operations. /// </summary> public static class DipHelper { /// <summary> /// Converts font points to DIP (Device Independant Pixels). /// </summary> /// <param name="pt">A font point value.</param> /// <returns>A DIP value.</returns> public static double PtToDip(double pt) { return (pt * 96.0 / 72.0); } /// <summary> /// Gets the system DPI scale factor (compared to 96 dpi). /// From http://blogs.msdn.com/jaimer/archive/2007/03/07/getting-system-dpi-in-wpf-app.aspx /// Should not be called before the Loaded event (else XamlException mat throw) /// </summary> /// <returns>A Point object containing the X- and Y- scale factor.</returns> private static Point GetSystemDpiFactor() { PresentationSource source = PresentationSource.FromVisual(Application.Current.MainWindow); Matrix m = source.CompositionTarget.TransformToDevice; return new Point(m.M11, m.M22); } private const double DpiBase = 96.0; /// <summary> /// Gets the system configured DPI. /// </summary> /// <returns>A Point object containing the X- and Y- DPI.</returns> public static Point GetSystemDpi() { Point sysDpiFactor = GetSystemDpiFactor(); return new Point( sysDpiFactor.X * DpiBase, sysDpiFactor.Y * DpiBase); } } }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论