下面以WPF为例,Winform几乎一样的效果
新建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;//托盘按钮是否可见
//重要提示:此处的图标图片在resouces文件夹。可是打包后安装发现无法获取路径,导致程序死机。建议复制一份resouces文件到UI层的bin目录下,确保万无一失。
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);
//右键菜单–退出菜单项
System.Windows.Forms.MenuItem exit = new System.Windows.Forms.MenuItem(“关闭”);
exit.Click += new EventHandler(exit_Click);
//关联托盘控件
System.Windows.Forms.MenuItem[] childen = new System.Windows.Forms.MenuItem[] { exit };
notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(childen);
//窗体状态改变时触发
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;
//解决最小化到任务栏可以强行关闭程序的问题。
this.ShowInTaskbar = false;//使Form不在任务栏上显示
}
else
{
this.Visibility = Visibility.Visible;
//解决最小化到任务栏可以强行关闭程序的问题。
this.ShowInTaskbar = false;//使Form不在任务栏上显示
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;
}
}
二、解决最小化程序后,程序图标重复问题。
2.1 问题原因:每次点击最小化或者关闭按钮后,都要执行一次最小化程序,每次都要添加一个最小化图标。导致多个图标。
2.2 问题解决思路:当程序运行开始,让它只执行一次,以后只让窗体隐藏即可。设置一个全局静态变量a,在主窗体中的构造函数中给a赋值“关闭”(一定是窗体构造函数中赋值。因为窗体load事件以后会执行很多次,会重复给a赋值)。因为番茄时钟的主窗体是不关闭的,所以全局变量的“关闭”只有一次。当程序执行一次后,给a赋值“不关闭”,这样以后a的值就都是“不关闭”了。(因为主窗体一直不会关闭。)然后当用户自定义窗体改变最小化的标签min时,只有两种状态0或1。在主窗体关闭事件中最判断,只有同时为global.min=0且global.a=“关闭”时,才执行最小化事件,其他情况都是隐藏事件或者关闭。
2.3 主窗体代码:
#region 主窗体构造函数
/// <summary>
///构造函数
/// </summary>
public MainWindow()
{
InitializeComponent();
GlobalData.win_close = “关闭”;
}
#endregion
#region 关闭窗体事件
/// <summary>
/// 关闭窗体事件
/// </summary>
/// <param name=”sender”></param>
/// <param name=”e”></param>
private void lblClose_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
//判断,只有同时
if (GlobalData.min == 0 && GlobalData.win_close == “关闭”)
{
initialTray();
//保证窗体显示在上方。
//wsl = WindowState;
//GlobalData.min = 2;
GlobalData.win_close = “不关闭”;
}
else if (GlobalData.min == 0)
{
this.Visibility = Visibility.Hidden;
}
else if (GlobalData.min == 1)
{
System.Environment.Exit(0);//关闭整个程序主界面
}
#endregion
2.4 用户自定义窗体代码:
#region 最小化功能
//选择最小化功能
private void RadioMinimize_Checked(object sender, RoutedEventArgs e)
{
if (RadioMinimize.IsChecked == true)
{
GlobalData.min = 0;//0表示最小化
userDefine.Mac_id = GlobalData.mac_id;
userDefine.Min_type = GlobalData.min;
userBll.updateUserMinstype(userDefine);
}
}
//选择退出功能
private void RadioExit_Checked(object sender, RoutedEventArgs e)
{
if (RadioExit.IsChecked == true)
{
GlobalData.min = 1;//1表示退出
userDefine.Mac_id = GlobalData.mac_id;
userDefine.Min_type = GlobalData.min;
userBll.updateUserMinstype(userDefine);
}
}
#endregion
————————————————
版权声明:本文为CSDN博主「Kevin’s life」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/ght886/article/details/84481583