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);
}
}
}
资源文件列表
WpfApplication1/WpfApplication1/App.config , 187
WpfApplication1/WpfApplication1/App.xaml , 326
WpfApplication1/WpfApplication1/App.xaml.cs , 342
WpfApplication1/WpfApplication1/bin/Debug/DevExpress.Images.v15.2.dll , 3297576
WpfApplication1/WpfApplication1/bin/Debug/WpfApplication1.exe , 14336
WpfApplication1/WpfApplication1/bin/Debug/WpfApplication1.exe.config , 187
WpfApplication1/WpfApplication1/bin/Debug/WpfApplication1.pdb , 46592
WpfApplication1/WpfApplication1/bin/Debug/WpfApplication1.vshost.exe , 24224
WpfApplication1/WpfApplication1/bin/Debug/WpfApplication1.vshost.exe.config , 187
WpfApplication1/WpfApplication1/bin/Debug/WpfApplication1.vshost.exe.manifest , 490
WpfApplication1/WpfApplication1/MainWindow.xaml , 1082
WpfApplication1/WpfApplication1/MainWindow.xaml.cs , 661
WpfApplication1/WpfApplication1/obj/Debug/App.g.cs , 2325
WpfApplication1/WpfApplication1/obj/Debug/App.g.i.cs , 2325
WpfApplication1/WpfApplication1/obj/Debug/DesignTimeResolveAssemblyReferences.cache , 92526
WpfApplication1/WpfApplication1/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache , 7072
WpfApplication1/WpfApplication1/obj/Debug/GeneratedInternalTypeHelper.g.cs , 7
WpfApplication1/WpfApplication1/obj/Debug/GeneratedInternalTypeHelper.g.i.cs , 3015
WpfApplication1/WpfApplication1/obj/Debug/MainWindow.baml , 1397
WpfApplication1/WpfApplication1/obj/Debug/MainWindow.g.cs , 3521
WpfApplication1/WpfApplication1/obj/Debug/MainWindow.g.i.cs , 3521
WpfApplication1/WpfApplication1/obj/Debug/TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs , 0
WpfApplication1/WpfApplication1/obj/Debug/TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs , 0
WpfApplication1/WpfApplication1/obj/Debug/TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs , 0
WpfApplication1/WpfApplication1/obj/Debug/TempPE/Properties.Resources.Designer.cs.dll , 4608
WpfApplication1/WpfApplication1/obj/Debug/WpfApplication1.csproj.FileListAbsolute.txt , 2367
WpfApplication1/WpfApplication1/obj/Debug/WpfApplication1.csproj.GenerateResource.Cache , 919
WpfApplication1/WpfApplication1/obj/Debug/WpfApplication1.csprojResolveAssemblyReference.cache , 2354
WpfApplication1/WpfApplication1/obj/Debug/WpfApplication1.exe , 14336
WpfApplication1/WpfApplication1/obj/Debug/wpfapplication1.exe.licenses , 715
WpfApplication1/WpfApplication1/obj/Debug/WpfApplication1.g.resources , 1625
WpfApplication1/WpfApplication1/obj/Debug/WpfApplication1.pdb , 46592
WpfApplication1/WpfApplication1/obj/Debug/WpfApplication1.Properties.Resources.resources , 180
WpfApplication1/WpfApplication1/obj/Debug/WpfApplication1_MarkupCompile.cache , 262
WpfApplication1/WpfApplication1/obj/Debug/WpfApplication1_MarkupCompile.i.cache , 262
WpfApplication1/WpfApplication1/obj/Debug/WpfApplication1_MarkupCompile.lref , 155
WpfApplication1/WpfApplication1/PixelRuler.cs , 13040
WpfApplication1/WpfApplication1/Properties/AssemblyInfo.cs , 2211
WpfApplication1/WpfApplication1/Properties/Licenses.licx , 670
WpfApplication1/WpfApplication1/Properties/Resources.Designer.cs , 2882
WpfApplication1/WpfApplication1/Properties/Resources.resx , 5612
WpfApplication1/WpfApplication1/Properties/Settings.Designer.cs , 1102
WpfApplication1/WpfApplication1/Properties/Settings.settings , 201
WpfApplication1/WpfApplication1/WpfApplication1.csproj , 4469
WpfApplication1/WpfApplication1.sln , 1014
WpfApplication1/WpfApplication1.v12.suo , 51200
