C# 控制台程序 托盘图标 事件响应

时间:2023-03-08 17:43:52

static void Main(string[] args)
{

NotifyIconHelper ni = new NotifyIconHelper();
NotifyIconHelper.ShowNotifyIcon();

while (true)
{
Application.DoEvents();
}

}

----------------------------

namespace ConsoleHideShow
{
/// <summary>
/// 系统图标操作
/// </summary>
public class NotifyIconHelper
{
public NotifyIconHelper()
{
_NotifyIcon.Icon = new Icon(@"C:\1.ico");
_NotifyIcon.Visible = false;
_NotifyIcon.Text = "tray";

ContextMenu menu = new ContextMenu();
MenuItem item = new MenuItem();
item.Text = "右键菜单,还没有添加事件";
item.Index = 0;

menu.MenuItems.Add(item);
_NotifyIcon.ContextMenu = menu;

_NotifyIcon.MouseDoubleClick += new MouseEventHandler(_NotifyIcon_MouseDoubleClick);
}
static void _NotifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
Console.WriteLine("托盘被双击.");
}

#region 托盘图标
static NotifyIcon _NotifyIcon = new NotifyIcon();
public static void ShowNotifyIcon()
{
_NotifyIcon.Visible = true;
_NotifyIcon.ShowBalloonTip(3000, "", "我是托盘图标,用右键点击我试试,还可以双击看看。", ToolTipIcon.None);
}
public static void HideNotifyIcon()
{
_NotifyIcon.Visible = false;
}

#endregion

}
}

参考》http://www.cnblogs.com/bruceli/archive/2011/04/28/2031584.html