桌面下雪效果(二)

增加系统托盘图标,并取消任务栏图标

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值