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

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();
}

时间: 2024-09-14 10:48:16

C# WinForm窗口最小化到系统托盘_C#教程的相关文章

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

  C#编写最小化时隐藏为任务栏图标的 Window appllication.   1.设置WinForm窗体属性showinTask=false 2.加notifyicon控件notifyIcon1,为控件notifyIcon1的属性Icon添加一个icon图标. 3.添加窗体最小化事件(首先需要添加事件引用): this.SizeChanged += new System.EventHandler(this.Form1_SizeChanged); //上面一行是主窗体InitializeC

WinForm实现最小化到系统托盘方法实例详解

  本文实例讲述了WinForm实现最小化到系统托盘方法.分享给大家供大家参考.具体分析如下: 有个叫NotifyIcon的控件 1.建个WinForm项目,其它操作略过. 2.拉个NotifyIcon控件,将属性Visable设置成False,在Text属性上随便填些文件. 3.实现Form的SizeChanged事件,代码如下: ? 1 2 3 4 5 if(this.WindowState == FormWindowState.Minimized) //判断是否最小化 { this.Sho

VC 制作系统托盘程序实现将窗口最小化到系统托盘

VC 制作系统托盘程序实现将窗口最小化到系统托盘 2008年01月11日 星期五 10:24 前段时间因为要用到系统拖盘,所以研究了一下,在这里记录一下,免得以后忘了 ^_^先在类中定义一个结构变量NOTIFYICONDATA pnid;然后在OnInitDialog进行初始化    pnid.cbSize=(DWORD)sizeof(NOTIFYICONDATA);//该结构体大小    pnid.hWnd=m_hWnd;    //窗口句柄    pnid.uID=(UINT)m_hIcon

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

关键字:C# 最小化 托盘原文:http://www.cnblogs.com/txw1958/archive/2012/12/17/csharp-minimize-tray.html 先添加notifyicon控件notifyIcon1   using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using Syste

AllTray 0.7.5dev发布 最小化到系统托盘工具

AllTray 0.7.5dev该版本Close-to-tray支持,http://www.aliyun.com/zixun/aggregation/32499.html">未成年人保护和文档更新.注意,要使用close-to-tray支持,您必须指定-c或--enable-ctt标记命令行启用CTT;有关详细信息,请参阅手册页. AllTray 是一个很有用的小程序,使用它你可以将程序的窗口最小化到系统托盘,从而腾出桌面空间以作它用.此程序主要为那些没有原生提供最小化到系统托盘功能的程序

[C#]WinFrom中实现最小化至系统托盘

刚在写个程序,遇到个小问题,就是当点击最小化按钮时,如何把程序最小化到系统托盘里去.其实这样的例子在很多软件上面都可以见到的,但刚开始找属性时并没有任何和系统托盘有关的,找了一下事件,也没有发现有关最小化按钮点击的事件.郁闷中,于是上网搜索了一下,才发现原来有个叫NotifyIcon的控件(汗一个,再BS自己一个,以前怎么没发现).而最小化的事件也可以换种思路去实现(用SizeChanged事件). 下面是实现当点击最小化按钮时,程序缩到系统托盘中,双击系统托盘图标时,还原程序. 1.建个Win

C#编写最小化到系统托盘的Windows应用程序

window|程序 事情是这样,为了自己使用方便,就顺手写了一个看网络电视的程序.程序最小化到系统托盘中,这样在看网络电视的时候,就可以随时打开,随时切换,比较方便,呵呵. 以前在Visual C++编程环境里,编写一个这样的系统托盘程序,应该说比较复杂,还要自己添加消息处理函数.而在C#中,这一切就变得非常的容易了.下面是简单步骤. 为程序添加两个主要控件,NotifyIcon控件和ContextMenu控件: 为ContextMenu设置Menu菜单和相应菜单的Click事件: 为控件Not

C# winform程序怎样将程序最小化到系统托盘?

这样最简单! 1.在form中添加一个NotifyIcon控件 2.把icon.ico这个图标放在/bin/Debug目录下 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace MinimizeForm

c# 重载WndProc,实现重写“最小化”的实现方法_C#教程

code #1 复制代码 代码如下: private void Form1_SizeChanged(object sender, EventArgs e) //最小化隐藏窗体 { if (this.WindowState == FormWindowState.Minimized)//窗体状态为最小化 { StopRectTimer.Enabled = false; this.Visible = false; this.notifyIcon1.Visible = true; //显示系统托盘图标