WPF 学习笔记-在WPF下创建托盘图标

首先需要在项目中引用System.Windows.Forms,System.Drawing;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing;

namespace WpfApplication1
{
    ///
    /// Interaction logic for MainWindow.xaml
    ///
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            InitialTray();
        }
        private System.Windows.Forms.NotifyIcon notifyIcon = null;
        private void InitialTray()
        {

            //设置托盘的各个属性
            notifyIcon = new System.Windows.Forms.NotifyIcon();
            notifyIcon.BalloonTipText = \"程序开始运行\";
            notifyIcon.Text = \"托盘图标\";
            notifyIcon.Icon = new System.Drawing.Icon(System.Windows.Forms.Application.StartupPath + \"\\\\wp.ico\");
            notifyIcon.Visible = true;
            notifyIcon.ShowBalloonTip(2000);
            notifyIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(notifyIcon_MouseClick);

            //设置菜单项
            System.Windows.Forms.MenuItem menu1 = new System.Windows.Forms.MenuItem(\"菜单项1\");
            System.Windows.Forms.MenuItem menu2 = new System.Windows.Forms.MenuItem(\"菜单项2\");
            System.Windows.Forms.MenuItem menu = new System.Windows.Forms.MenuItem(\"菜单\", new System.Windows.Forms.MenuItem[] { menu1 , menu2 });

            //退出菜单项
            System.Windows.Forms.MenuItem exit = new System.Windows.Forms.MenuItem(\"exit\");
            exit.Click += new EventHandler(exit_Click);

            //关联托盘控件
            System.Windows.Forms.MenuItem[] childen = new System.Windows.Forms.MenuItem[] { menu , exit };
            notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(childen);

            //窗体状态改变时候触发
            this.StateChanged += new EventHandler(SysTray_StateChanged);
        }
        ///
        /// 窗体状态改变时候触发
        ///
        ///

        ///

        private void SysTray_StateChanged(object sender, EventArgs e)
        {
            if (this.WindowState == WindowState.Minimized)
            {
                this.Visibility = Visibility.Hidden;
            }
        }

        ///
        /// 退出选项
        ///
        ///

        ///

        private void exit_Click(object sender, EventArgs e)
        {
            if (System.Windows.MessageBox.Show(\"确定要关闭吗?\",
                                               \"退出\",
                                                MessageBoxButton.YesNo,
                                                MessageBoxImage.Question,
                                                MessageBoxResult.No) == MessageBoxResult.Yes)
            {
                notifyIcon.Dispose();
                System.Windows.Application.Current.Shutdown();
            }
        }

        ///
        /// 鼠标单击
        ///
        ///

        ///

        private void notifyIcon_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                if (this.Visibility == Visibility.Visible)
                {
                    this.Visibility = Visibility.Hidden;
                }
                else
                {
                    this.Visibility = Visibility.Visible;
                    this.Activate();
                }
            }
        }
    }
}
 
 
以上代码并非用户控件代码,只需加在主窗体中即可。

转载于:https://www.cnblogs.com/hayywcy/archive/2011/11/29/2267515.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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中双击托盘图标显示窗体的功能了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值