桌面下雪效果(二)
增加系统托盘图标,并取消任务栏图标
1.取消任务栏图标
启动窗口设置ShowInTaskbar="False"
<Window x:Class="SnowEffect.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Background="Transparent" WindowState="Maximized" WindowStyle="None" ShowInTaskbar="False" IsHitTestVisible="False" AllowsTransparency="True" DataContext="{Binding Main, Source={StaticResource Locator}}"> <Grid x:Name="LayoutRoot" Background="Transparent"/> </Window>
2.增加系统托盘图标
新增SystemNotifyIcon类
/// <summary> /// 系统托盘图标 /// </summary> internal class SystemNotifyIcon { /// <summary> /// 添加系统托盘 /// </summary> internal static void AddSystemNotifyIcon() { new NotifyIcon { Text = Properties.Resources.SnowFlower, Visible = true, Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath), ContextMenu = new ContextMenu(GetSystemNotifyIconContextMenuItems()) }; } //获取系统托盘的右键关联菜单 private static MenuItem[] GetSystemNotifyIconContextMenuItems() { var shutdownItem = new MenuItem { Text = Properties.Resources.Shutdown }; shutdownItem.Click += (sender, args) => { System.Windows.Application.Current.Shutdown(); }; var result = new[] { shutdownItem }; return result; } }
在App.xaml.cs中调用方法
public partial class App { static App() { DispatcherHelper.Initialize(); SystemNotifyIcon.AddSystemNotifyIcon(); } }