C#最小化到系统托盘实现

目标:程序点击关闭后,弹出一个对话框,选择退出,最小化,或者取消.然后系统托盘双击,可以打开最小化的程序,右键有菜单,菜单里有个"选项",可以设置每次点击关闭按钮时是否弹出这个对话框.

难点:增加一个"下次不再提示"的comboBox到对话框中.

使用到的知识:

1. 非模态对话框: CloseDialog.Show();

    模态对话框:  CloseDialog.ShowDialog();

2. 禁止拉伸对话框:  this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;

3. 最小化到托盘的实现:

      3.1.加notifyicon控件notifyIcon,为控件notifyIcon的属性Icon添加一个icon图标

      3.2.添加事件:

 privatevoid QPan_SizeChanged(object sender, System.EventArgs e)

{

      if(this.WindowState == System.Windows.Forms.FormWindowState.Minimized)

      {

            this.QPan_MiniMizedToTuoPan();

      }

 

}

//最小化到托盘,函数

publicvoid QPan_MiniMizedToTuoPan()

{

      this.Hide();

      this.ShowInTaskbar = false;

      this.notifyIcon.Visible = true;

}

4.托盘双击:

 //双击托盘图标

privatevoid notifyIcon_DoubleClick(object sender, System.EventArgs e)

{

      this.QPan_OpenFromTuoPan();

}

 

//从托盘返回,函数

Publicvoid QPan_OpenFromTuoPan()

{

      this.Visible = true;

      this.Show();

      this.ShowInTaskbar = true;

      this.WindowState = System.Windows.Forms.FormWindowState.Normal;

      this.notifyIcon.Visible = true;

}

5.右键菜单:

5.1.增加一个ContextmenuStrip

5.2.增加菜单项:

 // TuoPanContextMenuStrip

//

this.TuoPanContextMenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {

this.TrayOptions,

this.TraySeparator1,

this.TrayOpen,

this.TrayExit});

this.TuoPanContextMenuStrip.Name = "TuoPancontextMenuStrip";

this.TuoPanContextMenuStrip.Size = new System.Drawing.Size(112, 76);

//

// TrayOptions

//

this.TrayOptions.Name = "TrayOptions";

this.TrayOptions.Size = new System.Drawing.Size(111, 22);

this.TrayOptions.Text = "Options";

this.TrayOptions.Click += new System.EventHandler(this.OpenSettingOptions);

//

// TraySeparator1

//

this.TraySeparator1.Name = "TraySeparator1";

this.TraySeparator1.Size = new System.Drawing.Size(108, 6);

//

// TrayOpen

//

this.TrayOpen.Name = "TrayOpen";

this.TrayOpen.Size = new System.Drawing.Size(111, 22);

this.TrayOpen.Text = "Open";

this.TrayOpen.Click += new System.EventHandler(this.OpenQPan);

//

// TrayExit

//

this.TrayExit.Name = "TrayExit";

this.TrayExit.Size = new System.Drawing.Size(111, 22);

this.TrayExit.Text = "Exit";

this.TrayExit.Click += new System.EventHandler(this.CloseQPan);

//

5.3.给菜单项增加事件:

//退出

privatevoid CloseQPan(object sender, System.EventArgs e)

{

boolCloseFromExitMenu = true;

Application.Exit();

}

//打开

privatevoid OpenQPan(object sender, System.EventArgs e)

{

      this.QPan_OpenFromTuoPan();

}

//打开选项设置

privatevoid OpenSettingOptions(object sender, System.EventArgs e)

{

      this.NewOptionDialog.ShowDialog();

}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C# 中,你可以使用 NotifyIcon 类来实现程序最小化系统托盘的功能。下面是一个简单的示例代码: 首先,在你的窗体类中声明一个 NotifyIcon 对象和一个 ContextMenu 对象: ```csharp private NotifyIcon notifyIcon; private ContextMenu contextMenu; ``` 然后,在窗体的构造函数中初始化这两个对象,并设置相应的属性和事件: ```csharp public Form1() { InitializeComponent(); // 初始化 NotifyIcon notifyIcon = new NotifyIcon(); notifyIcon.Icon = new Icon("icon.ico"); // 设置托盘图标 notifyIcon.Text = "My App"; // 设置鼠标悬停时显示的文本 // 初始化 ContextMenu contextMenu = new ContextMenu(); contextMenu.MenuItems.Add("打开", OnOpen); // 添加打开菜单项的点击事件 contextMenu.MenuItems.Add("退出", OnExit); // 添加退出菜单项的点击事件 // 将 ContextMenu 关联到 NotifyIcon notifyIcon.ContextMenu = contextMenu; // 设置双击托盘图标时的事件 notifyIcon.DoubleClick += OnOpen; // 显示托盘图标 notifyIcon.Visible = true; } ``` 接下来,实现打开和退出菜单项的点击事件: ```csharp private void OnOpen(object sender, EventArgs e) { // 还原窗体 this.WindowState = FormWindowState.Normal; this.Activate(); } private void OnExit(object sender, EventArgs e) { // 关闭程序 this.Close(); } ``` 最后,在窗体的 Resize 事件中处理窗体最小化时的逻辑: ```csharp private void Form1_Resize(object sender, EventArgs e) { // 判断窗体是否最小化 if (this.WindowState == FormWindowState.Minimized) { // 隐藏窗体 this.Hide(); } } ``` 这样,当你的窗体最小化时,程序就会隐藏到系统托盘中。你可以通过点击托盘图标或右键菜单来打开窗体或退出程序。记得在窗体关闭时,调用 `notifyIcon.Dispose()` 来释放资源。 希望这个示例对你有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值