WPF实现一个带旋转动画的菜单栏
创始人
2024-12-29 06:41:42
0

WPF实现一个带旋转动画的菜单栏

  • 一、创建WPF项目及文件
    • 1、创建项目
    • 2、创建文件夹及文件
    • 3、添加引用
  • 二、代码实现
    • 2.ControlAttachProperty类

一、创建WPF项目及文件

1、创建项目

打开VS2022,创建一个WPF项目,如下所示
在这里插入图片描述在这里插入图片描述

2、创建文件夹及文件

创建资源文件夹,添加字体图标文件,添加 Menu样式文件,如下所示
在这里插入图片描述
创建公共附加属性用控件类库,并创建对应的 ControlAttachProperty 类文件如下:
在这里插入图片描述
在这里插入图片描述

3、添加引用

右键 Menu_WPF 添加引用,将类库引用到当前项目
在这里插入图片描述

二、代码实现

2.ControlAttachProperty类

代码如下(示例):

using Microsoft.Win32; using System; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using CheckBox = System.Windows.Controls.CheckBox; using ComboBox = System.Windows.Controls.ComboBox; using OpenFileDialog = Microsoft.Win32.OpenFileDialog; using RadioButton = System.Windows.Controls.RadioButton; using RichTextBox = System.Windows.Controls.RichTextBox; using TextBox = System.Windows.Controls.TextBox;  namespace Common.UserControl.Extession {     ///      /// 公共附加属性     ///      public static class ControlAttachProperty     {          #region FocusBorderBrush 焦点边框色,输入控件          public static readonly DependencyProperty FocusBorderBrushProperty = DependencyProperty.RegisterAttached(             "FocusBorderBrush", typeof(Brush), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(null));         public static void SetFocusBorderBrush(DependencyObject element, Brush value)         {             element.SetValue(FocusBorderBrushProperty, value);         }         public static Brush GetFocusBorderBrush(DependencyObject element)         {             return (Brush)element.GetValue(FocusBorderBrushProperty);         }          #endregion          #region MouseOverBorderBrush 鼠标进入边框色,输入控件          public static readonly DependencyProperty MouseOverBorderBrushProperty =             DependencyProperty.RegisterAttached("MouseOverBorderBrush", typeof(Brush), typeof(ControlAttachProperty),                 new FrameworkPropertyMetadata(Brushes.Transparent,                     FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Inherits));          ///          /// Sets the brush used to draw the mouse over brush.         ///          public static void SetMouseOverBorderBrush(DependencyObject obj, Brush value)         {             obj.SetValue(MouseOverBorderBrushProperty, value);         }          ///          /// Gets the brush used to draw the mouse over brush.         ///          [AttachedPropertyBrowsableForType(typeof(TextBox))]         [AttachedPropertyBrowsableForType(typeof(CheckBox))]         [AttachedPropertyBrowsableForType(typeof(RadioButton))]         [AttachedPropertyBrowsableForType(typeof(DatePicker))]         [AttachedPropertyBrowsableForType(typeof(ComboBox))]         [AttachedPropertyBrowsableForType(typeof(RichTextBox))]         public static Brush GetMouseOverBorderBrush(DependencyObject obj)         {             return (Brush)obj.GetValue(MouseOverBorderBrushProperty);         }          #endregion          #region AttachContentProperty 附加组件模板         ///          /// 附加组件模板         ///          public static readonly DependencyProperty AttachContentProperty = DependencyProperty.RegisterAttached(             "AttachContent", typeof(ControlTemplate), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(null));          public static ControlTemplate GetAttachContent(DependencyObject d)         {             return (ControlTemplate)d.GetValue(AttachContentProperty);         }          public static void SetAttachContent(DependencyObject obj, ControlTemplate value)         {             obj.SetValue(AttachContentProperty, value);         }         #endregion          #region WatermarkProperty 水印         ///          /// 水印         ///          public static readonly DependencyProperty WatermarkProperty = DependencyProperty.RegisterAttached(             "Watermark", typeof(string), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(""));          public static string GetWatermark(DependencyObject d)         {             return (string)d.GetValue(WatermarkProperty);         }          public static void SetWatermark(DependencyObject obj, string value)         {             obj.SetValue(WatermarkProperty, value);         }         #endregion          #region FIconProperty 字体图标         ///          /// 字体图标         ///          public static readonly DependencyProperty FIconProperty = DependencyProperty.RegisterAttached(             "FIcon", typeof(string), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(""));          public static string GetFIcon(DependencyObject d)         {             return (string)d.GetValue(FIconProperty);         }          public static void SetFIcon(DependencyObject obj, string value)         {             obj.SetValue(FIconProperty, value);         }         #endregion          #region FIconSizeProperty 字体图标大小         ///          /// 字体图标         ///           public static readonly DependencyProperty FIconSizeProperty = DependencyProperty.RegisterAttached(             "FIconSize", typeof(double), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(12D));          public static double GetFIconSize(DependencyObject d)         {             return (double)d.GetValue(FIconSizeProperty);         }          public static void SetFIconSize(DependencyObject obj, double value)         {             obj.SetValue(FIconSizeProperty, value);         }         #endregion          #region FIconMarginProperty 字体图标边距         ///          /// 字体图标         ///          public static readonly DependencyProperty FIconMarginProperty = DependencyProperty.RegisterAttached(             "FIconMargin", typeof(Thickness), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(null));          public static Thickness GetFIconMargin(DependencyObject d)         {             return (Thickness)d.GetValue(FIconMarginProperty);         }          public static void SetFIconMargin(DependencyObject obj, Thickness value)         {             obj.SetValue(FIconMarginProperty, value);         }         #endregion          #region AllowsAnimationProperty 启用旋转动画         ///          /// 启用旋转动画         ///          public static readonly DependencyProperty AllowsAnimationProperty = DependencyProperty.RegisterAttached("AllowsAnimation"             , typeof(bool), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(false, AllowsAnimationChanged));          public static bool GetAllowsAnimation(DependencyObject d)         {             return (bool)d.GetValue(AllowsAnimationProperty);         }          public static void SetAllowsAnimation(DependencyObject obj, bool value)         {             obj.SetValue(AllowsAnimationProperty, value);         }          ///          /// 旋转动画刻度         ///          private static DoubleAnimation RotateAnimation = new DoubleAnimation(0, new Duration(TimeSpan.FromMilliseconds(200)));          ///          /// 绑定动画事件         ///          private static void AllowsAnimationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)         {             var uc = d as FrameworkElement;             if (uc == null) return;             if (uc.RenderTransformOrigin == new Point(0, 0))             {                 uc.RenderTransformOrigin = new Point(0.5, 0.5);                 RotateTransform trans = new RotateTransform(0);                 uc.RenderTransform = trans;             }             var value = (bool)e.NewValue;             if (value)             {                 RotateAnimation.To = 180;                 uc.RenderTransform.BeginAnimation(RotateTransform.AngleProperty, RotateAnimation);             }             else             {                 RotateAnimation.To = 0;                 uc.RenderTransform.BeginAnimation(RotateTransform.AngleProperty, RotateAnimation);             }         }         #endregion          #region CornerRadiusProperty Border圆角         ///          /// Border圆角         ///          public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.RegisterAttached(             "CornerRadius", typeof(CornerRadius), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(null));          public static CornerRadius GetCornerRadius(DependencyObject d)         {             return (CornerRadius)d.GetValue(CornerRadiusProperty);         }          public static void SetCornerRadius(DependencyObject obj, CornerRadius value)         {             obj.SetValue(CornerRadiusProperty, value);         }         #endregion          #region LabelProperty TextBox的头部Label         ///          /// TextBox的头部Label         ///          public static readonly DependencyProperty LabelProperty = DependencyProperty.RegisterAttached(             "Label", typeof(string), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(null));          [AttachedPropertyBrowsableForType(typeof(TextBox))]         public static string GetLabel(DependencyObject d)         {             return (string)d.GetValue(LabelProperty);         }          public static void SetLabel(DependencyObject obj, string value)         {             obj.SetValue(LabelProperty, value);         }         #endregion          #region LabelTemplateProperty TextBox的头部Label模板         ///          /// TextBox的头部Label模板         ///          public static readonly DependencyProperty LabelTemplateProperty = DependencyProperty.RegisterAttached(             "LabelTemplate", typeof(ControlTemplate), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(null));          [AttachedPropertyBrowsableForType(typeof(TextBox))]         public static ControlTemplate GetLabelTemplate(DependencyObject d)         {             return (ControlTemplate)d.GetValue(LabelTemplateProperty);         }          public static void SetLabelTemplate(DependencyObject obj, ControlTemplate value)         {             obj.SetValue(LabelTemplateProperty, value);         }         #endregion               } }   

Menu样式文件实现

           

MainWindow样式实现

                                                                                                                                  

相关内容

热门资讯

1分钟机器人!(昆明文山麻将)... 1分钟机器人!(昆明文山麻将)外挂透视辅助挂,微扑克专用其实有挂猫腻详细教程(有挂解惑)-哔哩哔哩;...
四分钟输赢!(WEPOKE)其... 四分钟输赢!(WEPOKE)其实是有挂的,WEPOKE2020已更新,详细教程(有挂方针)-哔哩哔哩...
1分钟识别!(大鱼扑克)原来是... 您好,大鱼扑克这款游戏可以开挂的,确实是有挂的,需要了解加微【841106723】很多玩家在这款游戏...
5分钟下载!(欢乐风暴)其实真... 5分钟下载!(欢乐风暴)其实真实是有挂,欢乐风暴2023已更新,详细教程(有挂方针)-哔哩哔哩;致您...
二分钟科技!微扑克辅助器ios... 二分钟科技!微扑克辅助器ios,WPK代码其实真实是有挂的,详细教程(2021已更新)(哔哩哔哩)是...
7分钟ai辅助!(Wepoke... 您好,Wepoke工具这款游戏可以开挂的,确实是有挂的,需要了解加微【841106723】很多玩家在...
四分钟模拟器!(德扑之星)确实... 四分钟模拟器!(德扑之星)确实有挂是有猫腻,德扑之星2021已更新,详细教程(有挂技巧)-哔哩哔哩;...
两分钟针对!(一起跑得快)外挂... 两分钟针对!(一起跑得快)外挂透视辅助挂,Wepoke安卓版本原来是有挂详细教程(有挂教学)-哔哩哔...
十分钟实锤!(天天畅玩德州)原... 十分钟实锤!(天天畅玩德州)原来确实是有挂,Wepoke中牌率确实是有挂猫腻,详细教程(真的有挂)-...
两分钟分钟!Wepoke最新款... 两分钟分钟!Wepoke最新款是有挂猫腻吗,Wepoke透视其实一直都是有挂,详细教程(2025已更...