wpf 水波進度條 用戶控制項

来源:https://www.cnblogs.com/wuyaxiansheng/archive/2019/03/05/10478574.html
-Advertisement-
Play Games

之前看公司web前端做了個 圓形的水波紋 進度條,就想用wpf 做一個,奈何自己太菜 一直做不出來,在看過 “普通的地球人” 的 “ WPF實現三星手機充電界面 博客之後 我也來照葫蘆畫個瓢。 廢話不多說 先貼一下效果圖 雖然樣子 low 了些 但是基本滿足我的需求了,下麵是代碼 前端 後臺 美中不 ...


之前看公司web前端做了個 圓形的水波紋 進度條,就想用wpf 做一個,奈何自己太菜 一直做不出來,在看過 “普通的地球人” 的 “

WPF實現三星手機充電界面 博客之後 我也來照葫蘆畫個瓢。

廢話不多說 先貼一下效果圖

雖然樣子 low 了些 但是基本滿足我的需求了,下麵是代碼

前端

<UserControl x:Class="WaveProgress.UserControl.WaveProgressControl"
             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:WaveProgress.UserControl"
             mc:Ignorable="d" 
             Height="150" Width="150" x:Name="wave_control">
    <UserControl.Resources>
        <Storyboard x:Key="WaterStoryboard">
            <PointAnimation Storyboard.TargetName="bs_Water" DesiredFrameRate="20" Storyboard.TargetProperty="Point1" From="90,60" To="90,90" Duration="00:00:2" AutoReverse="True" RepeatBehavior="Forever"></PointAnimation>
            <PointAnimation Storyboard.TargetName="bs_Water" DesiredFrameRate="20" Storyboard.TargetProperty="Point2" From="100,110" To="100,95" Duration="00:00:1.8" AutoReverse="True" RepeatBehavior="Forever"></PointAnimation>
        </Storyboard>
    </UserControl.Resources>
    <Grid Width="{Binding ElementName=wave_control,Path=Width}" Height="{Binding ElementName=wave_control,Path=Height}" 
          Background="{Binding WaveProgressBackground,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
        <Grid.Clip>
            <EllipseGeometry Center="75,75" RadiusX="75" RadiusY="75" ></EllipseGeometry>
        </Grid.Clip>
        <StackPanel Width="150" VerticalAlignment="Bottom">
            <Path Fill="{Binding WavePorgressBarColor,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" >
                <Path.Data>
                    <PathGeometry FillRule="EvenOdd" >
                        <PathFigure StartPoint="0,90" >
                            <BezierSegment x:Name="bs_Water" Point1="90,60" Point2="100,110" Point3="150,90"></BezierSegment>
                            <PolyLineSegment Points="150,100 0,100"></PolyLineSegment>
                        </PathFigure>
                    </PathGeometry>
                </Path.Data>
                <Path.Triggers>
                    <EventTrigger RoutedEvent="Path.Loaded">
                        <BeginStoryboard Storyboard="{StaticResource WaterStoryboard}"></BeginStoryboard>
                    </EventTrigger>
                </Path.Triggers>
            </Path>
            <Rectangle Height="{Binding WaveProgressHeight,UpdateSourceTrigger=PropertyChanged}" Fill="{Binding WavePorgressBarColor,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
        </StackPanel>
        <Ellipse VerticalAlignment="Bottom" Width="150" Height="150" Stroke="{Binding WaveBorderBrush,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Fill="Transparent" 
                 StrokeThickness="{Binding WaveBorderThickness,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
        <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="22" Foreground="{Binding TextColor,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
            <Run Text="{Binding DisPlayValue,UpdateSourceTrigger=PropertyChanged}"></Run>
            <Run Text="%"></Run>
        </TextBlock>
    </Grid>
</UserControl>

後臺

using System.Globalization;
using System.Windows;
using System.Windows.Media;

namespace WaveProgress.UserControl
{
    /// <summary>
    /// WaveProgressControl.xaml 的交互邏輯
    /// </summary>
    public partial class WaveProgressControl : System.Windows.Controls.UserControl
    {
        public WaveProgressControl()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        public static readonly DependencyProperty WaveProgressBackgroundProperty = DependencyProperty.Register(
            "WaveProgressBackground", typeof(SolidColorBrush), typeof(WaveProgressControl), new PropertyMetadata(Brushes.White));

        /// <summary>
        /// 進度條背景色
        /// </summary>
        public SolidColorBrush WaveProgressBackground
        {
            get { return (SolidColorBrush) GetValue(WaveProgressBackgroundProperty); }
            set { SetValue(WaveProgressBackgroundProperty, value); }
        }

        public static readonly DependencyProperty WaveBorderBrushProperty = DependencyProperty.Register(
            "WaveBorderBrush", typeof(SolidColorBrush), typeof(WaveProgressControl), new PropertyMetadata(Brushes.Blue));
        /// <summary>
        /// 邊框顏色
        /// </summary>
        public SolidColorBrush WaveBorderBrush
        {
            get { return (SolidColorBrush) GetValue(WaveBorderBrushProperty); }
            set { SetValue(WaveBorderBrushProperty, value); }
        }

        public static readonly DependencyProperty WaveBorderThicknessProperty = DependencyProperty.Register(
            "WaveBorderThickness", typeof(double), typeof(WaveProgressControl), new PropertyMetadata(2.0));

