wpf 让图标显示在系统托盘_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > wpf 让图标显示在系统托盘

wpf 让图标显示在系统托盘

 2015/3/4 13:14:28  明天将会怎样  程序员俱乐部  我要评论(0)
  • 摘要:上次做wpf时想把程序运行的图标显示在任务栏,结果发现wpf的系统托盘和winform的不一样,以前的方法不管用了。网上搜的好多都是winform的资料,wpf的很少。最后我把我现在做好的整理分享下,方便别人,也方便自己。文章难免有些错误,欢迎指正,下面代码usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Windows;usingSystem.Windows.Input
  • 标签:图标

上次做wpf时想把程序运行的图标显示在任务栏,结果发现wpf的系统托盘和winform的不一样,以前的方法不管用了。

网上搜的好多都是winform的资料,wpf的很少。

最后我把我现在做好的整理分享下,方便别人,也方便自己。

文章难免有些错误,欢迎指正,下面代码

 

class="brush:csharp;gutter:true;">using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Threading;
using Drawing = System.Drawing;
using System.Windows.Forms;

namespace WpfWin
{
    public class GWindow : Window
    {
        //托盘
        NotifyIcon notifyIcon;

        //注册AreaIcon属性,用于托盘的图标
        public static readonly DependencyProperty AreaIconProperty =
            DependencyProperty.Register("AreaIcon", typeof(ImageSource), typeof(GWindow));
        //注册AreaText属性,用于鼠标滑到托盘图标时显示的文字
        public static readonly DependencyProperty AreaTextProperty =
            DependencyProperty.Register("AreaText", typeof(string), typeof(GWindow));
        //注册AreaVisibility属性,用于显示隐藏托盘图标
        public static readonly DependencyProperty AreaVisibilityProperty =
            DependencyProperty.Register("AreaVisibility", typeof(bool), typeof(GWindow));
        //注册AreaMenuItems属性,用于托盘右键在单的列表
        public static readonly DependencyProperty AreaMenuItemsProperty =
            DependencyProperty.Register("AreaMenuItems", typeof(List<MenuItem>), typeof(GWindow), new PropertyMetadata(new List<MenuItem>()));

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);

            notifyIcon = new NotifyIcon();
            notifyIcon.Text = AreaText;
            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                notifyIcon.Icon = GetImageSource(AreaIcon);
            }
            notifyIcon.Visible = AreaVisibility;

            if (this.AreaMenuItems != null && this.AreaMenuItems.Count > 0)
            {
                notifyIcon.ContextMenu = new ContextMenu(this.AreaMenuItems.ToArray());
            }
        }

        public List<MenuItem> AreaMenuItems
        {
            get { return (List<MenuItem>)GetValue(AreaMenuItemsProperty); }
            set { SetValue(AreaMenuItemsProperty, value); }
        }

        public ImageSource AreaIcon
        {
            get { return (ImageSource)GetValue(AreaIconProperty); }
            set { SetValue(IconProperty, value); }
        }

        public string AreaText
        {
            get { return (string)GetValue(AreaTextProperty); }
            set { SetValue(AreaTextProperty, value); }
        }

        public bool AreaVisibility
        {
            get { return (bool)GetValue(AreaVisibilityProperty); }
            set { SetValue(AreaVisibilityProperty, value); }
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            notifyIcon.Visible = false;
            notifyIcon.Dispose();
            AreaMenuItems.Clear();
        }

        private static Drawing.Icon GetImageSource(ImageSource icon)
        {
            if (icon == null)
            {
                return null;
            }
            Uri iconUri = new Uri(icon.ToString());
            return new Drawing.Icon(System.Windows.Application.GetResourceStream(iconUri).Stream);
        }
    }
}

 

前台调用时的代码,直接将Window  换成  GWindow,在后面就可以设置属性了

AreaMenuItems   中设置托盘图标右键菜单,自己设定
<loacl:GWindow x:Class="WpfWin.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:loacl="clr-namespace:WpfWin"
        xmlns:forms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
        Title="MainWindow" Height="350" Width="525" AreaText="MainWindow" ShowInTaskbar="False" AreaVisibility="True"  AreaIcon="Image/clock.ico" Icon="Image/clock.ico">
    <loacl:GWindow.AreaMenuItems>
        <forms:MenuItem Text="Open" DefaultItem="True" />
        <forms:MenuItem Text="Exit" />
    </loacl:GWindow.AreaMenuItems>
    <Grid/>
</loacl:GWindow>

 

程序运行图片

QQmd3I55AkKRS2T/wl6q/4eFhiko2BjiOYxjmmBMMwwKBQJINKYoaHh5OsgKHL65Vrc+db9piMhkWGv1koWm6Rwd3q6BeNTxhcSCegJ+K+EgKD4RdwbDV7X8kM1/qkp1vl6byuwB/qfrr6+sjSTLRo9Fo1JI4ZrM5ydiQJNnX15dkhXnxxSqclI8vBsG8oWm6TwcLlJbbY/rrj6ca+6bq2iSf3n7yVaesRwN3KMy3xLrG3qnPgb/s+hMKhUn8qVQqCIIgCLJYLC+tXXfn7t07d+++8upr3J1JtiVJUigUJhm85P5YgsueBWmaFmmsgino5oimoUdxVaQ4+3C8+s5og1Deq4U7FJY7Yv0VkfzmoHp5+2cD/KXqTyAQJHmXTK1WoyiKouiHO3ZMTEzYbDZ28c/r16MoiiBIEn+hUEggECQZvETH3/rB5wdNdX/v+8djgqLzTVsWJ2H+vHP61s7LnV88mhDKoYYu+dGbQxcfTXarrUrEhXqD42bszqju6x7FsvcP/KXqr62tLYk/rVbrdDr/5Re/bG9vj/26T4VC8cKLLzocjuT+2trakgyey+Wai+/rJ2uCFMEwTP3g8+wUuDgJ82dHg+jgdwOf3H5y+sFY9QOxBHJMIs5+vU04BT2QGpqGNdd6FV91yZa9f+BvPn+RABN2cKWpwLL96XQ6HMd/9+//8dLatXhMnjx58tt//Ten05mKP2bmEFzT9dwd2Rb2V/b+bt1xTuQCBJKm+q740A+DZ1rH69qlF4WT4xAmtTmGjWiXCnogNdwc1jT2TV0C/nLWn1qtttvtv1q1yj6TX//6N3a73Ww2/2rVKhRFV8Tf10/WMAzjChrZO8etjbEzYvI9JIovTI4YUSnkQDwBT4jsnrJe6pZLrU45ik/Ajm6V5Zpw7FRL791hVTSFlwDB+2/p9ad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uKBtYX9f25qOv1ovZXi279d9HVNUXFL/3Grx1Bbh5Fbh5Fmo4iTXzfVH/FX9dkfajyshV/XeOb6keajiJNR/8fZd0uwM4+DRkAAAAASUVORK5CYII=" alt="" />

 

源码已全部贴出,就不另设下载地址了

发表评论
用户名: 匿名