wpf 重绘一个浮动的圆形按钮

鼠扑  论坛元老 | 2024-12-30 03:50:43 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 1068|帖子 1068|积分 3204


  1. <Window x:Class="DataParser.FlowView"
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6.         xmlns:local="clr-namespace:DataParser"
  7.         xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
  8.         xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
  9.         xmlns:bl="clr-namespace:BehaviorsModule;assembly=BehaviorsModule"
  10.         mc:Ignorable="d"
  11.         Title="FlowView"
  12.         Background="Transparent"
  13.         WindowStyle="None"
  14.         AllowsTransparency="True"
  15.         BorderThickness="0" ShowInTaskbar="False">
  16.     <i:Interaction.Behaviors>
  17.         <bl:MoveViewBehavior/>
  18.     </i:Interaction.Behaviors>
  19.     <Grid  Background="Transparent">
  20.         <Grid.ColumnDefinitions>
  21.             <ColumnDefinition  Width="auto"></ColumnDefinition>
  22.             <ColumnDefinition  Width="auto"></ColumnDefinition>
  23.         </Grid.ColumnDefinitions>
  24.         <ToggleButton x:Name="toggleBtn"
  25.                       VerticalAlignment="Top" Width="34" Height="34" Padding="0"
  26.                       Checked="toggleBtn_Checked">
  27.             <ToggleButton.ContextMenu>
  28.                 <ContextMenu>
  29.                     <MenuItem Header="Clear" Command="{Binding ClearMessageCommand}" CommandParameter="ReceivedData" FontSize="12" Height="30" Width="80" VerticalAlignment="Top"  HorizontalAlignment="Left" >
  30.                         <MenuItem.Template>
  31.                             <ControlTemplate>
  32.                                 <Button Width="50" Height="30"/>
  33.                             </ControlTemplate>
  34.                         </MenuItem.Template>
  35.                         
  36.                     </MenuItem>
  37.                 </ContextMenu>
  38.             </ToggleButton.ContextMenu>
  39.             <ToggleButton.Template>
  40.                 <ControlTemplate>
  41.                     <Border Background="AliceBlue" BorderThickness="1" CornerRadius="15">
  42.                         <iconPacks:PackIconVaadinIcons Kind="DotCircle" x:Name="PackIconFileIcon" Foreground="#FF6AFCFF"
  43.                                                        HorizontalAlignment="Center" VerticalAlignment="Center"
  44.                                                        Height="20" Width="20">
  45.                             <iconPacks:PackIconVaadinIcons.RenderTransformOrigin>
  46.                                 <Point X="0.5" Y="0.5"/>
  47.                             </iconPacks:PackIconVaadinIcons.RenderTransformOrigin>
  48.                             <iconPacks:PackIconVaadinIcons.RenderTransform>
  49.                                 <ScaleTransform ScaleX="1" ScaleY="1" x:Name="iconScaleTransform" />
  50.                             </iconPacks:PackIconVaadinIcons.RenderTransform>
  51.                         </iconPacks:PackIconVaadinIcons>
  52.                     </Border>
  53.                     <ControlTemplate.Triggers>
  54.                         <Trigger Property="IsMouseOver" Value="True">
  55.                             <Trigger.EnterActions>
  56.                                 <BeginStoryboard>
  57.                                     <Storyboard>
  58.                                         <DoubleAnimation
  59.                                     Storyboard.TargetName="iconScaleTransform"
  60.                                     Storyboard.TargetProperty="ScaleX"
  61.                                     To="1.5" Duration="0:0:0.25"/>
  62.                                         <DoubleAnimation
  63.                                     Storyboard.TargetName="iconScaleTransform"
  64.                                     Storyboard.TargetProperty="ScaleY"
  65.                                     To="1.5" Duration="0:0:0.25"/>
  66.                                     </Storyboard>
  67.                                 </BeginStoryboard>
  68.                             </Trigger.EnterActions>
  69.                             <Trigger.ExitActions>
  70.                                 <BeginStoryboard>
  71.                                     <Storyboard>
  72.                                         <DoubleAnimation
  73.                                     Storyboard.TargetName="iconScaleTransform"
  74.                                     Storyboard.TargetProperty="ScaleX"
  75.                                     To="1" Duration="0:0:0.25"/>
  76.                                         <DoubleAnimation
  77.                                     Storyboard.TargetName="iconScaleTransform"
  78.                                     Storyboard.TargetProperty="ScaleY"
  79.                                     To="1" Duration="0:0:0.25"/>
  80.                                     </Storyboard>
  81.                                 </BeginStoryboard>
  82.                             </Trigger.ExitActions>
  83.                         </Trigger>
  84.                     </ControlTemplate.Triggers>
  85.                 </ControlTemplate>
  86.             </ToggleButton.Template>
  87.         </ToggleButton>
  88.     </Grid>
  89. </Window>
