托盘图标的制作,点击与触发
2015-12-17 12:29
357 查看
//using System.Windows.Forms;
//using System.Drawing;
#region 托盘图标显示
private System.Windows.Forms.NotifyIcon notifyIcon = null;
/// <summary>
/// /托盘图标显示
/// </summary>
private void InitialTray()
{
//设置托盘的各个属性
notifyIcon = new System.Windows.Forms.NotifyIcon();
notifyIcon.Icon = new System.Drawing.Icon(System.Windows.Forms.Application.StartupPath + @"\Resources\TrayIcon.ico");
notifyIcon.Visible = true;
notifyIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(notifyIcon_MouseClick);
//退出菜单项
System.Windows.Forms.MenuItem exit = new System.Windows.Forms.MenuItem("exit");
exit.Click += new EventHandler(exit_Click);
//关联托盘控件
//System.Windows.Forms.MenuItem[] childen = new System.Windows.Forms.MenuItem[] { exit };
//notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(childen);
//窗体状态改变时候触发
this.StateChanged += new EventHandler(SysTray_StateChanged);
}
/// <summary>
/// 窗体状态改变时候触发
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SysTray_StateChanged(object sender, EventArgs e)
{
if (this.WindowState == WindowState.Minimized)
{
this.Visibility = Visibility.Hidden;
}
}
/// <summary>
/// 鼠标单击
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void notifyIcon_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
if (this.Visibility == Visibility.Visible)
{
this.Visibility = Visibility.Hidden;
}
else
{
this.Visibility = Visibility.Visible;
this.Activate();
}
}
}
/// <summary>
/// 退出选项
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void exit_Click(object sender, EventArgs e)
{
if (System.Windows.MessageBox.Show("确定要关闭吗?",
"退出",
MessageBoxButton.YesNo,
MessageBoxImage.Question,
MessageBoxResult.No) == MessageBoxResult.Yes)
{
notifyIcon.Dispose();
System.Windows.Application.Current.Shutdown();
}
}
#endregion
//using System.Drawing;
#region 托盘图标显示
private System.Windows.Forms.NotifyIcon notifyIcon = null;
/// <summary>
/// /托盘图标显示
/// </summary>
private void InitialTray()
{
//设置托盘的各个属性
notifyIcon = new System.Windows.Forms.NotifyIcon();
notifyIcon.Icon = new System.Drawing.Icon(System.Windows.Forms.Application.StartupPath + @"\Resources\TrayIcon.ico");
notifyIcon.Visible = true;
notifyIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(notifyIcon_MouseClick);
//退出菜单项
System.Windows.Forms.MenuItem exit = new System.Windows.Forms.MenuItem("exit");
exit.Click += new EventHandler(exit_Click);
//关联托盘控件
//System.Windows.Forms.MenuItem[] childen = new System.Windows.Forms.MenuItem[] { exit };
//notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(childen);
//窗体状态改变时候触发
this.StateChanged += new EventHandler(SysTray_StateChanged);
}
/// <summary>
/// 窗体状态改变时候触发
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SysTray_StateChanged(object sender, EventArgs e)
{
if (this.WindowState == WindowState.Minimized)
{
this.Visibility = Visibility.Hidden;
}
}
/// <summary>
/// 鼠标单击
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void notifyIcon_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
if (this.Visibility == Visibility.Visible)
{
this.Visibility = Visibility.Hidden;
}
else
{
this.Visibility = Visibility.Visible;
this.Activate();
}
}
}
/// <summary>
/// 退出选项
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void exit_Click(object sender, EventArgs e)
{
if (System.Windows.MessageBox.Show("确定要关闭吗?",
"退出",
MessageBoxButton.YesNo,
MessageBoxImage.Question,
MessageBoxResult.No) == MessageBoxResult.Yes)
{
notifyIcon.Dispose();
System.Windows.Application.Current.Shutdown();
}
}
#endregion
相关文章推荐
- 轻量级JavaEE企业应用实战(六)
- 95,autorelease的注意事项
- No important thing
- iOS工作小常识
- iOS 【UIKit-iOS事件之触摸事件】
- retain、copy、mutableCopy的区别(深拷贝 浅拷贝)
- 如何获取选中的复选框
- Java 斐波那契数列
- 第一次了解GitHub,在Windows下使用GitHub
- 深入理解Java的接口和抽象类(转载---写的非常好~)
- sendmsg() Permission denied
- [网络管理]共享文件之卷影副本的使用
- Spring数据源配置总结
- python科学计算库安装
- python科学计算库安装
- python科学计算库安装
- python科学计算库安装
- python科学计算库安装
- python科学计算库安装
- python科学计算库安装