一、前言 上回說到需要做放視頻的使用嚮導,這兩天公司里的老司機一直幫我答疑解惑,讓這個任務變得挺順的,真心感謝他們! 這次與【1】中的不同之處在於: (1)播放和暫停按鈕集成在<MediaElement>的點擊事件之中,點一下是播放,再點一下是暫停 (2)加入了微軟官方改寫的粒子特效 (3)加上了自 ...
一、前言
上回說到需要做放視頻的使用嚮導,這兩天公司里的老司機一直幫我答疑解惑,讓這個任務變得挺順的,真心感謝他們!
這次與【1】中的不同之處在於:
(1)播放和暫停按鈕集成在<MediaElement>的點擊事件之中,點一下是播放,再點一下是暫停
(2)加入了微軟官方改寫的粒子特效
(3)加上了自己琢磨的按鈕旋轉效果,以及按鈕淡出popup效果
(4)進度條改善美觀
二、代碼
前臺:
1 <Window 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:WPF_Nav" 7 xmlns:dxlc="http://schemas.devexpress.com/winfx/2008/xaml/layoutcontrol" xmlns:dxwui="http://schemas.devexpress.com/winfx/2008/xaml/windowsui" x:Class="WPF_Nav.MainWindow" 8 mc:Ignorable="d" 9 Title="MainWindow" Height="600" Width="800" WindowStyle="None" ResizeMode="NoResize" WindowStartupLocation ="CenterScreen" Loaded="Window_Loaded"> 10 11 <Window.Resources> 12 <LinearGradientBrush x:Key="SliderBackground" StartPoint="0,0" EndPoint="0,1"> 13 <GradientStop Offset="0.5" Color="#00b3fe"/> 14 </LinearGradientBrush> 15 <LinearGradientBrush x:Key="SliderThumb" StartPoint="0,0" EndPoint="0,1"> 16 <GradientStop Offset="0" Color="#FFD9D3E8"/> 17 </LinearGradientBrush> 18 19 <Style x:Key="Slider_RepeatButton" TargetType="RepeatButton"> 20 <Setter Property="Focusable" Value="false" /> 21 <Setter Property="Height" Value="5"/> 22 <Setter Property="BorderBrush" Value="Transparent"/> 23 <Setter Property="Template"> 24 <Setter.Value> 25 <ControlTemplate TargetType="RepeatButton"> 26 <Border Background="{StaticResource SliderBackground}" /> 27 </ControlTemplate> 28 </Setter.Value> 29 </Setter> 30 </Style> 31 32 <Style x:Key="Slider_RepeatButton1" TargetType="RepeatButton"> 33 <Setter Property="Focusable" Value="false" /> 34 <Setter Property="Height" Value="5"/> 35 <Setter Property="BorderBrush" Value="Transparent"/> 36 <Setter Property="Template"> 37 <Setter.Value> 38 <ControlTemplate TargetType="RepeatButton"> 39 <Border Background="Silver" /> 40 </ControlTemplate> 41 </Setter.Value> 42 </Setter> 43 </Style> 44 45 <Style x:Key="Slider_Thumb" TargetType="Thumb"> 46 <Setter Property="Focusable" Value="false" /> 47 <Setter Property="Template"> 48 <Setter.Value> 49 <ControlTemplate TargetType="Thumb"> 50 <Ellipse Name="e" Width="15" Height="15" Fill="White" Stroke="Gray"/> 51 </ControlTemplate> 52 </Setter.Value> 53 </Setter> 54 </Style> 55 56 <Style TargetType="Slider"> 57 <Setter Property="Focusable" Value="false" /> 58 <Setter Property="Template"> 59 <Setter.Value> 60 <ControlTemplate TargetType="Slider"> 61 <Grid> 62 <Border BorderBrush="Red" BorderThickness="0" CornerRadius="0,0,0,0"> 63 <Track Name="PART_Track"> 64 <Track.DecreaseRepeatButton> 65 <RepeatButton Style="{StaticResource Slider_RepeatButton}" Command="Slider.DecreaseLarge"/> 66 </Track.DecreaseRepeatButton> 67 <Track.IncreaseRepeatButton> 68 <RepeatButton Style="{StaticResource Slider_RepeatButton1}" Command="Slider.IncreaseLarge"/> 69 </Track.IncreaseRepeatButton> 70 <Track.Thumb> 71 <Thumb Style="{StaticResource Slider_Thumb}"/> 72 </Track.Thumb> 73 </Track> 74 </Border> 75 </Grid> 76 </ControlTemplate> 77 </Setter.Value> 78 </Setter> 79 </Style> 80 81 82 <Style x:Key="Button_Close" TargetType="Button"> 83 <Setter Property="Template"> 84 <Setter.Value> 85 <ControlTemplate> 86 <Image x:Name="IMG" Source="E:\Test\WPFTest\Sources\Close.jpg"></Image> 87 </ControlTemplate> 88 </Setter.Value> 89 </Setter> 90 </Style> 91 92 <Style x:Key="Button_Forbidden" TargetType="Button"> 93 <Setter Property="Template"> 94 <Setter.Value> 95 <ControlTemplate> 96 <Image x:Name="IMG" Source="E:\Test\WPFTest\Sources\Forbidden.jpg"></Image> 97 </ControlTemplate> 98 </Setter.Value> 99 </Setter> 100 </Style> 101 102 103 <Style x:Key="Button_Left" TargetType="Button"> 104 <Setter Property="Template"> 105 <Setter.Value> 106 <ControlTemplate> 107 <Image x:Name="IMG" Source="E:\Test\WPFTest\Sources\Left.png" Stretch="Fill"></Image> 108 </ControlTemplate> 109 </Setter.Value> 110 </Setter> 111 </Style> 112 113 114 <Style x:Key="Button_Right" TargetType="Button"> 115 <Setter Property="Template"> 116 <Setter.Value> 117 <ControlTemplate> 118 <Image x:Name="IMG" Source="E:\Test\WPFTest\Sources\Right.png" Stretch="Fill"></Image> 119 </ControlTemplate> 120 </Setter.Value> 121 </Setter> 122 </Style> 123 124 </Window.Resources> 125 126 <Grid Name="Main_Grid"> 127 <Grid.RowDefinitions> 128 <RowDefinition Height="50"></RowDefinition> 129 <RowDefinition Height="500"></RowDefinition> 130 <RowDefinition Height="50"></RowDefinition> 131 </Grid.RowDefinitions> 132 133 <Border Name="title_Border" BorderBrush="#FBD3D0CD" BorderThickness="3" Grid.Row="0"> 134 <Grid Name="Title"> 135 <Grid.ColumnDefinitions> 136 <ColumnDefinition Width="200"></ColumnDefinition> 137 <ColumnDefinition Width="400"></ColumnDefinition> 138 <ColumnDefinition Width="120"></ColumnDefinition> 139 <ColumnDefinition Width="80"></ColumnDefinition> 140 </Grid.ColumnDefinitions> 141 142 <Grid Grid.Column="1"> 143 <Canvas x:Name="ParticleHost" Width="400" > 144 <TextBlock Name="txtB_Step" Grid.Column="1" Width="200" Height="30" TextAlignment="Center" FontSize="20" FontFamily="Microsoft YaHei" Text="asss" Margin="100,7,0,0"/> 145 </Canvas> 146 </Grid> 147 148 <Grid Name="grid_Cofig" Grid.Column="3"> 149 <Button Name="btn_Forbidden" Width="30" Click="Config_Click" Margin="2,10,48,12" HorizontalAlignment="Center" Focusable="False" Style="{StaticResource Button_Forbidden}" RenderTransformOrigin="0.5,0.5" ToolTipService.ToolTip="播放設置" ToolTipService.InitialShowDelay="1" ToolTipService.Placement="Bottom"> 150 <Button.RenderTransform> 151 <RotateTransform x:Name="trans_forbidden" Angle="0"/> 152 </Button.RenderTransform> 153 <Button.Triggers> 154 <EventTrigger RoutedEvent="Button.MouseEnter"> 155 <BeginStoryboard > 156 <Storyboard> 157 <DoubleAnimation From="0" To="90" Duration="0:0:0.4" 158 Storyboard.TargetName="trans_forbidden" 159 Storyboard.TargetProperty="Angle"/> 160 </Storyboard> 161 </BeginStoryboard> 162 </EventTrigger> 163 </Button.Triggers> 164 </Button> 165 </Grid> 166 167 <Popup x:Name="Pop" PopupAnimation="Slide" Placement="Bottom" Width="93" Height="58" PlacementTarget="{Binding ElementName=grid_Cofig}" AllowsTransparency="True" StaysOpen="False" IsOpen="False" > 168 <Border Background="White" BorderBrush="#FFC4C9CD" BorderThickness="2" Width="93"> 169 <StackPanel Margin="1"> 170 <StackPanel Name="sp_play" Orientation="Horizontal" Width="83" Height="22" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="2" MouseEnter="sp_play_enter" MouseLeave="sp_play_leave" > 171 <CheckBox x:Name="ch_play" VerticalAlignment="Center" Margin="3,4" /> 172 <TextBlock x:Name="txb_play" Text="不再播放" Margin="5,0" VerticalAlignment="Center"/> 173 </StackPanel> 174 <StackPanel Name="sp_doc" Width="83" Height="22" Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="2" MouseEnter="sp_doc_enter" MouseLeave="sp_doc_leave"> 175 <TextBlock Name="txb_Doc" Text="官方幫助文檔" Margin="4,0,5,0" VerticalAlignment="Center"> 176 </TextBlock> 177 </StackPanel> 178 </StackPanel> 179 </Border> 180 </Popup> 181 182 <Button Name="btn_Close" Grid.Column="3" Width="30" Click="Close_Click" Margin="37,10,13,12" HorizontalAlignment="Center" Focusable="False" Style="{StaticResource Button_Close}" RenderTransformOrigin="0.5,0.5" ToolTipService.ToolTip="關閉視頻" ToolTipService.InitialShowDelay="1" ToolTipService.Placement="Bottom"> 183 <Button.RenderTransform> 184 <RotateTransform x:Name="trans" Angle="0"/> 185 </Button.RenderTransform> 186 <Button.Triggers> 187 <EventTrigger RoutedEvent="Button.MouseEnter"> 188 <BeginStoryboard > 189 <Storyboard> 190 <DoubleAnimation From="0" To="90" Duration="0:0:0.4" 191 Storyboard.TargetName="trans" 192 Storyboard.TargetProperty="Angle"/> 193 </Storyboard> 194 </BeginStoryboard> 195 </EventTrigger> 196 </Button.Triggers> 197 </Button> 198 199 </Grid> 200 </Border> 201 202 203 <Grid Name="Movie" Grid.Row="1"> 204 <MediaElement Stretch="Fill" LoadedBehavior="Manual" Name="QS_Movie" MediaOpened="Element_MediaOpened" Loaded="QS_Movie_Loaded" MouseLeftButtonDown="QS_Movie_MouseLeftButtonDown" /> 205 <Button Name="btn_pre" Width="30" Height="40" HorizontalAlignment="Left" VerticalAlignment="Center" Click="Left_Click" Focusable="False" Style="{StaticResource Button_Left}"/> 206 <Button Name="btn_next" Width="30" Height="40" HorizontalAlignment="Right" VerticalAlignment="Center" Click="Right_Click" Focusable="False" Style="{StaticResource Button_Right}"/> 207 </Grid> 208 209 210 <Border Name="Progress_Border" BorderBrush="#FBD3D0CD" BorderThickness="3" Grid.Row="2"> 211 <Grid Name="Control_Progress" > 212 <Slider Grid.Column="0" Width="700" Name="timelineSlider" VerticalAlignment="Center" PreviewMouseLeftButtonDown="timelineMDown" PreviewMouseLeftButtonUp="timelineMUp" BorderThickness="0,6,0,0" /> 213 </Grid> 214 </Border> 215 216 </Grid> 217 </Window>
後臺:
1 using System.Windows.Media; 2 using System.Windows.Media.Effects; 3 using System.Windows.Shapes; 4 5 namespace WPF_Nav 6 { 7 public class Particle 8 { 9 public Point3D Position { get; set; } 10 public Point3D Velocity { get; set; } 11 public double Size { get; set; } 12 public Ellipse Ellipse { get; set; } 13 public BlurEffect Blur { get; set; } 14 public Brush Brush { get; set; } 15 } 16 }
1 namespace WPF_Nav 2 { 3 public class Point3D 4 { 5 public double X { get; set; } 6 public double Y { get; set; } 7 public double Z { get; set; } 8 9 public Point3D(double X, double Y, double Z) 10 { 11 this.X = X; 12 this.Y = Y; 13 this.Z = Z; 14 } 15 } 16 }
主體:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Windows; 7 using System.Windows.Controls; 8 using System.Windows.Controls.Primitives; 9 using System.Windows.Data; 10 using System.Windows.Documents; 11 using System.Windows.Input; 12 using System.Windows.Media; 13 using System.Windows.Media.Imaging; 14 using System.Windows.Navigation; 15 using System.Windows.Shapes; 16 using System.Windows.Threading; 17 using System.Windows.Media.Effects; 18 19 namespace WPF_Nav 20 { 21 /// <summary> 22 /// MainWindow.xaml 的交互邏輯 23 /// </summary> 24 public partial class MainWindow : Window 25 { 26 DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer(); // 定義一個DT 27 bool play_flag = true; //判斷播放狀態 28 int play_now = 0; //判斷當前視頻索引 29 int play_target; 30 List<string> Play_Video = new List<string>(); 31 List<string> Title_Video = new List<string>(); 32 33 34 35 public MainWindow() 36 { 37 InitializeComponent(); 38 Play_Video = LoadMovies(); 39 Title_Video = LoadTitles(); 40 } 41 42 private List<string> LoadTitles() 43 { 44 List<string> list_title = new List<string>(); 45 list_title.Add("Step1"); 46 list_title.Add("Step2"); 47 list_title.Add("Step3"); 48 return list_title; 49 } 50 private List<string> LoadMovies() 51 { 52 List<string> Movie_Uri = new List<string>(); 53 Movie_Uri.Add(@"E:\Test\WPFTest\Sources\preview.mp4"); 54 Movie_Uri.Add(@"E:\Test\WPFTest\Sources\preview1.mp4"); 55 Movie_Uri.Add(@"E:\Test\WPFTest\Sources\preview2.mp4"); 56 return Movie_Uri; 57 } 58 59 private void Play_Click(object sender, RoutedEventArgs e) 60 { 61 QS_Movie.Play(); 62 } 63 64 private void Pause_Click(object