WPF 托盘闪烁

 WPF 托盘闪烁

控件名:NotifyIcon

作者:WPFDevelopersOrg  - 弈虎、驚鏵

原文链接:   https://github.com/WPFDevelopersOrg/WPFDevelopers

  • 框架使用大于等于.NET40

  • Visual Studio 2022

  • 项目使用 MIT 开源许可协议。

  • 接着上一篇基础托盘

  • 新增如下:

    • 增加属性TwinkInterval托盘图标闪烁间隔默认500msIsTwink = 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>
       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;
        }

 鸣谢 - 弈虎

229b6ac059d1b2ffb20250d2eacbeaf2.gif

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值