WPF应用在系统托盘显示图标

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

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

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

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

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>
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
WPF中,要实现托盘双击显示窗体的功能,可以按照以下几个步骤进行操作。 首先,在XAML文件中添加一个System.Windows.Forms的命名空间引用,这是因为托盘图标使用的是Windows Forms的控件。 然后,在XAML文件中添加一个NotifyIcon控件,用于创建托盘图标。可以设置Icon(图标)、Text(鼠标悬停时显示的文本)等属性。 接下来,在窗体的Loaded事件中,使用以下代码实现托盘图标显示: ```csharp private System.Windows.Forms.NotifyIcon notifyIcon; private void Window_Loaded(object sender, RoutedEventArgs e) { notifyIcon = new System.Windows.Forms.NotifyIcon(); notifyIcon.Icon = new System.Drawing.Icon("icon.ico"); // 设置托盘图标 notifyIcon.Text = "双击显示窗体"; // 设置鼠标悬停时显示的文本 notifyIcon.DoubleClick += NotifyIcon_DoubleClick; // 添加双击事件处理函数 // 显示托盘图标 notifyIcon.Visible = true; } ``` 在双击事件处理函数NotifyIcon_DoubleClick中,可以使用以下代码实现窗体的显示: ```csharp private void NotifyIcon_DoubleClick(object sender, EventArgs e) { this.Show(); // 显示窗体 this.WindowState = WindowState.Normal; // 恢复到正常状态 } ``` 需要注意的是,在窗体的Closing事件中,应添加以下代码,以确保在关闭窗体时托盘图标也被释放: ```csharp private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) { notifyIcon.Dispose(); // 释放托盘图标资源 } ``` 通过以上步骤,就可以实现在WPF中双击托盘图标显示窗体的功能了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值