WPF 托盘闪烁

2023-01-11 15:55:12 +08:00
 yanjinhua

WPF 托盘闪烁

控件名:NotifyIcon

作者:WPFDevelopersOrg - 弈虎、驚鏵

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

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
码云|NotifyIcon

1125 次点击
所在节点    C#
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/908180

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX