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.pswp.cn/news/282773.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/282773.shtml
英文地址,請注明出處:http://en.pswp.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…