天天看點

WPF自定義動畫控件 風機

原文:

WPF自定義動畫控件 風機

一:建立WPF項目

二:在項目下添加檔案Themes,在此檔案下添加新項 ”資源詞典“取名為 Generic.xaml  注意大小寫,之前遇到因為大小寫不對應,導緻出錯的情況

Generic.xaml檔案代碼如下:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

                    xmlns:local="clr-namespace:YinFengJi">

    <Style TargetType="{x:Type local:YinFengJ}">

        <Setter Property="Template">

            <Setter.Value>

                <ControlTemplate TargetType="{x:Type local:YinFengJ}">

                    <Viewbox>

                        <Canvas Height="100" Width="80" Background="Transparent">

                            <!--給水泵 右側 通道-->

                            <Rectangle Canvas.Left="35" Canvas.Top="14.1" Width="28.3" Height="14.5">

                                <Rectangle.Fill>

                                    <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">

                                        <GradientStop Offset="0" Color="#646768"></GradientStop>

                                        <GradientStop Offset="0.2" Color="#EFEEF3"></GradientStop>

                                        <GradientStop Offset="0.5" Color="#EFEEF3"></GradientStop>

                                        <GradientStop Offset="1" Color="#646768"></GradientStop>

                                    </LinearGradientBrush>

                                </Rectangle.Fill>

                            </Rectangle>

                            <!--給水泵 右側 關節-->

                            <Path Stroke="#7E7E7E" StrokeThickness="1"   Data="M71.4,27.7c0,1.6-2,2.9-4.5,2.9l0,0c-2.5,0-4.5-1.3-4.5-2.9v-12.7c0-1.6,2-2.9,4.5-2.9l0,0

 c2.5,0,4.5,1.3,4.5,2.9V27.7z">

                                <Path.Fill>

                                </Path.Fill>

                            </Path>

                            <!--給水泵 外邊框-->

                            <Ellipse  Canvas.Left="9.7" Canvas.Top="14" Width="50.2" Height="50.2">

                                <Ellipse.Fill>

                                    <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">

                                        <GradientStop Offset="0" Color="#BEBFC1"></GradientStop>

                                        <GradientStop Offset="0.52" Color="#BEBFC1"></GradientStop>

                                        <GradientStop Offset="0.54" Color="#7A7D7C"></GradientStop>

                                        <GradientStop Offset="1" Color="#7A7D7C"></GradientStop>

                                </Ellipse.Fill>

                            </Ellipse>

                            <!--給水泵 風扇 背景-->

                            <Ellipse Fill="#626968" Canvas.Left="15.1" Canvas.Top="19.4" Width="39.4" Height="39.4"/>

                            <!--給水泵 三個 扇頁-->

                            <Grid x:Name="GridFanJiShuiBeng" Canvas.Left="15.3" Canvas.Top="18.6" Width="39.4" Height="39.4" RenderTransformOrigin="0.5,0.5">

                                <Canvas Width="39.4" Height="39.4">

                                    <Path Fill="#010C00" Data="M21.5,35.4c-0.3-0.1-0.6-0.3-0.9-0.5c-4.3-2.6-5.7-8.2-3.1-12.5c0.6-1,1.4-1.9,2.3-2.6

 c-4.1-1.9-9.1-0.4-11.5,3.5c-2.6,4.3-1.2,9.9,3.1,12.5C14.7,37.9,18.7,37.6,21.5,35.4z"/>

                                    <Path Fill="#010C00" Data="M5.3,13.5c0.3-0.2,0.5-0.4,0.8-0.6c4.3-2.6,9.9-1.3,12.5,3c0.6,1,1,2.1,1.2,3.3c3.6-2.8,4.6-7.9,2.1-11.9

 c-2.6-4.3-8.2-5.6-12.5-3C6.5,7.1,4.7,10,5.3,13.5z"/>

                                    <Path Fill="#010C00" Data="M32.4,10.4c0,0.3,0.1,0.7,0.1,1c0,5-4.1,9.1-9.1,9.1c-1.2,0-2.4-0.2-3.4-0.7c0.5,4.5,4.3,8,9,8

 c5,0,9.1-4.1,9.1-9.1C38,15,35.7,11.7,32.4,10.4z"/>

                                </Canvas>

                            </Grid>

                            <!-- 風扇 圓心-->

                            <Ellipse Fill="#FCFCFC" Canvas.Left="32" Canvas.Top="35.3" Height="6" Width="6"/>

                            <Path Fill="#5C6162" Data="M27.2,69.3c0,1-0.6,1.8-1.2,1.8h-7.5c-0.7,0-1.2-0.8-1.2-1.8v-1.8c0-1,0.6-1.8,1.2-1.8h7.5

 c0.7,0,1.2,0.8,1.2,1.8V69.3z"/>

                            <Path Fill="#5C6162" Data="M27.6,60.5c1.3,0.3,2.2,1.1,2,1.8l-2.1,7.8c-0.2,0.7-1.4,1.1-2.7,0.8l-2.4-0.5c-1.3-0.3-2.2-1.1-2-1.8

 l2.1-7.8c0.2-0.7,1.4-1.1,2.7-0.8L27.6,60.5z"/>

                            <Path Fill="#5C6162" Data="M43.7,59.9c1.3-0.3,2.5,0.1,2.7,0.8l2,7.9c0.2,0.7-0.7,1.5-2,1.8l-2.4,0.5c-1.3,0.3-2.5-0.1-2.7-0.8l-2-7.9

 c-0.2-0.7,0.7-1.5,2-1.8L43.7,59.9z"/>

                            <Path Fill="#5C6162" Data="M51.2,69.3c0,1-0.6,1.8-1.2,1.8h-7.5c-0.7,0-1.2-0.8-1.2-1.8v-1.8c0-1,0.6-1.8,1.2-1.8h7.5

                            <!--标題-->

                            <TextBlock x:Name="JiShuiBengTitle" Text="給水泵" FontSize="20" FontWeight="Bold" Canvas.Left="7.1" Canvas.Top="74.4"></TextBlock>

                            <!--風力值-->

                            <TextBlock x:Name="FanValue" Visibility="Hidden"></TextBlock>

                        </Canvas>

                    </Viewbox>

                </ControlTemplate>

            </Setter.Value>

        </Setter>

    </Style>

