您的位置:首页 > 编程语言

Windows窗体编程基础学习:使用 NotifyIcon 组件向任务栏添加应用程序图标

2010-12-31 14:01 801 查看
1.在Windows项目中新加一个窗体myNotifyIcon

2.在设计视图 通过工具箱
向该Form加入NotifyIcon 和 ContextMenuStrip
并查看确保窗体myNotifyIcon的ShowIcon属性设置为True

3.通过notifyIcon1的属性对话框 设置相关内容
属性部分设置
BalloonTipIcon:
BalloonTipText:程序在后台运行
BalloonTipTitle:提示
ContextMenuStrip:contextMenuStrip1
Icon:
Text:这是程序的图标
Visible:True

事件部分
双击MouseDoubleClick
其示例代码如下
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Show();
}

4.通过contextMenuStrip1的属性对话框 设置相关内容
编辑其Items
加入如下菜单项
myNotifyIconOpen(最大化)
其单击事件的示例代码如下
private void toolStripMenuItemOpen_Click(object sender, EventArgs e)
{
this.Show();
}
myNotifyIconHide(隐藏)
其单击事件的示例代码如下
private void toolStripMenuItemHide_Click(object sender, EventArgs e)
{
//隐藏窗体
this.Hide();
//弹出气球显示
this.notifyIcon1.ShowBalloonTip(30);
}
myNotifyIconExit(退出)
其单击事件的示例代码如下
private void toolStripMenuItemExit_Click(object sender, EventArgs e)
{
this.strCloseReason = "EXIT";
this.Close();
}

5.在窗体myNotifyIcon属性对话框的事件栏
选择并双击FormClosing
其示例代码如下
private void myNotifyIcon_FormClosing(object sender, FormClosingEventArgs e)
{
//strCloseReason是一个全局的私有变量
//加上这个判断 为的是
//点击窗体上的关闭按钮时 起隐藏窗体作用
//点击notifyIcon1的菜单上的退出菜单项时 才关闭窗体
if (string.IsNullOrEmpty(strCloseReason))
{
this.Hide();
e.Cancel = true;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: