品牌网站建设哪好,设计师网页设计,美容培训东莞网站建设,十大网站开发公司WPF开发者QQ群此群已满340500857 #xff0c;请加新群458041663由于微信群人数太多入群请添加小编微信号yanjinhuawechat 或 W_Feng_aiQ 邀请入群需备注WPF开发者 01—代码如下一、创建AnimationAudio.xaml代码如下。ResourceDictionary xmlnshttp://schemas.micros… WPF开发者QQ群此群已满340500857 请加新群458041663 由于微信群人数太多入群请添加小编微信号 yanjinhuawechat 或 W_Feng_aiQ 邀请入群 需备注WPF开发者 01—代码如下一、创建AnimationAudio.xaml代码如下。ResourceDictionary xmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:controlsclr-namespace:WPFDevelopers.Controlsxmlns:helpersclr-namespace:WPFDevelopers.HelpersResourceDictionary.MergedDictionariesResourceDictionary SourceBasic/ControlBasic.xaml/ResourceDictionary SourceBasic/Animations.xaml//ResourceDictionary.MergedDictionariesStyle TargetType{x:Type controls:AnimationAudio} BasedOn{StaticResource ControlBasicStyle}Setter PropertyWidth Value80/Setter PropertyHeight Value35/Setter PropertyCursor ValueHand/Setter PropertyForeground Value{DynamicResource WhiteSolidColorBrush}/Setter PropertyBackground Value{DynamicResource PrimaryNormalSolidColorBrush}/Setter PropertyTemplateSetter.ValueControlTemplate TargetType{x:Type controls:AnimationAudio}ControlTemplate.ResourcesStoryboard x:KeyPlayStoryboard RepeatBehaviorForeverObjectAnimationUsingKeyFrames Storyboard.TargetNamePathAudioTwo Storyboard.TargetProperty(Path.Visibility)DiscreteObjectKeyFrame KeyTime0:0:0 Value{x:Static Visibility.Hidden} //ObjectAnimationUsingKeyFramesObjectAnimationUsingKeyFrames Storyboard.TargetNamePathAudioThree Storyboard.TargetProperty(Path.Visibility)DiscreteObjectKeyFrame KeyTime0:0:0 Value{x:Static Visibility.Hidden} //ObjectAnimationUsingKeyFramesObjectAnimationUsingKeyFrames BeginTime0:0:.3 Duration0:0:.4 Storyboard.TargetNamePathAudioTwoStoryboard.TargetProperty(Path.Visibility)DiscreteObjectKeyFrame KeyTime0:0:0 Value{x:Static Visibility.Visible} //ObjectAnimationUsingKeyFramesObjectAnimationUsingKeyFrames BeginTime0:0:.7 Duration0:0:.4 Storyboard.TargetNamePathAudioThreeStoryboard.TargetProperty(Path.Visibility)DiscreteObjectKeyFrame KeyTime0:0:0 Value{x:Static Visibility.Visible} //ObjectAnimationUsingKeyFrames/Storyboard/ControlTemplate.ResourcesBorder x:NamePART_Border Background{TemplateBinding Background} CornerRadius{TemplateBinding helpers:ControlsHelper.CornerRadius}SnapsToDevicePixelsTrue UseLayoutRoundingTrueGridGrid.ColumnDefinitionsColumnDefinition/ColumnDefinition//Grid.ColumnDefinitionsStackPanel Width20 Height30 HorizontalAlignmentLeft OrientationHorizontal Margin10,0RenderTransformOrigin.5,.5x:NamePART_StackPanelPath Data{StaticResource PathAudioOne} Width4 Height6 StretchFill Fill{TemplateBinding Foreground}/Path x:NamePathAudioTwo Data{StaticResource PathAudioTwo} Width6 StrokeThickness1.5 StrokeTransparent Margin0,7 StretchFill Fill{TemplateBinding Foreground}/Path x:NamePathAudioThree Data{StaticResource PathAudioThree} Width8 Margin-3,4 StretchFill Fill{TemplateBinding Foreground} StrokeThickness2 StrokeTransparent//StackPanelTextBlock VerticalAlignmentCenter Foreground{TemplateBinding Foreground}FontSize{DynamicResource TitleFontSize}Grid.Column1x:NamePART_TextBlockRun x:NamePART_RunTimeLength/Run/TextBlock/Grid/BorderControlTemplate.TriggersTrigger PropertyIsPlay ValueTrueTrigger.EnterActionsBeginStoryboard x:NamePlayBeginStoryboard Storyboard{StaticResource PlayStoryboard}//Trigger.EnterActionsTrigger.ExitActionsStopStoryboard BeginStoryboardNamePlayBeginStoryboard//Trigger.ExitActions/TriggerTrigger PropertyIsRight ValueTrueSetter PropertyGrid.Column TargetNamePART_TextBlock Value0/Setter PropertyHorizontalAlignment TargetNamePART_TextBlock ValueRight/Setter PropertyGrid.Column TargetNamePART_StackPanel Value1/Setter PropertyHorizontalAlignment TargetNamePART_StackPanel ValueRight/Setter PropertyRenderTransform TargetNamePART_StackPanelSetter.ValueTransformGroupRotateTransform Angle180//TransformGroup/Setter.Value/Setter/Trigger/ControlTemplate.Triggers/ControlTemplate/Setter.Value/Setter/Style/ResourceDictionary二、创建AnimationAudioe.cs代码如下。using System;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Interop;
using WPFDevelopers.Helpers;namespace WPFDevelopers.Controls
{[TemplatePart(Name RunTemplateName, Type typeof(Run))]public partial class AnimationAudio : Control{const string RunTemplateName PART_RunTimeLength;private Run _run;private TimeSpan _timeSpan;private IntPtr _handle;private AudioWindow _win null;static string[] mediaExtensions { .MP3, .WAV };/// summary/// 音频路径/// /summarypublic string AudioPath{get { return (string)GetValue(AudioPathProperty); }set { SetValue(AudioPathProperty, value); }}public static readonly DependencyProperty AudioPathProperty DependencyProperty.Register(AudioPath, typeof(string), typeof(AnimationAudio), new PropertyMetadata(string.Empty));/// summary/// 是否右侧/// /summarypublic bool IsRight{get { return (bool)GetValue(IsRightProperty); }set { SetValue(IsRightProperty, value); }}public static readonly DependencyProperty IsRightProperty DependencyProperty.Register(IsRight, typeof(bool), typeof(AnimationAudio), new PropertyMetadata(false));public bool IsPlay{get { return (bool)GetValue(IsPlayProperty); }set { SetValue(IsPlayProperty, value); }}public static readonly DependencyProperty IsPlayProperty DependencyProperty.Register(IsPlay, typeof(bool), typeof(AnimationAudio), new PropertyMetadata(false, new PropertyChangedCallback(OnIsPlayChanged)));private static void OnIsPlayChanged(DependencyObject d, DependencyPropertyChangedEventArgs e){bool newValue (bool)e.NewValue;var animationAudio d as AnimationAudio;if(newValue ! (bool)e.OldValue){if (newValue){animationAudio.Play();}else{AudioPlayer.Stop();}}}static AnimationAudio(){DefaultStyleKeyProperty.OverrideMetadata(typeof(AnimationAudio), new FrameworkPropertyMetadata(typeof(AnimationAudio)));}public override void OnApplyTemplate(){base.OnApplyTemplate();_run GetTemplateChild(RunTemplateName) as Run;if (string.IsNullOrWhiteSpace(AudioPath)) return;if (!File.Exists(AudioPath)) return;if (!mediaExtensions.Contains(Path.GetExtension(AudioPath), StringComparer.OrdinalIgnoreCase)) return;_timeSpan AudioPlayer.GetSoundLength(AudioPath);if (_timeSpan TimeSpan.Zero) return;_run.Text ${_timeSpan.Seconds.ToString()}\;Width 80;if (_timeSpan.Seconds 5){Width _timeSpan.Seconds;}}private void Play(){if(_win ! null){_win.Close();_win null;}_win new AudioWindow{Width 0,Height 0,Left Int32.MinValue,Top Int32.MinValue,WindowStyle WindowStyle.None,ShowInTaskbar false,ShowActivated false,};_win.Show();_win.StopDelegateEvent _win_StopDelegateEvent;_handle new WindowInteropHelper(_win).Handle;AudioPlayer.PlaySong(AudioPath, _handle);}private void _win_StopDelegateEvent(){IsPlay false;_win.Close();_win null;}}
}三、新建AudioWindow.cs代码如下。using System;
using System.Windows;
using System.Windows.Interop;namespace WPFDevelopers.Controls
{public class AudioWindow:Window{const int MM_MCINOTIFY 0x3B9;public delegate void StopDelegate();public event StopDelegate StopDelegateEvent;protected override void OnSourceInitialized(EventArgs e){base.OnSourceInitialized(e);HwndSource hwndSource PresentationSource.FromVisual(this) as HwndSource;if (hwndSource ! null){hwndSource.AddHook(new HwndSourceHook(this.WndProc));}}IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled){switch (msg){case MM_MCINOTIFY:StopDelegateEvent?.Invoke();break;}return IntPtr.Zero;}}
}四、新建AnimationAudioExample.xaml代码如下。UserControl x:ClassWPFDevelopers.Samples.ExampleViews.AnimationAudioExamplexmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006 xmlns:dhttp://schemas.microsoft.com/expression/blend/2008 xmlns:localclr-namespace:WPFDevelopers.Samples.ExampleViewsxmlns:wpfdevhttps://github.com/WPFDevelopersOrg/WPFDevelopersmc:Ignorabled d:DesignHeight450 d:DesignWidth800UniformGrid Columns2 x:NameMyUniformGridStackPanel OrientationHorizontalwpfdev:BreathLamp Width60 Height60 LampEffectRippleIsLampStarttrueMargin10,0Ellipse Width50 Height50Ellipse.FillImageBrush ImageSourcepack://application:,,,/WPFDevelopers.Samples;component/Images/Breathe/0.jpg//Ellipse.Fill/Ellipse/wpfdev:BreathLampwpfdev:AnimationAudio x:NameAnimationAudioLeft MouseDownAnimationAudioLeft_MouseDown//StackPanelStackPanel OrientationHorizontalHorizontalAlignmentRightwpfdev:AnimationAudio x:NameAnimationAudioRight IsRight true Background{DynamicResource SuccessSolidColorBrush}ForegroundBlackMouseDownAnimationAudioLeft_MouseDown/wpfdev:BreathLamp Width50 Height50 LampEffectStreamerBackgroundLightGrayIsLampStartTrueMargin10,0Ellipse Width43 Height43Ellipse.FillImageBrush ImageSourcepack://application:,,,/WPFDevelopers.Samples;component/Images/Chat/UserImages/yanjinhua.png//Ellipse.Fill/Ellipse/wpfdev:BreathLamp/StackPanel/UniformGrid
/UserControl六、新建AnimationAudioExample.xaml.cs下。using System;
using System.IO;
using System.Windows.Controls;
using WPFDevelopers.Controls;
using WPFDevelopers.Samples.Helpers;namespace WPFDevelopers.Samples.ExampleViews
{/// summary/// 微信公众号:WPF开发者/// /summarypublic partial class AnimationAudioExample : UserControl{public AnimationAudioExample(){InitializeComponent();AnimationAudioLeft.AudioPath Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Resources, Audio, HelloWPFDevelopes_en.mp3);AnimationAudioRight.AudioPath Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Resources, Audio, HelloWPFDevelopes_zh.mp3);}private void AnimationAudioLeft_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e){var animationAudio sender as AnimationAudio;var animationAudioList ElementVisualTreeHelper.FindVisualChildAnimationAudio(MyUniformGrid);if (animationAudioList null) return;if (!animationAudio.IsPlay){animationAudioList.ForEach(h {if (h.IsPlay h ! animationAudio){h.IsPlay false;}});animationAudio.IsPlay true;}elseanimationAudio.IsPlay false;}}
}02—效果预览鸣谢素材提供者 - 吴锋源码地址如下Githubhttps://github.com/WPFDevelopersOrgGiteehttps://gitee.com/WPFDevelopersOrgWPF开发者QQ群 340500857 | 458041663Githubhttps://github.com/WPFDevelopersOrg出处https://www.cnblogs.com/yanjinhua版权本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。转载请著名作者 出处 https://github.com/WPFDevelopersOrg扫一扫关注我们更多知识早知道点击阅读原文可跳转至源代码