增加系统托盘图标,并取消任务栏图标
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(); } }
转载于:https://www.cnblogs.com/XzcBlog/p/4878090.html
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/37641.html