您的位置:首页 > 其它

托盘图标的制作,点击与触发

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: