【C#】窗体最小化到托盘(WinForm和WPF)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ght886/article/details/84481583

下面以WPF为例

新建WPF项目Test,主窗体MainWindow.xaml,在后台MainWindow.xaml.cs填写下面的代码。然后就能实现最小化到托盘的功能。

//引用根据需要添加,可以去除不必要的引用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Windows.Threading;

using System.ServiceModel;
using System.Drawing; //添加引用
using System.Windows.Forms;//添加引用,必须用到的
using System.Diagnostics;
using System.Runtime.InteropServices;
using MouseEventArgs = System.Windows.Input.MouseEventArgs;

using EF;
using BLL;
using System.Data;

namespace Test
{
    public partial class MainWindow : Window
    {
       //实例化notifyIOC控件最小化托盘
        private NotifyIcon notifyIcon = null;
        
       // 构造函数
        public MainWindow()
        {
            InitializeComponent();
        }
        
        // 最小化系统托盘
        private void initialTray()
        {
            //隐藏主窗体
            this.Visibility = Visibility.Hidden;
            //设置托盘的各个属性
            notifyIcon = new NotifyIcon();
            notifyIcon.BalloonTipText = "番茄时钟运行中...";//托盘气泡显示内容
            notifyIcon.Text = "DamifanqieApp";
            notifyIcon.Visible = true;//托盘按钮是否可见
            notifyIcon.Icon = new System.Drawing.Icon("../../Resources/tomato1.ico");//托盘中显示的图标
            notifyIcon.ShowBalloonTip(1000);//托盘气泡显示时间
            //双击事件
            //_notifyIcon.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(notifyIcon_MouseClick);
            //鼠标点击事件
            notifyIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(notifyIcon_MouseClick);
            //窗体状态改变时触发
            this.StateChanged += MainWindow_StateChanged;
        }
      

       // 托盘图标鼠标单击事件
        private void notifyIcon_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            //鼠标左键,实现窗体最小化隐藏或显示窗体
            if (e.Button == MouseButtons.Left)
            {
                if (this.Visibility == Visibility.Visible)
                {
                    this.Visibility = Visibility.Hidden;
                }
                else
                {
                    this.Visibility = Visibility.Visible;
                    this.Activate();
                }
            }
            if (e.Button == MouseButtons.Right)
            {
                //object sender = new object();
                // EventArgs e = new EventArgs();
                exit_Click(sender, e);//触发单击退出事件
            }
        }
      
        // 窗体状态改变时候触发
        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("确定退出吗?",
                                               "application",
                                                MessageBoxButton.YesNo,
                                                MessageBoxImage.Question,
                                                MessageBoxResult.Yes) == MessageBoxResult.Yes)
            {
                //System.Windows.Application.Current.Shutdown();
                System.Environment.Exit(0);
            }
        }
      

       // 窗口状态改变,最小化托盘
        private void MainWindow_StateChanged(object sender, EventArgs e)
        {
            if (this.WindowState == WindowState.Minimized)
            {
                this.Visibility = Visibility.Hidden;
            }
        }
       

猜你喜欢

转载自blog.csdn.net/ght886/article/details/84481583