闽公网安备 35020302035485号




protected override void OnFormClosing(FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true; // 取消关闭窗体
this.Hide(); // 隐藏窗体
this.notifyIcon1.Visible = true; // 显示托盘图标
}
}
6.双击notifyIcon1写鼠标双击事件处理程序: private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Show(); // 显示窗体
this.WindowState = FormWindowState.Normal; // 恢复窗体正常大小
this.notifyIcon1.Visible = false; // 隐藏托盘图标
}
7.双击显示窗体按钮,写点击事件处理程序: private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Show(); // 显示窗体
this.WindowState = FormWindowState.Normal; // 恢复窗体正常大小
this.notifyIcon1.Visible = false; // 隐藏托盘图标
}
8.双击显示气泡按钮,写点击事件处理程序: private void 显示气泡2ToolStripMenuItem_Click(object sender, EventArgs e)
{
// 堆代码 duidaima.com
// 显示气泡提示,参数表示提示显示的时间(单位:毫秒)
notifyIcon1.ShowBalloonTip(3000);
}
9.双击退出按钮,写点击事件处理程序: private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit(); // 退出应用程序
}
10.查看实现效果:winform实现最小至系统托盘效果
namespace Minimized_to_the_system_tray_demo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true; // 取消关闭窗体
this.Hide(); // 隐藏窗体
this.notifyIcon1.Visible = true; // 显示托盘图标
}
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Show(); // 显示窗体
this.WindowState = FormWindowState.Normal; // 恢复窗体正常大小
this.notifyIcon1.Visible = false; // 隐藏托盘图标
}
private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Show(); // 显示窗体
this.WindowState = FormWindowState.Normal; // 恢复窗体正常大小
this.notifyIcon1.Visible = false; // 隐藏托盘图标
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit(); // 退出应用程序
}
private void 显示气泡2ToolStripMenuItem_Click(object sender, EventArgs e)
{
// 显示气泡提示,参数表示提示显示的时间(单位:毫秒)
notifyIcon1.ShowBalloonTip(3000);
}
}
}
通过代码实现private NotifyIcon notifyIcon1; private ContextMenuStrip menuStrip;2.menuStrip的相关设置:
// 创建 ContextMenuStrip。
this.menuStrip = new ContextMenuStrip();
// 创建并初始化 ToolStripMenuItem 对象。
ToolStripMenuItem item1 = new ToolStripMenuItem("显示窗体");
item1.Click += (object? sender, EventArgs e) =>
{
this.Show(); // 显示窗体
this.WindowState = FormWindowState.Normal; // 恢复窗体正常大小
this.notifyIcon1.Visible = false; // 隐藏托盘图标
};
ToolStripMenuItem item2 = new ToolStripMenuItem("显示气泡");
item2.Click += (object? sender, EventArgs e) =>
{
// 显示气泡提示,参数表示提示显示的时间(单位:毫秒)
notifyIcon1.ShowBalloonTip(3000);
};
ToolStripMenuItem item3 = new ToolStripMenuItem("退出");
item3.Click += (object? sender, EventArgs e) =>
{
Application.Exit(); // 退出应用程序
};
// 将 ToolStripMenuItem 对象添加到 ContextMenuStrip 的 Items 集合中。
this.menuStrip.Items.Add(item1);
this.menuStrip.Items.Add(item2);
this.menuStrip.Items.Add(item3);
3.notifyIcon1的相关设置: // 创建 NotifyIcon。
this.notifyIcon1 = new NotifyIcon();
// Icon 属性设置将在系统托盘中显示的图标。
notifyIcon1.Icon = new Icon("你的ico图标路径"");
// ContextMenu 属性设置当右键点击系统托盘图标时显示的菜单。
notifyIcon1.ContextMenuStrip = this.menuStrip;
// Text 属性设置当鼠标悬停在系统托盘图标上时显示的提示文本。
notifyIcon1.Text = "最小化至系统托盘示例程序";
notifyIcon1.Visible = true;
// 气泡提示相关设置
notifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
notifyIcon1.BalloonTipTitle = "提示";
notifyIcon1.BalloonTipText = "您有一条新消息";
// 注册鼠标双击事件
notifyIcon1.MouseDoubleClick += NotifyIcon1_MouseDoubleClick;
4.notifyIcon1鼠标双击事件处理程序: private void NotifyIcon1_MouseDoubleClick(object? sender, MouseEventArgs e)
{
this.Show(); // 显示窗体
this.WindowState = FormWindowState.Normal; // 恢复窗体正常大小
this.notifyIcon1.Visible = false; // 隐藏托盘图标
}
5.重写窗体关闭事件处理程序:protected override void OnFormClosing(FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true; // 取消关闭窗体
this.Hide(); // 隐藏窗体
this.notifyIcon1.Visible = true; // 显示托盘图标
}
}
实现效果与上述相同。using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Minimized_to_the_system_tray_demo
{
public partial class Form2 : Form
{
private NotifyIcon notifyIcon1;
private ContextMenuStrip menuStrip;
public Form2()
{
InitializeComponent();
// 创建 NotifyIcon。
this.notifyIcon1 = new NotifyIcon();
// 创建 ContextMenuStrip。
this.menuStrip = new ContextMenuStrip();
// 创建并初始化 ToolStripMenuItem 对象。
ToolStripMenuItem item1 = new ToolStripMenuItem("显示窗体");
item1.Click += (object? sender, EventArgs e) =>
{
this.Show(); // 显示窗体
this.WindowState = FormWindowState.Normal; // 恢复窗体正常大小
this.notifyIcon1.Visible = false; // 隐藏托盘图标
};
ToolStripMenuItem item2 = new ToolStripMenuItem("显示气泡");
item2.Click += (object? sender, EventArgs e) =>
{
// 显示气泡提示,参数表示提示显示的时间(单位:毫秒)
notifyIcon1.ShowBalloonTip(3000);
};
ToolStripMenuItem item3 = new ToolStripMenuItem("退出");
item3.Click += (object? sender, EventArgs e) =>
{
Application.Exit(); // 退出应用程序
};
// 将 ToolStripMenuItem 对象添加到 ContextMenuStrip 的 Items 集合中。
this.menuStrip.Items.Add(item1);
this.menuStrip.Items.Add(item2);
this.menuStrip.Items.Add(item3);
// Icon 属性设置将在系统托盘中显示的图标。
notifyIcon1.Icon = new Icon("你的ico图标路径");
// ContextMenu 属性设置当右键点击系统托盘图标时显示的菜单。
notifyIcon1.ContextMenuStrip = this.menuStrip;
// Text 属性设置当鼠标悬停在系统托盘图标上时显示的提示文本。
notifyIcon1.Text = "最小化至系统托盘示例程序";
notifyIcon1.Visible = true;
notifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
notifyIcon1.BalloonTipTitle = "提示";
notifyIcon1.BalloonTipText = "您有一条新消息";
notifyIcon1.MouseDoubleClick += NotifyIcon1_MouseDoubleClick;
}
private void NotifyIcon1_MouseDoubleClick(object? sender, MouseEventArgs e)
{
this.Show(); // 显示窗体
this.WindowState = FormWindowState.Normal; // 恢复窗体正常大小
this.notifyIcon1.Visible = false; // 隐藏托盘图标
}
private void Form2_Load(object sender, EventArgs e)
{
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true; // 取消关闭窗体
this.Hide(); // 隐藏窗体
this.notifyIcon1.Visible = true; // 显示托盘图标
}
}
}
}