</ResourceDictionary>

三:在項目下添加實作類

代碼如下:

using System;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Media;

using System.Windows.Media.Animation;

namespace YinFengJi

{

    /// <summary>

    ///

    /// </summary>

    public class YinFengJ : Button

    {

        /// <summary>

        /// 轉速設定

        /// </summary>

        private int RotationRate = 0;

        /// 動畫 重複 次數

        private int RotationRepeatTime = 100000;

        static YinFengJ()

        {

            DefaultStyleKeyProperty.OverrideMetadata(typeof(YinFengJ), new FrameworkPropertyMetadata(typeof(YinFengJ)));

        }

        #region 控件清單

        /// 風扇 扇頁元素

        Grid _GridFanJiShuiBeng = null;

        /// 風力大小

        TextBlock _FanValue = null;

        /// 動畫标題

        TextBlock _JiShuiBengTitle = null;

        #endregion

        #region 控件綁定

        public override void OnApplyTemplate()

            base.OnApplyTemplate();

            _GridFanJiShuiBeng = GetTemplateChild("GridFanJiShuiBeng") as Grid;

            _FanValue = GetTemplateChild("FanValue") as TextBlock;

            _JiShuiBengTitle = GetTemplateChild("JiShuiBengTitle") as TextBlock;

            _FanValue.SetBinding(TextBlock.TextProperty, new Binding("FanValue") { Source = this });

            _JiShuiBengTitle.SetBinding(TextBlock.TextProperty, new Binding("JiShuiBengTitle") { Source = this });

            _GridFanJiShuiBeng.SetBinding(Grid.StyleProperty, new Binding("FanValue") { Source = this });

        #region 依賴及屬性

        /// 标題

        public string JiShuiBengTitle

            get { return (string)GetValue(_JiShuiBengTitleProperty); }

            set { SetValue(_JiShuiBengTitleProperty, value); }

        public static readonly DependencyProperty _JiShuiBengTitleProperty =

            DependencyProperty.Register("JiShuiBengTitle", typeof(string), typeof(YinFengJ), new PropertyMetadata("引風機"));

        /// 風力值

        public float FanValue

            get { return (float)GetValue(_FanValueProperty); }

            set { SetValue(_FanValueProperty, value); }

        public static readonly DependencyProperty _FanValueProperty =

            DependencyProperty.Register("FireValue", typeof(float), typeof(YinFengJ), new PropertyMetadata(0f, FanValueCallBack));

        private static void FanValueCallBack(DependencyObject d, DependencyPropertyChangedEventArgs e)

            (d as YinFengJ).FanValueStoryboardPlay(e);

        private void FanValueStoryboardPlay(DependencyPropertyChangedEventArgs e)

            float newV = float.Parse(e.NewValue.ToString());

            float oldV = float.Parse(e.OldValue.ToString());

            ////轉速設定

            RotationRepeatTime = 100000;

            if (newV == 1)

            {

                RotationRate = 35000;

            }

            else if (newV == 2)

                RotationRate = 16000;

            else if (newV == 3)

                RotationRate = 6000;

            else if (newV == 0)

                RotationRepeatTime = 0;

            //元素 轉動動畫

            RotateTransform rtGSB = new RotateTransform();

            rtGSB.CenterX = 0;

            rtGSB.CenterY = 0;

            _GridFanJiShuiBeng.RenderTransform = rtGSB;

            //rotationRate = 9000;

            DoubleAnimation GSB_DA = new DoubleAnimation(0, 3000, new Duration(TimeSpan.FromMilliseconds(RotationRate)));

            GSB_DA.RepeatBehavior = new RepeatBehavior(RotationRepeatTime);

            rtGSB.BeginAnimation(RotateTransform.AngleProperty, GSB_DA);

    }

}

四:以類庫項目輸出,就可以作為控件用于其它WPF項目了

此程式中的Generic.xaml 是由,svg格式圖中的元素修改來的,注意svg圖檔元素跟xaml格式的差別