WPF 使用 DrawingContext 绘制刻度条

 WPF 使用 DrawingContext 绘制刻度条

控件名:Ruler

作者:WPFDevelopersOrg

原文链接:   https://github.com/WPFDevelopersOrg/WPFDevelopers

  • 框架使用大于等于.NET40

  • Visual Studio 2022;

  • 项目使用 MIT 开源许可协议;

  • 定义Interval步长、SpanInterval间隔步长、MiddleMask中间步长。

  • 当步长/ 间隔步长= 需要绘制的小刻度。

  • 循环绘制小刻度,判断当前j并取中间步长的模,如果模!=零就绘制中高线。

  • StartValue 开始绘制刻度到EndValue 结束刻度。

  • CurrentGeometry 重新绘制当前刻度的Path值。

  • CurrentValue 当前值如果发生变化时则去重新CurrentGeometry

1) 准备Ruler.cs如下:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;namespace WPFDevelopers.Controls
{public class Ruler : Control{public static readonly DependencyProperty IntervalProperty =DependencyProperty.Register("Interval", typeof(double), typeof(Ruler), new UIPropertyMetadata(30.0));public static readonly DependencyProperty SpanIntervalProperty =DependencyProperty.Register("SpanInterval", typeof(double), typeof(Ruler), new UIPropertyMetadata(5.0));public static readonly DependencyProperty MiddleMaskProperty =DependencyProperty.Register("MiddleMask", typeof(int), typeof(Ruler), new UIPropertyMetadata(2));public static readonly DependencyProperty CurrentValueProperty =DependencyProperty.Register("CurrentValue", typeof(double), typeof(Ruler),new UIPropertyMetadata(OnCurrentValueChanged));public static readonly DependencyProperty StartValueProperty =DependencyProperty.Register("StartValue", typeof(double), typeof(Ruler), new UIPropertyMetadata(120.0));public static readonly DependencyProperty EndValueProperty =DependencyProperty.Register("EndValue", typeof(double), typeof(Ruler), new UIPropertyMetadata(240.0));public static readonly DependencyProperty CurrentGeometryProperty =DependencyProperty.Register("CurrentGeometry", typeof(Geometry), typeof(Ruler),new PropertyMetadata(Geometry.Parse("M 257,0 257,25 264,49 250,49 257,25")));static Ruler(){DefaultStyleKeyProperty.OverrideMetadata(typeof(Ruler), new FrameworkPropertyMetadata(typeof(Ruler)));}public Ruler(){Loaded += Ruler_Loaded;}public double Interval{get => (double)GetValue(IntervalProperty);set => SetValue(IntervalProperty, value);}public double SpanInterval{get => (double)GetValue(SpanIntervalProperty);set => SetValue(SpanIntervalProperty, value);}public int MiddleMask{get => (int)GetValue(MiddleMaskProperty);set => SetValue(MiddleMaskProperty, value);}public double CurrentValue{get => (double)GetValue(CurrentValueProperty);set{SetValue(CurrentValueProperty, value);PaintPath();}}public double StartValue{get => (double)GetValue(StartValueProperty);set => SetValue(StartValueProperty, value);}public double EndValue{get => (double)GetValue(EndValueProperty);set => SetValue(EndValueProperty, value);}public Geometry CurrentGeometry{get => (Geometry)GetValue(CurrentGeometryProperty);set => SetValue(CurrentGeometryProperty, value);}private static void OnCurrentValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e){var ruler = d as Ruler;ruler.CurrentValue = Convert.ToDouble(e.NewValue);}protected override void OnRender(DrawingContext drawingContext){RenderOptions.SetEdgeMode(this, EdgeMode.Aliased);var nextLineValue = 0d;var one_Width = ActualWidth / ((EndValue - StartValue) / Interval);for (var i = 0; i <= (EndValue - StartValue) / Interval; i++){var numberText = DrawingContextHelper.GetFormattedText((StartValue + i * Interval).ToString(),(Brush)DrawingContextHelper.BrushConverter.ConvertFromString("#FFFFFF"), FlowDirection.LeftToRight,10);drawingContext.DrawText(numberText, new Point(i * one_Width - 8, 0));drawingContext.DrawLine(new Pen(new SolidColorBrush(Colors.White), 1), new Point(i * one_Width, 25),new Point(i * one_Width, ActualHeight - 2));var cnt = Interval / SpanInterval;for (var j = 1; j <= cnt; j++)if (j % MiddleMask == 0)drawingContext.DrawLine(new Pen(new SolidColorBrush(Colors.White), 1),new Point(j * (one_Width / cnt) + nextLineValue, ActualHeight - 2),new Point(j * (one_Width / cnt) + nextLineValue, ActualHeight - 10));elsedrawingContext.DrawLine(new Pen(new SolidColorBrush(Colors.White), 1),new Point(j * (one_Width / cnt) + nextLineValue, ActualHeight - 2),new Point(j * (one_Width / cnt) + nextLineValue, ActualHeight - 5));nextLineValue = i * one_Width;}}private void Ruler_Loaded(object sender, RoutedEventArgs e){PaintPath();}private void PaintPath(){var d_Value = CurrentValue - StartValue;var one_Value = ActualWidth / (EndValue - StartValue);var x_Point = one_Value * d_Value + ((double)Parent.GetValue(ActualWidthProperty) - ActualWidth) / 2d;CurrentGeometry =Geometry.Parse($"M {x_Point},0 {x_Point},25 {x_Point + 7},49 {x_Point - 7},49 {x_Point},25");}}
}

