转自:http://www.cnblogs.com/ensure125/archive/2007/04/12/711024.html
采用NotifyIcon控件、记得要将窗体showintaskbar=true/false,它主要用来控制是否在任务栏显示。记得要给icon设置图片。
代码如下:
1
//
窗体最小化事件
2
private
void
pbMinisize_Click(
object
sender, System.EventArgs e)
3
{
4
//
Set the WindowState to normal if the form is minimized.
5
WindowState
=
FormWindowState.Minimized;
6
//
指示是否在Windows任务栏中显示窗体
7
this
.ShowInTaskbar
=
false
;
8
notifyIcon1.Visible
=
true
;
9
}
10
#endregion
11
12
//
任务栏区域的双击事件
13
private
void
notifyIcon1_DoubleClick(
object
Sender, EventArgs e)
14
{
15
//
Show the form when the user double clicks on the notify icon.
16
if
(
this
.WindowState
==
FormWindowState.Minimized)
17
this
.WindowState
=
FormWindowState.Normal;
18
this
.Activate();
19
this
.ShowInTaskbar
=
true
;
20
this
.notifyIcon1.Visible
=
false
;
21
}
用menuItem来设置任务栏区域的click菜单。
//
退出
private
void
menuItem1_Click(
object
Sender, EventArgs e)
{
this
.Close();
}
//
打开
private
void
menuItem2_Click(
object
sender, System.EventArgs e)
{
this
.WindowState
=
FormWindowState.Normal;
this
.Activate();
this
.ShowInTaskbar
=
true
;
this
.notifyIcon1.Visible
=
false
;
}
C#编写最小化时隐藏为任务栏图标的Window appllication
转自:
http://lybabiandbel.cnblogs.com/archive/2006/01/09/313886.html
1.设置窗体属性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控件contextMenu1,点中控件,在上下文菜单中添加菜单,notifyIcon1的ContextMenu行为中选中contextMenu1作为上下文菜单。
(可以在子菜单中添加行为)