码迷,mamicode.com
首页 > 其他好文 > 详细

桌面下雪效果(二)

时间:2015-10-14 19:24:44      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

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

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();
        }
    }


源码

桌面下雪效果(二)

标签:

原文地址:http://www.cnblogs.com/XzcBlog/p/4878090.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!