?WPF 托盤閃爍
控件名:NotifyIcon
作者:WPFDevelopersOrg ?- 弈虎、驚鏵
原文鏈接:? ?https://github.com/WPFDevelopersOrg/WPFDevelopers
框架使用大于等于
.NET40
。Visual Studio 2022
。項目使用 MIT 開源許可協議。
接著上一篇基礎托盤。
新增如下:
增加屬性
TwinkInterval
托盤圖標閃爍間隔默認500ms
,IsTwink = true
開啟閃爍。設計器時不顯示托盤。
Nuget 最新[1]
Install-Package WPFDevelopers
1.0.9.2-previewNuget 最新[2]
Install-Package WPFDevelopers..Minimal
3.2.8.2-preview
1)托盤閃爍代碼如下:
///?<summary>///?托盤圖標閃爍間隔///?</summary>public?static?readonly?DependencyProperty?TwinkIntervalProperty?=?DependencyProperty.Register("TwinkInterval",typeof(TimeSpan),?typeof(NotifyIcon),?new?PropertyMetadata(TimeSpan.FromMilliseconds(500),?OnTwinkIntervalChanged));///?<summary>///?托盤圖標是否開啟閃爍///?</summary>public?static?readonly?DependencyProperty?IsTwinkProperty?=?DependencyProperty.Register("IsTwink",?typeof(bool),?typeof(NotifyIcon),?new?PropertyMetadata(false,?OnIsTwinkChanged));private?static?void?OnIsTwinkChanged(DependencyObject?d,?DependencyPropertyChangedEventArgs?e){if?(d?is?NotifyIcon?trayService){var?notifyIcon?=?(NotifyIcon)d;if?(notifyIcon.Visibility?!=?Visibility.Visible)?return;if?((bool)e.NewValue){if?(notifyIcon._dispatcherTimerTwink?==?null){notifyIcon._dispatcherTimerTwink?=?new?DispatcherTimer{Interval?=?notifyIcon.TwinkInterval};notifyIcon._dispatcherTimerTwink.Tick?+=?notifyIcon.DispatcherTimerTwinkTick;}notifyIcon._tempIconHandle?=?notifyIcon._hIcon;notifyIcon._dispatcherTimerTwink.Start();}else{notifyIcon._dispatcherTimerTwink?.Stop();notifyIcon._dispatcherTimerTwink.Tick?-=?notifyIcon.DispatcherTimerTwinkTick;notifyIcon._dispatcherTimerTwink?=?null;notifyIcon._iconHandle?=?notifyIcon._tempIconHandle;notifyIcon.ChangeIcon(false,?notifyIcon._iconHandle);notifyIcon._tempIconHandle?=?IntPtr.Zero;}}}private?static?void?OnTwinkIntervalChanged(DependencyObject?d,?DependencyPropertyChangedEventArgs?e){if?(d?is?NotifyIcon?trayService){var?notifyIcon?=?(NotifyIcon)d;notifyIcon._dispatcherTimerTwink.Interval?=?(TimeSpan)e.NewValue;}}
2)設計器時不顯示托盤代碼如下:
if?(DesignerHelper.IsInDesignMode?==?true)?return?false;#region?是否設計時模式public?class?DesignerHelper{private?static?bool??_isInDesignMode;public?static?bool?IsInDesignMode{get{if?(!_isInDesignMode.HasValue){_isInDesignMode?=?(bool)DependencyPropertyDescriptor.FromProperty(DesignerProperties.IsInDesignModeProperty,typeof(FrameworkElement)).Metadata.DefaultValue;}return?_isInDesignMode.Value;}}}#endregion
3)使用托盤代碼如下:
<wpfdev:NotifyIcon?Title="WPF開發者"?Name="WpfNotifyIcon"><wpfdev:NotifyIcon.ContextMenu><ContextMenu><MenuItem?Header="托盤消息"?Click="SendMessage_Click"/><MenuItem?Header="閃爍"?Name="menuItemTwink"??Click="Twink_Click"/><MenuItem?Header="關于"?Click="About_Click"><MenuItem.Icon><Path?Data="{StaticResource?PathWarning}"?Fill="{DynamicResource?PrimaryNormalSolidColorBrush}"Stretch="Uniform"?Height="20"?Width="20"/></MenuItem.Icon></MenuItem><MenuItem?Header="退出"?Click="Quit_Click"/></ContextMenu></wpfdev:NotifyIcon.ContextMenu></wpfdev:NotifyIcon>
4)事件Twink_Click
代碼如下:
private?void?Twink_Click(object?sender,?RoutedEventArgs?e){WpfNotifyIcon.IsTwink?=?!WpfNotifyIcon.IsTwink;menuItemTwink.IsChecked?=?WpfNotifyIcon.IsTwink;}
?鳴謝 - 弈虎

Github|NotifyIcon[3]
碼云|NotifyIcon[4]
參考資料
[1]
Nuget : https://www.nuget.org/packages/WPFDevelopers/
[2]Nuget : https://www.nuget.org/packages/WPFDevelopers.Minimal/
[3]Github|NotifyIcon: https://github.com/WPFDevelopersOrg/WPFDevelopers/blob/master/src/WPFDevelopers.Samples/ExampleViews/MainWindow.xaml
[4]碼云|NotifyIcon: https://gitee.com/WPFDevelopersOrg/WPFDevelopers/blob/master/src/WPFDevelopers.Samples/ExampleViews/MainWindow.xaml