复制代码
1. Window 界说

  1. <Window x:Class="DataParser.FlowView"
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6.         xmlns:local="clr-namespace:DataParser"
  7.         xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
  8.         xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
  9.         xmlns:bl="clr-namespace:BehaviorsModule;assembly=BehaviorsModule"
  10.         mc:Ignorable="d"
  11.         Title="FlowView"
  12.         Background="Transparent"
  13.         WindowStyle="None"
  14.         AllowsTransparency="True"
  15.         BorderThickness="0" ShowInTaskbar="False">
复制代码


  • x:Class=“DataParser.FlowView”: 指定窗口的类名为 FlowView,位于 DataParser 命名空间。
  • xmlns: 界说了多个 XML 命名空间,包括 WPF 核心命名空间、XAML 命名空间、Blend 筹划器命名空间、以及一些自界说命名空间(如 iconPacks、i、bl)。
  • Title=“FlowView”: 设置窗口的标题为 FlowView。
  • Background=“Transparent”: 设置窗口配景为透明。
  • WindowStyle=“None”: 设置窗口样式为无边框。
  • AllowsTransparency=“True”: 允许窗口透明。
  • BorderThickness=“0”: 设置窗口边框厚度为0。
  • ShowInTaskbar=“False”: 窗口不显示在使命栏上。
2. Interaction.Behaviors

  1. <i:Interaction.Behaviors>
  2.     <bl:MoveViewBehavior/>
  3. </i:Interaction.Behaviors>
复制代码


  • Interaction.Behaviors: 使用 System.Windows.Interactivity 库来为窗口添加行为。
  • bl:MoveViewBehavior: 自界说行为,允许用户通过拖动窗口移动它。
3. Grid 布局

  1. <Grid Background="Transparent">
  2.     <Grid.ColumnDefinitions>
  3.         <ColumnDefinition Width="auto"></ColumnDefinition>
  4.         <ColumnDefinition Width="auto"></ColumnDefinition>
  5.     </Grid.ColumnDefinitions>
复制代码


  • Grid: 界说了一个透明的网格布局。
  • Grid.ColumnDefinitions: 界说了两列,宽度均为主动调整(auto)。
4. ToggleButton

  1. <ToggleButton x:Name="toggleBtn"
  2.               VerticalAlignment="Top" Width="34" Height="34" Padding="0"
  3.               Checked="toggleBtn_Checked">
复制代码


  • ToggleButton: 界说了一个切换按钮,命名为 toggleBtn。
  • VerticalAlignment=“Top”: 按钮垂直对齐到顶部。
  • Width=“34” Height=“34” Padding=“0”: 设置按钮的宽度和高度为34,内边距为0。
  • Checked=“toggleBtn_Checked”: 当按钮被选中时,触发 toggleBtn_Checked 事件处置惩罚步伐。
5. ContextMenu

  1. <ToggleButton.ContextMenu>
  2.     <ContextMenu>
  3.         <MenuItem Header="Clear" Command="{Binding ClearMessageCommand}" CommandParameter="ReceivedData" FontSize="12" Height="30" Width="80" VerticalAlignment="Top"  HorizontalAlignment="Left">
  4.             <MenuItem.Template>
  5.                 <ControlTemplate>
  6.                     <Button Width="50" Height="30"/>
  7.                 </ControlTemplate>
  8.             </MenuItem.Template>
  9.         </MenuItem>
  10.     </ContextMenu>
  11. </ToggleButton.ContextMenu>
复制代码


  • ContextMenu: 为 ToggleButton 界说了一个上下文菜单。
  • MenuItem Header=“Clear”: 菜单项的标题为 “Clear”,绑定了一个命令 ClearMessageCommand,参数为 ReceivedData。
  • ControlTemplate: 界说了一个模板,使得菜单项看起来像一个按钮。