        /// <summary>
        /// 邊框粗細
        /// </summary>
        public double WaveBorderThickness
        {
            get { return (double) GetValue(WaveBorderThicknessProperty); }
            set { SetValue(WaveBorderThicknessProperty, value); }
        }

     
        public static readonly DependencyProperty WavePorgressBarColorProperty = DependencyProperty.Register(
            "WavePorgressBarColor", typeof(SolidColorBrush), typeof(WaveProgressControl), new PropertyMetadata(Brushes.Red));
        /// <summary>
        /// 進度條顏色
        /// </summary>
        public SolidColorBrush WavePorgressBarColor
        {
            get { return (SolidColorBrush) GetValue(WavePorgressBarColorProperty); }
            set { SetValue(WavePorgressBarColorProperty, value); }
        }

        public static readonly DependencyProperty TextColorProperty = DependencyProperty.Register(
            "TextColor", typeof(SolidColorBrush), typeof(WaveProgressControl), new PropertyMetadata(Brushes.Black));
        /// <summary>
        /// 文字顏色
        /// </summary>
        public SolidColorBrush TextColor
        {
            get { return (SolidColorBrush) GetValue(TextColorProperty); }
            set { SetValue(TextColorProperty, value); }
        }

        public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
            "Value", typeof(double), typeof(WaveProgressControl), new PropertyMetadata(default(double)));

        /// <summary>
        /// 當前進度
        /// </summary>
        public double Value
        {
            get { return (double) GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        public static readonly DependencyProperty MaxValueProperty = DependencyProperty.Register(
            "MaxValue", typeof(double), typeof(WaveProgressControl), new PropertyMetadata(default(double)));

        public double MaxValue
        {
            get { return (double) GetValue(MaxValueProperty); }
            set { SetValue(MaxValueProperty, value); }
        }

        public static readonly DependencyProperty DisPlayValueProperty = DependencyProperty.Register(
            "DisPlayValue", typeof(string), typeof(WaveProgressControl), new PropertyMetadata("0"));

        public string DisPlayValue
        {
            get { return (string) GetValue(DisPlayValueProperty); }
            set { SetValue(DisPlayValueProperty, value); }
        }

        public static readonly DependencyProperty WaveProgressHeightProperty = DependencyProperty.Register(
            "WaveProgressHeight", typeof(double), typeof(WaveProgressControl), new PropertyMetadata(default(double)));

        /// <summary>
        /// 次屬性不要手動設置
        /// </summary>
        public double WaveProgressHeight
        {
            get { return (double) GetValue(WaveProgressHeightProperty); }
            set { SetValue(WaveProgressHeightProperty, value); }
        }

        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            base.OnPropertyChanged(e);
            if (e.Property == ValueProperty)
            {
                double bl = Value / MaxValue;
                WaveProgressHeight = 140 * bl;
                DisPlayValue = (bl * 100).ToString(CultureInfo.InvariantCulture);
            }
        }
    }

}

 

美中不足的是:

1、大小是我寫死了的,因為裡面那個水波是用path 寫的 是個固定的

2、仔細看 中間有條白色的線(等有時間在解決吧)

 

學習到的知識:

1、學會用貝塞爾曲線,和它的動畫

2、學會了Clip剪裁

3、看大佬的文章果然受益匪淺


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • FluentValidation 是一個基於 .NET 開發的驗證框架,開源免費,而且優雅,支持鏈式操作,易於理解,功能完善,還是可與 MVC5、WebApi 和 ASP.NET Core深度集成,組件內提供十幾種常用驗證器,可擴展性好,支持自定義驗證器,支持本地化多語言。 要使用驗證框架,需要在項 ...
  • 配置,幾乎所有的應用程式都離不開它。.Net Framework時代我們使用App.config、Web.config,到了.Net Core的時代我們使用appsettings.json,這些我們再熟悉不過了。然而到了容器化、微服務的時代,這些本地文件配置有的時候就不太合適了。當你把本地部署的服務 ...
  • 所有的類型都從 System.Object 派生 1、類型System.Object 運行時要求每一個類型都是從System.Object派生,如果沒有顯示的寫明繼承關係,最後都會預設的從System.Object來派生。System.Object提供了四個公用方法和兩個收保護方法: 2、new 操 ...
  • c#方法的重載:分為實例方法重載和靜態方法重載倆種 1.實例方法重載的調用特點 首先寫三個Add方法和三個Sub方法 特點:編譯器將自動更具方法的參數個數和類型自動匹配類的對應方法。 2.實例方法重載的好處 1.減少類的對外介面(只顯示一個方法),降低類的複雜度。 2.便於用戶使用(相同功能的方法名 ...
  • 想寫博客不知道從何處開始,就從回憶開始吧. 第一個就從自定義日曆控制項開始 產生背景: 大概2015年時候有個項目要用到日曆,用預設日曆展示給用戶看,用戶毫不客氣都說界面太醜,最好做成像百度日曆那樣方便使用。 花費了一些時間感覺模仿相似度達到95%,模糊不清楚是因為圖片被壓縮了,瀏覽器中看圖片還是挺正... ...
  • 在項目實踐中,我們 可能會遇到需要將一些控制項上顯示的內容只顯示一段時間過後清空。 下麵我們來實現這種操作: 首先需要註意的是:在wpf中涉及到界面操作的話,一定要使用定時器DispatcherTime,DispatcherTimer是為wpf專門設計的,不然的話使用其他種類的定時器會提示界面資源被其 ...
  • 之前做過一個MES系統,發一些裡面的截圖。如果有朋友也用這個框架。或者有興趣可以一起學習學習。使用開發工具VS2013,資料庫SqlServer2008和Oracle11C。插件dev15.2,開發模式基於MVC三層模式。部分截圖。 資料庫連接工具,保存到配置文件。 ORM,BLL,DAL等生成工具 ...
  • 一 在negut添加EPPlus.dll庫文件。 之前有寫過直接只用Microsoft.Office.Interop.Excel 導出EXCEL,並生成Chart表,非常耗時,所以找了個EPPlus控制項。 二 代碼部分 三 效果 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...