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

简介:
 

C#编写最小化时隐藏为任务栏图标的 Window appllication.  

1.设置WinForm窗体属性showinTask=false

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

3.添加窗体最小化事件(首先需要添加事件引用):

this.SizeChanged += new System.EventHandler(this.Form1_SizeChanged);

//上面一行是主窗体InitializeComponent()方法中需要添加的引用

private void Form1_SizeChanged(object sender, EventArgs e)
{
    if(this.WindowState == FormWindowState.Minimized)
    {
       this.Hide();
       this.notifyIcon1.Visible=true;
    }
}

4.添加点击图标事件(首先需要添加事件引用):

private void notifyIcon1_Click(object sender, EventArgs e)
{
    this.Visible = true;
    this.WindowState = FormWindowState.Normal;
    this.notifyIcon1.Visible = false;
}


5.可以给notifyIcon添加右键菜单:

主窗体中拖入一个ContextMenu控件NicontextMenu,点中控件,在上下文菜单中添加菜单,notifyIcon1的ContextMenu行为中选中NicontextMenu 作为上下文菜单。

this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
this.NicontextMenu = new System.Windows.Forms.ContextMenu();
this.menuItem_Hide = new System.Windows.Forms.MenuItem();
this.menuItem_Show = new System.Windows.Forms.MenuItem();
this.menuItem_Aubot = new System.Windows.Forms.MenuItem();
this.menuItem_Exit = new System.Windows.Forms.MenuItem();  


this.notifyIcon1.ContextMenu = this.NicontextMenu;
this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject( "NotifyIcon.Icon ")));
this.notifyIcon1.Text = " ";
this.notifyIcon1.Visible = true;
this.notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick);
this.notifyIcon1.Click += new System.EventHandler(this.notifyIcon1_Click);


this.NicontextMenu.MenuItems.AddRange(

    new System.Windows.Forms.MenuItem[]
    {
          this.menuItem_Hide,
          this.menuItem_Show,
          this.menuItem_Aubot,
          this.menuItem_Exit
    }
);

//    
//       menuItem_Hide
//    
this.menuItem_Hide.Index = 0;
this.menuItem_Hide.Text = "隐藏 ";
this.menuItem_Hide.Click += new System.EventHandler(this.menuItem_Hide_Click);
//    
//       menuItem_Show
//    
this.menuItem_Show.Index = 1;
this.menuItem_Show.Text = "显示 ";
this.menuItem_Show.Click += new System.EventHandler(this.menuItem_Show_Click);
//    
//       menuItem_Aubot
//    
this.menuItem_Aubot.Index = 2;
this.menuItem_Aubot.Text = "关于 ";
this.menuItem_Aubot.Click += new System.EventHandler(this.menuItem_Aubot_Click);
//    
//       menuItem_Exit
//    
this.menuItem_Exit.Index = 3;
this.menuItem_Exit.Text = "退出 ";
this.menuItem_Exit.Click += new System.EventHandler(this.menuItem_Exit_Click);

protected  override  void  OnClosing(CancelEventArgs e)
{
   this.ShowInTaskbar = false;
   this.WindowState = FormWindowState.Minimized;
   e.Cancel = true;    
}
protected  override  void  OnClosing(CancelEventArgs e)
{
   //this.ShowInTaskbar = false;
   this.WindowState = FormWindowState.Minimized;
   e.Cancel = true;    
}

private  void  CloseCtiServer()
{
   timer.Enabled = false;
   DJ160API.DisableCard();
   this.NotifyIcon.Visible = false;
   this.Close();
   this.Dispose();
   Application.Exit();
}

private  void  HideCtiServer()
{
   this.Hide();
}

private  void  ShowCtiServer()
{
   this.Show();
   this.WindowState = FormWindowState.Normal;
   this.Activate();

}
private  void  CtiManiForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    this.CloseCtiServer();
}

private  void  menuItem_Show_Click(object sender, System.EventArgs e)
{
    this.ShowCtiServer();
}

private  void  menuItem_Aubot_Click(object sender, System.EventArgs e)
{

}

private  void  menuItem_Exit_Click(object sender, System.EventArgs e)
{
    this.CloseCtiServer();
}

private  void  menuItem_Hide_Click(object sender, System.EventArgs e)
{
    this.HideCtiServer();
}

private  void  CtiManiForm_SizeChanged(object sender, System.EventArgs e)
{
   if(this.WindowState == FormWindowState.Minimized)
   {
      this.HideCtiServer();
   }
}

private  void  notifyIcon1_DoubleClick(object sender,System.EventArgs e)
{                                                                
    this.ShowCtiServer();
}

目录
相关文章
|
4月前
|
安全 开发工具
VBA窗体最大化最小化按钮实现
VBA窗体最大化最小化按钮实现
149 0
|
API C# Windows
Winform控件优化之无边框窗体及其拖动、调整大小和实现最大最小化关闭功能的自定义标题栏效果
Winform中实现无边框窗体只需要设置FormBorderStyle = FormBorderStyle.None,但是无边框下我们需要保留移动窗体、拖拽调整大小、自定义美观好看的标题栏等...
2715 0
Winform控件优化之无边框窗体及其拖动、调整大小和实现最大最小化关闭功能的自定义标题栏效果
|
21天前
|
C#
C#如何实现窗体最小化到托盘
C#如何实现窗体最小化到托盘
17 0
|
2月前
|
C# 开发者
35.c#:winform窗口
35.c#:winform窗口
13 1
|
C# Windows
C#实现无标题栏窗体点击任务栏图标正常最小化或还原的解决方法
对于无标题栏窗体,也就是FormBorderStyle等于System.Windows.Forms.FormBorderStyle.None的窗体,点击任务栏图标的时候,是不能象标准窗体那样最小化或还原的。
1163 0
C++ 让窗体最大最小化
C++ 让窗体最大最小化
200 0
QT隐藏窗口标题栏、最大化、最小化、关闭按钮
QT隐藏窗口标题栏、最大化、最小化、关闭按钮
966 0
|
C#
WPF程序最小化到任务通知栏
原文:WPF程序最小化到任务通知栏 我们通常使用的桌面软件,都可以最小化到任务通知栏,并且可以从任务通知栏再打开当前软件,或者通过软件的快捷方式从任务通知栏呼出。 我们可以通过下面的方式把WPF程序最小化到任务栏。
1890 0
|
C#
WPF自定义窗口最大化显示任务栏
原文:WPF自定义窗口最大化显示任务栏 当我们要自定义WPF窗口样式时,通常是采用设计窗口的属性 WindowStyle="None" ,然后为窗口自定义放大,缩小,关闭按钮的样式。 然而这样的话,当通过代码设置窗口(代码如下)放大时,窗口会把任务栏给遮档住。
1193 0