要实现C#窗体最小化到托盘,需要以下几步:
1.添加命名空间
需要添加System.Windows.Forms命名空间来使用NotifyIcon类。
using System.Windows.Forms;
2.创建NotifyIcon对象
在窗体类中定义一个NotifyIcon对象,用来实现窗体最小化后显示在系统托盘中。
private System.Windows.Forms.NotifyIcon notifyIcon1;
在窗体的构造函数中初始化NotifyIcon对象并添加鼠标事件处理方法。
public Form1()
{
InitializeComponent();
notifyIcon1 = new System.Windows.Forms.NotifyIcon();
notifyIcon1.BalloonTipIcon = System.Windows.Forms.ToolTipIcon.Info;
notifyIcon1.BalloonTipText = "应用已最小化到托盘";
notifyIcon1.BalloonTipTitle = "提示";
notifyIcon1.Icon = this.Icon;
notifyIcon1.Text = "应用名称";
notifyIcon1.Visible = true;
notifyIcon1.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.notifyIcon1_MouseDoubleClick);
}
在鼠标双击事件处理方法中实现最小化到托盘的功能。首先将窗体隐藏,并显示系统托盘图标上的提示信息。然后设置图标左键单击事件的处理方法并将图标显示在系统托盘中。
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
else
{
this.WindowState = FormWindowState.Minimized;
this.Hide();
notifyIcon1.ShowBalloonTip(1000);
notifyIcon1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.notifyIcon1_MouseClick);
}
}
3.设置窗体关闭事件
设置窗体关闭事件处理方法,实现在窗体关闭前隐藏到系统托盘中。在窗体关闭事件处理方法中需要将双击图标事件处理方法和左键单击图标事件处理方法从NotifyIcon对象的MouseDoubleClick和MouseClick事件中移除,否则在图标上单击会重复打开窗体。
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
notifyIcon1.Visible = false;
notifyIcon1.MouseDoubleClick -= new System.Windows.Forms.MouseEventHandler(this.notifyIcon1_MouseDoubleClick);
notifyIcon1.MouseClick -= new System.Windows.Forms.MouseEventHandler(this.notifyIcon1_MouseClick);
}
下面是完整的示例代码:
using System;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
private System.Windows.Forms.NotifyIcon notifyIcon1;
public Form1()
{
InitializeComponent();
notifyIcon1 = new System.Windows.Forms.NotifyIcon();
notifyIcon1.BalloonTipIcon = System.Windows.Forms.ToolTipIcon.Info;
notifyIcon1.BalloonTipText = "应用已最小化到托盘";
notifyIcon1.BalloonTipTitle = "提示";
notifyIcon1.Icon = this.Icon;
notifyIcon1.Text = "应用名称";
notifyIcon1.Visible = true;
notifyIcon1.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.notifyIcon1_MouseDoubleClick);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
notifyIcon1.Visible = false;
notifyIcon1.MouseDoubleClick -= new System.Windows.Forms.MouseEventHandler(this.notifyIcon1_MouseDoubleClick);
notifyIcon1.MouseClick -= new System.Windows.Forms.MouseEventHandler(this.notifyIcon1_MouseClick);
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
else
{
this.WindowState = FormWindowState.Minimized;
this.Hide();
notifyIcon1.ShowBalloonTip(1000);
notifyIcon1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.notifyIcon1_MouseClick);
}
}
private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.Show();
this.WindowState = FormWindowState.Normal;
notifyIcon1.MouseClick -= new System.Windows.Forms.MouseEventHandler(this.notifyIcon1_MouseClick);
}
}
}
}
示例1:
public Form1()
{
InitializeComponent();
notifyIcon1 = new System.Windows.Forms.NotifyIcon();
notifyIcon1.BalloonTipIcon = System.Windows.Forms.ToolTipIcon.Info;
notifyIcon1.BalloonTipText = "应用已最小化到托盘";
notifyIcon1.BalloonTipTitle = "提示";
notifyIcon1.Icon = this.Icon;
notifyIcon1.Text = "应用名称";
notifyIcon1.Visible = true;
notifyIcon1.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.notifyIcon1_MouseDoubleClick);
}
在以上示例中,创建了NotifyIcon对象,设置了提示信息、图标以及鼠标双击事件处理方法。当双击图标时,在窗体最小化到托盘前会执行Show、WindowState和Hide方法,并显示提示信息。
示例2:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
notifyIcon1.Visible = false;
notifyIcon1.MouseDoubleClick -= new System.Windows.Forms.MouseEventHandler(this.notifyIcon1_MouseDoubleClick);
notifyIcon1.MouseClick -= new System.Windows.Forms.MouseEventHandler(this.notifyIcon1_MouseClick);
}
在以上示例中,实现了窗体关闭前隐藏到托盘中的功能,在窗体关闭事件处理方法中移除了双击图标事件处理方法和左键单击图标事件处理方法。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C#怎样才能实现窗体最小化到托盘呢? - Python技术站