网站建议:179001057@qq.com

C# WinForm窗口最小化到系统托盘

技术2022-05-11  3

1.加notifyicon控件notifyIcon1,为控件notifyIcon1的属性Icon添加一个icon图标。

this.ShowInTaskbar = false//不在任务栏中显示图标

2.加载form的SizeChanged事件.

3.加载notifyicon的单击事件:

 view plaincopy to clipboardprint?#region 系统托盘          /// <summary>          /// 单击任务栏图标显示或隐藏主面板          /// </summary>          /// <param name="sender">The source of the event.</param>          /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>          private void notifyIcon_Click(object sender, EventArgs e)          {              //已显示则让它显示在最上面              if (this.Visible)                  this.TopMost = true;              else                 this.Visible = true;              this.WindowState = FormWindowState.Normal;              this.TopMost = false;          }            /// <summary>          /// Handles the FormClosing event of the frmMain control.          /// </summary>          /// <param name="sender">The source of the event.</param>          /// <param name="e">The <see cref="System.Windows.Forms.FormClosingEventArgs"/> instance containing the event data.</param>          private void frmMain_FormClosing(object sender, FormClosingEventArgs e)          {              //不是退出程序则到系统托盘              if (!isExited)              {                  e.Cancel = true;                  this.Visible = false;              }          }            /// <summary>          /// 点击最小化时到系统托盘          /// </summary>          /// <param name="sender">The source of the event.</param>          /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>          private void frmMain_SizeChanged(object sender, EventArgs e)          {              if (this.WindowState == FormWindowState.Minimized)              {                  this.Visible = false;              }          }         #endregion   #region 系统托盘        /// <summary>        /// 单击任务栏图标显示或隐藏主面板        /// </summary>        /// <param name="sender">The source of the event.</param>        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>        private void notifyIcon_Click(object sender, EventArgs e)        {            //已显示则让它显示在最上面            if (this.Visible)                this.TopMost = true;            else                this.Visible = true;            this.WindowState = FormWindowState.Normal;            this.TopMost = false;        }

        /// <summary>        /// Handles the FormClosing event of the frmMain control.        /// </summary>        /// <param name="sender">The source of the event.</param>        /// <param name="e">The <see cref="System.Windows.Forms.FormClosingEventArgs"/> instance containing the event data.</param>        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)        {            //不是退出程序则到系统托盘            if (!isExited)            {                e.Cancel = true;                this.Visible = false;            }        }

        /// <summary>        /// 点击最小化时到系统托盘        /// </summary>        /// <param name="sender">The source of the event.</param>        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>        private void frmMain_SizeChanged(object sender, EventArgs e)        {            if (this.WindowState == FormWindowState.Minimized)            {                this.Visible = false;            }        }        #endregion

 测试时发现上面的代码不稳定,达不到以下效果:

1.如果窗体没有打开,双击则打开显示在最前面.

2.如果窗体已经打开,但其它软件显示在最前面的时候,双击显示在最前面.

3.如果窗体已经显示在最前面,用鼠标单击此软件之外的region,则让其它窗体显示在最前面.

把代码时进行修改后如下

view plaincopy to clipboardprint?#region 系统托盘           /// <summary>           /// 单击任务栏图标显示或隐藏主面板           /// </summary>           private void notifyIcon_DoubleClick(object sender, EventArgs e)           {               //鼠标左键才有效               if (((MouseEventArgs)e).Button != MouseButtons.Left)                   return;                 if (!this.Visible)               {                   this.Visible = true;               }               if (this.WindowState == FormWindowState.Minimized)               {                   this.WindowState = FormWindowState.Normal;               }               this.Activate();//激活窗体           }             /// <summary>           /// 判断是否关闭程序           /// </summary>           private void frmMain_FormClosing(object sender, FormClosingEventArgs e)           {               //不是退出程序则到系统托盘               if (!isExited)               {                   e.Cancel = true;                   this.Visible = false;               }           }             /// <summary>           /// 点击最小化时到系统托盘           /// </summary>           private void frmMain_SizeChanged(object sender, EventArgs e)           {               if (this.WindowState == FormWindowState.Minimized)               {                   this.Visible = false;               }           }          #endregion 

 

本文来自博客,转载请标明出处:http://blog.csdn.net/gh_li/archive/2009/01/21/3846982.aspx


最新回复(0)