WPF 托盘闪烁 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
yanjinhua
V2EX    C#

WPF 托盘闪烁

  •  
  •   yanjinhua 2023-01-11 15:55:12 +08:00 1574 次点击
    这是一个创建于 1003 天前的主题,其中的信息可能已经有所发展或是发生改变。

    WPF 托盘闪烁

    控件名:NotifyIcon

    作者:WPFDevelopersOrg - 弈虎、

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

    • 框架使用大于等于.NET40
    • Visual Studio 2022
    • 项目使用 MIT 开源许可协议。
    • 接着上一篇基础托盘
    • 新增如下:
      • 增加属性TwinkInterval托盘图标闪烁间隔默认500msIsTwink = true 开启闪烁。
      • 设计器时不显示托盘。
    • Nuget 最新 Install-Package WPFDevelopers
    • Nuget 最新 Install-Package WPFDevelopers..Minimal

    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

    目前尚无回复
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     6051 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 29ms UTC 02:37 PVG 10:37 LAX 19:37 JFK 22:37
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86