WPF 托盘闪烁
WPF 托盘闪烁
控件名:NotifyIcon
作者:WPFDevelopersOrg - 弈虎、驚鏵
原文链接:
框架使用 大于等于.NET40打开网页闪烁 。
Visual Studio 2022打开网页闪烁 。
项目使用 MIT开源许可协议打开网页闪烁 。
接着上一篇 基础托盘打开网页闪烁 。
新增如下:
增加属性 TwinkInterval托盘图标闪烁间隔默认 500ms, IsTwink = true 开启闪烁打开网页闪烁 。
设计器时不显示托盘打开网页闪烁 。
Nuget 最新[1] Install-Package WPFDevelopers 1.0.9.2-preview
Nuget 最新[2] Install-Package WPFDevelopers..Minimal 3.2.8.2-preview
1)托盘闪烁代码如下:
///<summary>
///托盘图标闪烁间隔
///</summary>
publicstaticreadonlyDependencyProperty TwinkIntervalProperty =
DependencyProperty.Register( "TwinkInterval",
typeof(TimeSpan), typeof(NotifyIcon), newPropertyMetadata(TimeSpan.FromMilliseconds( 500), OnTwinkIntervalChanged));
///<summary>
///托盘图标是否开启闪烁
///</summary>
publicstaticreadonlyDependencyProperty IsTwinkProperty =
DependencyProperty.Register( "IsTwink",
typeof( bool), typeof(NotifyIcon), newPropertyMetadata( false, OnIsTwinkChanged));
privatestaticvoidOnIsTwinkChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
if(d isNotifyIcon trayService)
varnotifyIcon = (NotifyIcon)d;
if(notifyIcon.Visibility != Visibility.Visible) return;
if(( bool)e.NewValue)
if(notifyIcon._dispatcherTimerTwink == null)
notifyIcon._dispatcherTimerTwink = newDispatcherTimer
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;
privatestaticvoidOnTwinkIntervalChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
if(d isNotifyIcon trayService)
varnotifyIcon = (NotifyIcon)d;
notifyIcon._dispatcherTimerTwink.Interval = (TimeSpan)e.NewValue;
2)设计器时不显示托盘代码如下:
if(DesignerHelper.IsInDesignMode == true) returnfalse;
#region是否设计时模式
publicclassDesignerHelper
privatestaticbool? _isInDesignMode;
publicstaticboolIsInDesignMode
get
if(!_isInDesignMode.HasValue)
_isInDesignMode = ( bool)DependencyPropertyDeor.FromProperty(DesignerProperties.IsInDesignModeProperty,
typeof(FrameworkElement)).Metadata.DefaultValue;
return_isInDesignMode.Value;
#endregion
3)使用托盘代码如下:
<wpfdev:NotifyIconTitle="WPF开发者"Name="WpfNotifyIcon">
<wpfdev:NotifyIcon.ContextMenu>
<ContextMenu>
<MenuItemHeader="托盘消息"Click="SendMessage_Click"/>
<MenuItemHeader="闪烁"Name="menuItemTwink"Click="Twink_Click"/>
<MenuItemHeader="关于"Click="About_Click">
<MenuItem.Icon>
<PathData="{StaticResource PathWarning}"
Fill="{DynamicResource PrimaryNormalSolidColorBrush}"
Stretch="Uniform"Height="20"Width="20"/>
</MenuItem.Icon>
</MenuItem>
<MenuItemHeader="退出"Click="Quit_Click"/>
</ContextMenu>
</wpfdev:NotifyIcon.ContextMenu>
</wpfdev:NotifyIcon>
4)事件Twink_Click代码如下:
privatevoidTwink_Click(objectsender, RoutedEventArgs e)
WpfNotifyIcon.IsTwink = !WpfNotifyIcon.IsTwink;
menuItemTwink.IsChecked = WpfNotifyIcon.IsTwink;
鸣谢 - 弈虎
Github|NotifyIcon[3]
码云|NotifyIcon[4]
参考资料
[1]
Nuget : /
[2]
Nuget : /
[3]
Github|NotifyIcon:
[4]
码云|NotifyIcon:
爱资源吧版权声明:以上文中内容来自网络,如有侵权请联系删除,谢谢。