WPF最小到系统托盘

让WPF应用最小到系统托盘?可以调用System.Windows.Forms.NotifyIcon来实现,下面是示例代码:

View Code
public partial class MainWindow : Window
    {
        private NotifyIcon notifyIcon;
        public MainWindow()
        {
            InitializeComponent();

            this.notifyIcon = new NotifyIcon();
            this.notifyIcon.BalloonTipText = "系统监控中... ...";
            this.notifyIcon.ShowBalloonTip(2000);
            this.notifyIcon.Text = "系统监控中... ...";
            this.notifyIcon.Icon = new System.Drawing.Icon(@"AppIcon.ico");
            this.notifyIcon.Visible = true;
            //打开菜单项
            System.Windows.Forms.MenuItem open = new System.Windows.Forms.MenuItem("Open");
            open.Click += new EventHandler(Show);
            //退出菜单项
            System.Windows.Forms.MenuItem exit = new System.Windows.Forms.MenuItem("Exit");
            exit.Click += new EventHandler(Close);
            //关联托盘控件
            System.Windows.Forms.MenuItem[] childen = new System.Windows.Forms.MenuItem[] { open, exit };
            notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(childen);

            this.notifyIcon.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler((o, e) =>
            {
                if (e.Button == MouseButtons.Left) this.Show(o, e);
            });
        }

        private void Show(object sender, EventArgs e)
        {
            this.Visibility = System.Windows.Visibility.Visible;
            this.ShowInTaskbar = true;
            this.Activate();
        }

        private void Hide(object sender, EventArgs e)
        {
            this.ShowInTaskbar = false;
            this.Visibility = System.Windows.Visibility.Hidden;
        }

        private void Close(object sender, EventArgs e)
        {
            System.Windows.Application.Current.Shutdown();
        }
    }

运行时发现,程序一定要能找到ICON,否则会报错,并且ICON还没包含到程序中,需要一个额外的ICON来做托盘图标。当然这个都是能解决的:

this.notifyIcon.Icon = new System.Drawing.Icon(@"AppIcon.ico");

将以上一句替换成下面内容,意思就是读取程序图标,来作为托盘图标

this.notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath);

运行,搞定,这样你的程序就不用拖着一个ICON文件当累赘了。

 

 

 

转载于:https://www.cnblogs.com/ke10/archive/2012/11/16/NotifyIcon.html

WPF最小化托盘(System Tray)的实现可以通过以下步骤来完成: 1. 首先,在XAML文件中创建一个托盘图标。可以使用Rectangle、Ellipse或Image等控件来作为图标,然后设置图标的宽度、高度和颜色等属性。 ```xml <Rectangle Width="16" Height="16" Fill="Red" /> ``` 2. 在MainWindow.xaml.cs文件中,首先添加以下命名空间: ```csharp using System.Windows.Forms; ``` 3. 在MainWindow.xaml.cs文件中,创建一个NotifyIcon对象,并在窗口加载事件中进行初始化,包括设置托盘图标和右键菜单等属性。 ```csharp private NotifyIcon notifyIcon; private void MainWindow_Loaded(object sender, RoutedEventArgs e) { notifyIcon = new NotifyIcon(); notifyIcon.Icon = new System.Drawing.Icon("Icon.ico"); notifyIcon.Visible = true; notifyIcon.Text = "My WPF Application"; ContextMenu menu = new ContextMenu(); // 创建右键菜单 MenuItem showMenuItem = new MenuItem(); showMenuItem.Text = "显示"; showMenuItem.Click += new EventHandler(ShowMenuItem_Click); menu.MenuItems.Add(showMenuItem); MenuItem exitMenuItem = new MenuItem(); exitMenuItem.Text = "退出"; exitMenuItem.Click += new EventHandler(ExitMenuItem_Click); menu.MenuItems.Add(exitMenuItem); notifyIcon.ContextMenu = menu; } ``` 4. 创建点击事件处理方法来处理托盘图标的左键双击事件,以及右键菜单中的"显示"和"退出"事件。 ```csharp private void TrayIcon_DoubleClick(object sender, EventArgs e) { this.Show(); // 双击托盘图标时显示主窗口 this.WindowState = WindowState.Normal; } private void ShowMenuItem_Click(object sender, EventArgs e) { this.Show(); // 右键菜单中点击"显示"时显示主窗口 this.WindowState = WindowState.Normal; } private void ExitMenuItem_Click(object sender, EventArgs e) { this.Close(); // 右键菜单中点击"退出"时关闭应用程序 } ``` 5. 最后,在MainWindow.xaml文件中的Window标签中添加Loaded事件调用MainWindow_Loaded方法。 ```xml <Window x:Class="Demo.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="WPF最小化托盘Demo" Height="450" Width="800" Loaded="MainWindow_Loaded"> <!-- 主窗口内容 --> </Window> ``` 通过以上步骤,我们可以实现一个简单的WPF最小化托盘Demo,点击托盘图标可以显示主窗口,右键菜单中可以选择显示或退出应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值