1、首先在工具栏中选择NotifyIcon控件拖入窗体中;

WinForm实现最小化窗体时隐藏到系统托盘中-LMLPHP

2、设置NotifyIcon控件的相关属性;

WinForm实现最小化窗体时隐藏到系统托盘中-LMLPHP

Icon:在系统托盘中显示的图标;

Text:当鼠标移动到系统托盘图标上时显示的文本;

ContextMenuStrip:设置系统托盘的右键菜单,这个实现很简单,拖入一个右键菜单的控件,并在下拉列表框中选择就可以了。

3、添加相关代码

/// <summary>
/// 窗体SizeChanged事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FrmMain_SizeChanged(object sender, EventArgs e)
{
  //当窗体最小化时,隐藏到系统托盘中
  if (WindowState == FormWindowState.Minimized)
  {
    this.Hide();
    this.notifyIcon1.Visible = true;
  }
} /// <summary>
/// 单击系统托盘图标显示窗体
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void notifyIcon1_Click(object sender, EventArgs e)
{
  this.Visible = true;
  this.WindowState = FormWindowState.Normal;
}

  

04-03 03:28