欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

WPF使用DrawingContext實(shí)現(xiàn)繪制刻度條

 更新時(shí)間:2022年09月15日 09:48:37   作者:驚鏵  
這篇文章主要為大家詳細(xì)介紹了如何利用WPF DrawingContext實(shí)現(xiàn)繪制刻度條,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,感興趣的小伙伴可以了解一下

WPF 使用 DrawingContext 繪制刻度條

  • 框架使用大于等于.NET40;
  • Visual Studio 2022;
  • 項(xiàng)目使用 MIT 開源許可協(xié)議;
  • 定義Interval步長(zhǎng)、SpanInterval間隔步長(zhǎng)、MiddleMask中間步長(zhǎng)。
  • 當(dāng)步長(zhǎng)/ 間隔步長(zhǎng)= 需要繪制的小刻度。
  • 循環(huán)繪制小刻度,判斷當(dāng)前j并取中間步長(zhǎng)的模,如果模!=零就繪制中高線。
  • StartValue 開始繪制刻度到EndValue 結(jié)束刻度。
  • CurrentGeometry 重新繪制當(dāng)前刻度的Path值。
  • CurrentValue 當(dāng)前值如果發(fā)生變化時(shí)則去重新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>

以上就是WPF使用DrawingContext實(shí)現(xiàn)繪制刻度條的詳細(xì)內(nèi)容,更多關(guān)于WPF刻度條的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 深入理解C#中new、override、virtual關(guān)鍵字的區(qū)別

    深入理解C#中new、override、virtual關(guān)鍵字的區(qū)別

    下面小編就為大家?guī)?lái)一篇深入理解C#中new、override、virtual關(guān)鍵字的區(qū)別。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-06-06
  • 關(guān)于C#版Nebula客戶端編譯的問(wèn)題

    關(guān)于C#版Nebula客戶端編譯的問(wèn)題

    這篇文章主要介紹了C#版Nebula客戶端編譯的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-07-07
  • C#實(shí)現(xiàn)文件與字符串互轉(zhuǎn)的方法詳解

    C#實(shí)現(xiàn)文件與字符串互轉(zhuǎn)的方法詳解

    這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)文件與字符串互轉(zhuǎn)效果,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定幫助,需要的可以參考一下
    2022-08-08
  • C#如何通過(guò)RFC連接sap系統(tǒng)

    C#如何通過(guò)RFC連接sap系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C#如何通過(guò)RFC連接sap系統(tǒng)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • C#中增強(qiáng)類功能的幾種方式詳解

    C#中增強(qiáng)類功能的幾種方式詳解

    這篇文章主要給大家介紹了關(guān)于C#中增強(qiáng)類功能的幾種方式的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-12-12
  • C# WinForm導(dǎo)出Excel方法介紹

    C# WinForm導(dǎo)出Excel方法介紹

    在.NET應(yīng)用中,導(dǎo)出Excel是很常見(jiàn)的需求,導(dǎo)出Excel報(bào)表大致有以下三種方式:Office PIA,文件流和NPOI開源庫(kù),本文只介紹前兩種方式
    2013-12-12
  • WPF自定義MenuItem樣式的實(shí)現(xiàn)方法

    WPF自定義MenuItem樣式的實(shí)現(xiàn)方法

    這篇文章主要給大家介紹了關(guān)于WPF自定義MenuItem樣式的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用WPF具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • c# yield提高代碼性能和可讀性

    c# yield提高代碼性能和可讀性

    Yield可以讓你的代碼更加高效并擁有更高的可讀性,我想已經(jīng)沒(méi)有什么借口可以阻止我們學(xué)習(xí)和使用yield
    2013-12-12
  • C#使用ZBar實(shí)現(xiàn)識(shí)別條形碼

    C#使用ZBar實(shí)現(xiàn)識(shí)別條形碼

    目前主流的識(shí)別庫(kù)主要有ZXing.NET和ZBar,本文主要介紹的是如何使用ZBar庫(kù)實(shí)現(xiàn)識(shí)別條形碼功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2023-07-07
  • 淺談C#設(shè)計(jì)模式之開放封閉原則

    淺談C#設(shè)計(jì)模式之開放封閉原則

    這篇文章主要介紹了淺談C#設(shè)計(jì)模式之開放封閉原則,需要的朋友可以參考下
    2014-12-12

最新評(píng)論