6. ToggleButton 的模板

  1. <ToggleButton.Template>
  2.     <ControlTemplate>
  3.         <Border Background="AliceBlue" BorderThickness="1" CornerRadius="15">
  4.             <iconPacks:PackIconVaadinIcons Kind="DotCircle" x:Name="PackIconFileIcon" Foreground="#FF6AFCFF"
  5.                                            HorizontalAlignment="Center" VerticalAlignment="Center"
  6.                                            Height="20" Width="20">
  7.                 <iconPacks:PackIconVaadinIcons.RenderTransformOrigin>
  8.                     <Point X="0.5" Y="0.5"/>
  9.                 </iconPacks:PackIconVaadinIcons.RenderTransformOrigin>
  10.                 <iconPacks:PackIconVaadinIcons.RenderTransform>
  11.                     <ScaleTransform ScaleX="1" ScaleY="1" x:Name="iconScaleTransform" />
  12.                 </iconPacks:PackIconVaadinIcons.RenderTransform>
  13.             </iconPacks:PackIconVaadinIcons>
  14.         </Border>
  15.         <ControlTemplate.Triggers>
  16.             <Trigger Property="IsMouseOver" Value="True">
  17.                 <Trigger.EnterActions>
  18.                     <BeginStoryboard>
  19.                         <Storyboard>
  20.                             <DoubleAnimation
  21.                         Storyboard.TargetName="iconScaleTransform"
  22.                         Storyboard.TargetProperty="ScaleX"
  23.                         To="1.5" Duration="0:0:0.25"/>
  24.                             <DoubleAnimation
  25.                         Storyboard.TargetName="iconScaleTransform"
  26.                         Storyboard.TargetProperty="ScaleY"
  27.                         To="1.5" Duration="0:0:0.25"/>
  28.                         </Storyboard>
  29.                     </BeginStoryboard>
  30.                 </Trigger.EnterActions>
  31.                 <Trigger.ExitActions>
  32.                     <BeginStoryboard>
  33.                         <Storyboard>
  34.                             <DoubleAnimation
  35.                         Storyboard.TargetName="iconScaleTransform"
  36.                         Storyboard.TargetProperty="ScaleX"
  37.                         To="1" Duration="0:0:0.25"/>
  38.                             <DoubleAnimation
  39.                         Storyboard.TargetName="iconScaleTransform"
  40.                         Storyboard.TargetProperty="ScaleY"
  41.                         To="1" Duration="0:0:0.25"/>
  42.                         </Storyboard>
  43.                     </BeginStoryboard>
  44.                 </Trigger.ExitActions>
  45.             </Trigger>
  46.         </ControlTemplate.Triggers>
  47.     </ControlTemplate>
  48. </ToggleButton.Template>
复制代码


  • ControlTemplate: 界说了 ToggleButton 的外观。
  • Border: 设置了一个带圆角的边框,配景为 AliceBlue。
  • PackIconVaadinIcons: 使用了 MahApps.Metro.IconPacks 库中的图标,类型为 DotCircle。
  • ScaleTransform: 界说了一个缩放变换,初始缩放比例为1。
  • ControlTemplate.Triggers: 界说了一个触发器,当鼠标悬停在按钮上时,图标的缩放比例变为1.5,鼠标移开后恢复为1。
  1. using System;
  2. namespace DataParser
  3. {
  4.     /// <summary>
  5.     /// FlowView.xaml 的交互逻辑
  6.     /// </summary>
  7.     public partial class FlowView : Window
  8.     {
  9.         public FlowView()
  10.         {
  11.             InitializeComponent();
  12.             InitializeFormPosition();
  13.         }
  14.         private void InitializeFormPosition()
  15.         {
  16.             var workingArea = SystemParameters.WorkArea;
  17.             double screenWidth = workingArea.Width;
  18.             double screenHeight = workingArea.Height;
  19.             double x = screenWidth / 1.2;
  20.             double y = screenHeight  /9;
  21.             this.Left = x;
  22.             this.Top = y;
  23.         }
  24.         private void toggleBtn_Checked(object sender, RoutedEventArgs e)
  25.         {
  26.         }
  27.     }
  28. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

鼠扑

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表