[导入]C#实现WinForm窗口最小化到系统托盘

摘要:
C#编写窗口应用程序,最小化时隐藏为任务栏图标。1.设置WinForm表单属性showinTask=false。2.添加notifyincon控件notifyincon1,并为notifyinicon1属性图标添加图标。3.添加表单最小化事件(您需要首先添加事件引用):this。SizeChanged+=新系统。事件处理程序(this.Form

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#实现WinForm窗口最小化到系统托盘第1张

钢钢 2007-12-19 16:36 发表评论
[小组]  [博问]  [闪存]

文章来源:http://www.cnblogs.com/xugang/archive/2007/12/19/1006005.html

免责声明:文章转载自《[导入]C#实现WinForm窗口最小化到系统托盘》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇cnpm安装Unity使用外部版本控制SVN下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

ViewPager+TabLayout+Fragment刷新Fragment中的数据

1.ViewPager与TabLayout的初始化: 1 private void initData() { 2 mTabFragmentAdapter = new TabFragmentFirstAdapter(getSupportFragmentManager(), mDeptType); 3 mVp.setAdapte...

ES6中的函数、对象定义

ES6之前的函数定义: 通过声明通过关键词 function 定义 ,这是最早也是最经典的function定义方式。function hello (firstname) {return `Hello ${firstname}`;} 这个函数有一个名称 "hello", 所以我们称这个函数为命名函数。 通过表达式这是比较现代化的定义方式了。这种定义方式也说明...

Winform开发框架的重要特性总结

Winform开发框架的重要特性总结 从事Winform开发框架的研究和推广,也做了有几个年头了,从最初的项目雏形到目前各种重要特性的加入完善,是经过了很多项目的总结归纳和升华,有些则是根据客户需要或者应用前景的需要进行的完善,整个Winform开发框架具有很好的通用性和借鉴性,本文从该Winform开发框架进行概括总结,力求把各个重要的特性进行一些详细的...

WinForm实现鼠标悬停显示控件

在某些需频繁操作的环节中,为提高效率、减少劳动,可以在操作细节上下功夫,以带来更好的用户体验。 今天介绍的鼠标悬停显示功能就是一例,看似仅仅节省了一次点击,实则在繁复操作中能够为用户节约很多体力,提高舒适度。 本例中的基本需求是: 鼠标移入设置区域后,直接弹出详尽设置内容,并当进行了设置或鼠标移出弹出的设置区域后,自动关闭弹出的设置区域。 在这里,我设计...

C# WinForm下DataGridView绘制背景图

昨天一个朋友突然问我如何在C#下给DataGridView绘制背景图,以前使用一些第三方控件时,看见它们有这个功能,只是我还没有过这种需求,于是便动手试了下。 最先想到的是BackgroundImage,这两天正在做B/S的界面,还觉得要说做界面方便,还得说CSS,从这点上来说,WPF或者Silverlight还真不赖,只可惜的是现在C/S的用武之地越来越...

c# Winform GridControl 给列自动生成快捷操作按钮

话不多说直接上代码: using DevExpress.XtraEditors.Controls; using DevExpress.XtraEditors.Repository; using DevExpress.XtraGrid; using DevExpress.XtraGrid.Columns; using DevExpress.XtraGrid....