2) 使用RulerControlExample.xaml.cs如下:

<UserControl x:Class="WPFDevelopers.Samples.ExampleViews.RulerControlExample"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:WPFDevelopers.Samples.ExampleViews"xmlns:wpfdev="https://github.com/WPFDevelopersOrg/WPFDevelopers"mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"><Grid><Slider x:Name="PART_Slider" IsSnapToTickEnabled="True"Value="40"Minimum="10"Maximum="210"/><UniformGrid Rows="3"><Grid Background="{StaticResource CircularDualSolidColorBrush}" Height="51" Margin="40,0"><Path Stroke="{StaticResource SuccessPressedSolidColorBrush}" StrokeThickness="1" Fill="{StaticResource SuccessPressedSolidColorBrush}"Data="{Binding ElementName=PART_Ruler, Path=CurrentGeometry,Mode=TwoWay}" /><wpfdev:Ruler x:Name="PART_Ruler" Margin="40,0" Interval="20" StartValue="10" EndValue="210"CurrentValue="{Binding ElementName=PART_Slider,Path=Value,Mode=TwoWay}"/></Grid><Grid Background="{StaticResource DangerPressedSolidColorBrush}" Height="51" Margin="40,0"><Path Stroke="{StaticResource SuccessPressedSolidColorBrush}" StrokeThickness="1" Fill="{StaticResource SuccessPressedSolidColorBrush}"Data="{Binding ElementName=PART_Ruler1, Path=CurrentGeometry,Mode=TwoWay}" /><wpfdev:Ruler x:Name="PART_Ruler1" Margin="40,0" Interval="20" StartValue="10" EndValue="210"CurrentValue="{Binding ElementName=PART_Slider,Path=Value,Mode=TwoWay}"/></Grid><Grid Background="{StaticResource WarningPressedSolidColorBrush}" Height="51" Margin="40,0"><Path Stroke="{StaticResource SuccessPressedSolidColorBrush}" StrokeThickness="1" Fill="{StaticResource SuccessPressedSolidColorBrush}"Data="{Binding ElementName=PART_Ruler2, Path=CurrentGeometry,Mode=TwoWay}" /><wpfdev:Ruler x:Name="PART_Ruler2" Margin="40,0" Interval="20" StartValue="10" EndValue="210"CurrentValue="{Binding ElementName=PART_Slider,Path=Value,Mode=TwoWay}"/></Grid></UniformGrid></Grid>
</UserControl>

 鸣谢 - 帅嘉欣5f5dc76d2f1e8b523829b95e67de89b2.gif

