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>
                                    <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>
                                </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>
                                    </LinearGradientBrush>
                                </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
 c0.7,0,1.2,0.8,1.2,1.8V69.3z"/>

                            <!--标题-->
                            <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;
        /// <summary>
        /// 动画 重复 次数
        /// </summary>
        private int RotationRepeatTime = 100000;
        static YinFengJ()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(YinFengJ), new FrameworkPropertyMetadata(typeof(YinFengJ)));

        }
        #region 控件列表
        /// <summary>
        /// 风扇 扇页元素
        /// </summary>
        Grid _GridFanJiShuiBeng = null;
        /// <summary>
        /// 风力大小
        /// </summary>
        TextBlock _FanValue = null;
        /// <summary>
        /// 动画标题
        /// </summary>
        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 });
        }
        #endregion

        #region 依赖及属性

        /// <summary>
        /// 标题
        /// </summary>
        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("引风机"));
        /// <summary>
        /// 风力值
        /// </summary>
        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);
        }
        #endregion
    }
}

四:以类库项目输出,就可以作为控件用于其它WPF项目了

此程序中的Generic.xaml 是由,svg格式图中的元素修改来的,注意svg图片元素跟xaml格式的区别


 

猜你喜欢

转载自www.cnblogs.com/bsyblog/p/9199517.html