網(wǎng)站首頁 編程語言 正文
WPF 使用 DrawingContext 繪制刻度條
- 框架使用大于等于
.NET40
; -
Visual Studio 2022
; - 項目使用?MIT?開源許可協(xié)議;
- 定義
Interval
步長、SpanInterval
間隔步長、MiddleMask
中間步長。 - 當(dāng)步長
/
?間隔步長=
?需要繪制的小刻度。 - 循環(huán)繪制小刻度,判斷當(dāng)前
j
并取中間步長的模,如果模!=
零就繪制中高線。 - 從
StartValue
?開始繪制刻度到EndValue
?結(jié)束刻度。 -
CurrentGeometry
?重新繪制當(dāng)前刻度的Path
值。 -
CurrentValue
?當(dāng)前值如果發(fā)生變化時則去重新CurrentGeometry
?。
1) 準(zhǔn)備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)); ????????????????????else ????????????????????????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?-?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>
原文鏈接:https://mp.weixin.qq.com/s/MUHo6TIlDl1xYM_ERa4MNQ
相關(guān)推薦
- 2022-10-02 C#使用is、as關(guān)鍵字以及顯式強轉(zhuǎn)實現(xiàn)引用類型轉(zhuǎn)換_C#教程
- 2022-11-04 SQL?Server還原完整備份和差異備份的操作過程_MsSql
- 2022-04-12 .NET 6 ‘Unable to configure HTTPS endpoint...
- 2022-09-26 ASP.NET?Core?6最小API中使用日志和DI示例詳解_ASP.NET
- 2022-05-14 一起來學(xué)習(xí)React元素的創(chuàng)建和渲染_React
- 2022-11-06 python中defaultdict用法實例詳解_python
- 2022-10-06 Django數(shù)據(jù)映射(一對一,一對多,多對多)_python
- 2022-10-30 解決docker訪問外部https數(shù)字證書問題_docker
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支