Github|RulerControlExample[1]
码云|RulerControlExample[2]

参考资料

[1]

Github|RulerControlExample: https://github.com/WPFDevelopersOrg/WPFDevelopers/blob/master/src/WPFDevelopers.Samples/ExampleViews/RulerControlExample.xaml

[2]

码云|RulerControlExample: https://gitee.com/WPFDevelopersOrg/WPFDevelopers/blob/master/src/WPFDevelopers.Samples/ExampleViews/RulerControlExample.xaml

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/282773.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

纯css实现漂亮又健壮的tooltip

前言 tooltip的实现有多种方式&#xff0c;下面是一个tooltip成长史。 预览 https://codepen.io/moddx/pen/... 原始版 最简单的莫过于就用原始title属性&#xff0c;像这样&#xff1a; <button title"tips">button</button> 缺点是体验是差了点&#x…

个人中心的html,个人中心.html

&#xfeff;个人中心$axure.utils.getTransparentGifPath function() { return resources/images/transparent.gif; };$axure.utils.getOtherPath function() { return resources/Other.html; };$axure.utils.getReloadPath function() { return resources/reload.html; };…

使用CMD命令修改Windows本地账户密码

2019独角兽企业重金招聘Python工程师标准>>> 一、以管理员身份运行cmd命令 二、在命令提示符窗口中输入命令符&#xff1a;net user Administrator 123&#xff0c;然后按回车键“Enter”。(Administrator是你的win8用户名&#xff0c;123是重新设置的密码。) ​ 三…

Android 编译时:m、mm、mmm、mma、mmma的区别

m&#xff1a;编译整个安卓系统 makes from the top of the tree mm&#xff1a;编译当前目录下的模块&#xff0c;当前目录下需要有Android.mk这个makefile文件&#xff0c;否则就往上找最近的Android.mk文件。 builds all of the moudles in the current directory mma&#…

java线程安全问题原因及解决办法

1.为什么会出现线程安全问题 计算机系统资源分配的单位为进程&#xff0c;同一个进程中允许多个线程并发执行&#xff0c;并且多个线程会共享进程范围内的资源&#xff1a;例如内存地址。当多个线程并发访问同一个内存地址并且内存地址保存的值是可变的时候可能会发生线程安全问…

html语言怎么添加图片,我想问你一下,你是怎么在html中插入本地图片?非常感谢...

满意答案小蜜蜂手工2013.10.03采纳率&#xff1a;43% 等级&#xff1a;12已帮助&#xff1a;7929人img{float:right}在下面的段落中&#xff0c;我们添加了一个样式为 float:right 的图像。结果是这个图像会浮动到段落的右侧。This is some text. This is some text. This i…

数组实现矩阵逐层向内层加1

