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

 更新时间:2008年11月26日 12:40:50   作者:  
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();
}

相关文章

  • c#委托与事件(详解)

    c#委托与事件(详解)

    本文中,我将通过两个范例由浅入深地讲述什么是委托、为什么要使用委托、事件的由来、.Net Framework中的委托和事件、委托和事件对Observer设计模式的意义,对它们的中间代码也做了讨论
    2021-07-07
  • C#项目中跨文件调用公共类的实例方法

    C#项目中跨文件调用公共类的实例方法

    在本篇文章里小编给大家整理的是关于C#项目中如何跨文件调用公共类的知识点内容,需要的朋友们学习下。
    2019-08-08
  • C#实现同步模式下的端口映射程序

    C#实现同步模式下的端口映射程序

    这篇文章介绍了C#实现同步模式下的端口映射程序,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-06-06
  • WPF运行时替换方法实现mvvm自动触发刷新

    WPF运行时替换方法实现mvvm自动触发刷新

    这篇文章主要为大家详细介绍了WPF运行时如何实现setter不需要调方法就可以自动触发界面刷新,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-04-04
  • C#连接SQL Sever数据库详细图文教程

    C#连接SQL Sever数据库详细图文教程

    C#是Microsoft公司为.NET Framework推出的重量级语言,和它搭配最完美的数据库无疑就是Microsoft SQL Server了,下面这篇文章主要给大家介绍了关于C#连接SQL Sever数据库的详细图文教程,需要的朋友可以参考下
    2023-06-06
  • C#实现DataTable,List和Json转换的方法

    C#实现DataTable,List和Json转换的方法

    这篇文章主要介绍了C#实现DataTable,List和Json转换的方法,结合实例形式分析了DataTable、list、DataReader、DataSet等转换成JSON的相关实现技巧,需要的朋友可以参考下
    2016-08-08
  • C#多线程系列之多阶段并行线程

    C#多线程系列之多阶段并行线程

    本文详细讲解了C#多线程的多阶段并行线程,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-02-02
  • 使用C#实现Windows组和用户管理的示例代码

    使用C#实现Windows组和用户管理的示例代码

    这篇文章主要介绍了使用C#实现Windows组和用户管理的示例代码,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下
    2021-01-01
  • 浅析WPF中ToolTip工具提示的应用

    浅析WPF中ToolTip工具提示的应用

    在日常应用中,当鼠标放置在某些控件上时,都会有相应的信息提示,从软件易用性上来说,这是一个非常友好的功能设计,本文就来和大家简单聊聊WPF中ToolTip工具提示的相关使用,有兴趣的可以了解下
    2023-12-12
  • Unity编辑器资源导入处理函数OnPreprocessAudio用法示例

    Unity编辑器资源导入处理函数OnPreprocessAudio用法示例

    这篇文章主要为大家介绍了Unity编辑器资源导入处理函数OnPreprocessAudio用法示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-08-08

最新评论