什么是托盘控件?
相信大家看到下面这个图片,就大概清楚了,没错,就是Windows窗体右下角的图标栏。
1、在App.xmal中添加
ShutdownMode="OnExplicitShutdown" Startup="ApplicationStartup" Exit="ApplicationExit">
2、在App.xmal.cs中添加命名空间
using System.Windows;
using System.Windows.Forms;
using Application = System.Windows.Application;
3、在App.xmal.cs中添加代码
/// <summary>
/// 开始运行的事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ApplicationStartup(object sender,StartupEventArgs e)
{
AddTrayIcon();
//RemoveTrayIcon();
}
/// <summary>
/// 添加基础信息
/// </summary>
private void AddTrayIcon()
{
if (trayIcon!=null)
{
return;
}
trayIcon = new NotifyIcon
{
Icon = new System.Drawing.Icon("托盘图标.ico"),//图标图片
Text = "智晟网络会所"//冒泡内容
};
trayIcon.Visible = true;
#region 添加右键菜单内容
//实例化右键菜单
ContextMenu menu = new ContextMenu();
//添加菜单的内容
MenuItem OnlineItem = new MenuItem();
OnlineItem.Text = "查看上机记录";
OnlineItem.Click += new EventHandler(OnlineNote);
MenuItem ReChargeItem = new MenuItem();
ReChargeItem.Text = "查看充值记录";
ReChargeItem.Click += new EventHandler(ReChargeNote);
MenuItem ModigyPWD = new MenuItem();
ModigyPWD.Text = "修改密码";
ModigyPWD.Click += new EventHandler(ModifyPwd);
//把写好的内容加进去
menu.MenuItems.Add(ReChargeItem);
menu.MenuItems.Add(OnlineItem);
menu.MenuItems.Add(ModigyPWD);
trayIcon.ContextMenu = menu;//设置右键弹出菜单 #endregion
#endregion
}
/// <summary>
/// 结束时调用的事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ApplicationExit(object sender,ExitEventArgs e)
{
RemoveTrayIcon();
}
/// <summary>
/// 释放资源
/// </summary>
private void RemoveTrayIcon()
{
if (trayIcon != null)
{
trayIcon.Visible = false;
trayIcon.Dispose();//释放资源
trayIcon = null;
}
}
/// <summary>
/// 查询上机记录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnlineNote(object sender, EventArgs e)
{
#region 单例模式
OnlineNoteUI OnlineNote = OnlineNoteUI.GetInstance();
OnlineNote.Show();
#endregion
}
/// <summary>
/// 双击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnNotifyIconDoubleClick(object sender,EventArgs e)
{
}
/// <summary>
/// 查询充值记录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ReChargeNote(object sender, EventArgs e)
{
//单例模式
ReChargeNoteUI ReChargeNote =ReChargeNoteUI.GetInstance();
ReChargeNote.Show();
}
/// <summary>
/// 修改密码
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ModifyPwd(object sender, EventArgs e)
{
//单例模式
ModifyPwdUI ModifyPwd = ModifyPwdUI.GetInStance();
ModifyPwd.Show();
}
总结一下,希望对大家有所帮助!