package java1701;public class javaMain { public static void main(String[] args) { // 逐层加 // 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 // 1 2 2 2 2 1 1 2 2 2 1 1 2 2 1 1 2 1 // 1 2 3 3 2 1 1 2 3 2 1 1 2 2 1 1 1 1 // 1 2 3 …

EntityFrameworkCore上下文如何实现继承?

【导读】如果我们存在基础设施服务和其他服务&#xff0c;我们会定义属于基础设施服务的上下文以及其他服务的上下文&#xff0c; 而且会独立部署&#xff0c;此时其他服务需要使用基础服务&#xff0c;我们都会暴露基础服务接口给到其他服务调用&#xff0c;这也是常规操作若在…

Unity 游戏框架搭建 (九) 减少加班利器-QConsole

为毛要实现这个工具? 在我小时候,每当游戏在真机运行时,我们看到的日志是这样的。 没高亮啊,还有乱七八糟的堆栈信息,好干扰日志查看,好影响心情。 还有就是必须始终连着usb线啊&#xff0c;我想要想躺着测试。。。 以上种种原因,QConsole诞生了。 如何使用? 使用方式和QLog…

android蓝牙多次后,android – 如何防止BluetoothGattCallback一次多次执行

我的服务有一个BluetoothGattCallback实例public class MyService extends Service {private BluetoothGattCallback callback;Overridepublic void onCreate() {super.onCreate();callback new BluetoothGattCallback() {Overridepublic synchronized void onConnectionState…

美观又实用,10 款强大的开源 Javascript 图表库

2019独角兽企业重金招聘Python工程师标准>>> 随着发展&#xff0c;现代 Web 设计在改善体验和功能的同时&#xff0c;对于美观的追求也越来越高&#xff0c;可视化、交互式、动态等元素和效果似乎已成为标配。 以下是为开发者推荐的 10 款开源 Javascript 图表库&am…

EF CORE 7 RC1 发布

原文链接&#xff1a;https://devblogs.microsoft.com/dotnet/announcing-ef7-rc1/[1]原文作者&#xff1a;Jeremy Likness翻译&#xff1a;沙漠尽头的狼(谷歌翻译加持)Entity Framework Core 7 (EF7) Release Candidate 1 已发布&#xff01;该团队专注于解决缺陷、小幅改进以…

0 重新学习Ubuntu -- 这一段没怎么学习

在完成了前面的几个学习后&#xff0c;再没有进行系统的学习。 虽然在真机上安装系统&#xff0c;每天都打开&#xff0c;完成以下的工作&#xff1a; 升级软件用来查看相关的网站在Ubuntu上&#xff0c;现在可以完成办公、上网、娱乐。 但专业的学习&#xff0c;例如编程方面进…

自定义地图怎么做成html,自定义html为谷歌地图制作标记

好吧&#xff0c;似乎Custom Overlays会做我想要的。这是ping层&#xff1a;function PingLayer(bounds, map) {this.bounds bounds;this.setMap(map);}PingLayer.prototype new google.maps.OverlayView();PingLayer.prototype.onAdd function() {var div document.create…

HDU5248:序列变换(二分)

序列变换 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1348 Accepted Submission(s): 593Problem Description给定序列A{A1,A2,...,An}, 要求改变序列A中的某些元素&#xff0c;形成一个严格单调的序列B&am…

微服务太分散?使用Fundebug集中式bug监控

摘要&#xff1a; 微服务日志分散&#xff0c;可以使用Fundebug的异常监控将它们集中起来。 当一个项目复杂到一定程度&#xff0c;功能越来越多&#xff0c;随之对应的模块也越来越多。 如果都放在一个大的项目下面&#xff0c;共同开发&#xff0c;整合发布&#xff0c;那么会…

html404页面怎么添加,网站要如何设置自定义404页面?

之前我们讲述过网站设置404页面对于优化或是用户体验的重要意义&#xff0c;大家可移步到《网站为什么要设置404页面》查看&#xff0c;今天我们讲解的是网站要如何设置自己的404页面。现在大多数空间商都有了404设置的功能&#xff0c;我们可将404页面上传至空间里面&#xff…

设计模式之——工厂方法模式

1、工厂方法模式&#xff08;Factory Method&#xff09;工厂方法模式分为三种&#xff1a;11、普通工厂模式&#xff0c;就是建立一个工厂类&#xff0c;对实现了同一接口的一些类进行实例的创建。首先看下关系图&#xff1a;举例如下&#xff1a;&#xff08;我们举一个发送邮…

记一次性能故障排查

最近一次公司服务出了一些性能的问题&#xff0c;主要是内存不释放。领到任务后就开始展开工作。项目是用.net core 6写的&#xff0c;在框上应该不会有什么问题&#xff0c;这是大背景。另外服务是部署在k8s上的&#xff0c;于是就和性能测试人员&#xff0c;开发人员搭测试环…

html单选框 点击取消选中,radio单选框再点击取消选中

html:html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">单选框选项a选项b选项c选项dcheckradio.js://参数&#xff1a;obj为当前点击的radio对象function onClickRadioStyle(obj){var…