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.添加事件:

 private void QPan_SizeChanged(object sender, System.EventArgs e)

{

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

      {

            this.QPan_MiniMizedToTuoPan();

      }

 

}

//最小化到托盘,函数

public void QPan_MiniMizedToTuoPan()

{

      this.Hide();

      this.ShowInTaskbar = false;

      this.notifyIcon.Visible = true;

}

4.托盘双击:

 //双击托盘图标

private void notifyIcon_DoubleClick(object sender, System.EventArgs e)

{

      this.QPan_OpenFromTuoPan();

}

 

//从托盘返回,函数

Public void 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.给菜单项增加事件:

//退出

private void CloseQPan(object sender, System.EventArgs e)

{

boolCloseFromExitMenu = true;

Application.Exit();

}

//打开

private void OpenQPan(object sender, System.EventArgs e)

{

      this.QPan_OpenFromTuoPan();

}

//打开选项设置

private void OpenSettingOptions(object sender, System.EventArgs e)

{

      this.NewOptionDialog.ShowDialog();

}

 

           

 

转载于:https://www.cnblogs.com/linglizeng/archive/2009/06/19/SystemTrayProgram.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值