#region 全局变量 //创建NotifyIcon对象 NotifyIcon notifyIcon = new NotifyIcon(); d.Icon ico = new d.Icon("../../ico/logo_x.ico"); d.Image img = null; ContextMenuStrip conTextMenuStrip = new ContextMenuStrip(); List<ToolStripMenuItem> toolStripMenuItem = new List<ToolStripMenuItem>(); const string Red = "Red"; const string Green = "Green"; const string Blue = "Blue"; const string Yellow = "Yellow"; #endregion #region 加载 public DelegateControlUpdateOne() { InitializeComponent(); //加载图片 notifyIcon.Icon = ico; //隐藏任务栏区图标 this.ShowInTaskbar = false; //图标显示在托盘区 notifyIcon.Visible = true; //添加托盘菜单项 conTextMenuStrip.ShowImageMargin = true; img = d.Image.FromFile("../../ico/logo_x.ico"); CreateItem("红色", Red); CreateItem("蓝色", Blue); CreateItem("绿色", Green); CreateItem("黄色", Yellow); conTextMenuStrip.Items.AddRange(toolStripMenuItem.ToArray()); //托盘菜单项在图标右键时显示 notifyIcon.ContextMenuStrip = conTextMenuStrip; //委托点击事件 this.Closed += DelegateControlUpdateOne_Closed; } #endregion #region CreateItem(string text, string tag) /// <param name="text">text</param> /// <param name="tag">颜色</param> void CreateItem(string text, string tag) { ToolStripMenuItem toolStripMenuitem = new ToolStripMenuItem("白色", img); toolStripMenuitem.Click += toolStripMenuitem_Click; toolStripMenuitem.Tag = tag; toolStripMenuItem.Add(toolStripMenuitem); } #endregion #region 改变颜色 void toolStripMenuitem_Click(object sender, EventArgs e) { ToolStripMenuItem item = sender as ToolStripMenuItem; if (item == null) return; switch (item.Tag.ToString()) { case Red: Hint.Background = Brushes.Red; break; case Blue: Hint.Background = Brushes.Blue; break; case Yellow: Hint.Background = Brushes.Yellow; break; case Green: Hint.Background = Brushes.Green; break; } } #endregion #region 清空窗体 DelegateControlUpdateOne_Closed(object sender, EventArgs e) void DelegateControlUpdateOne_Closed(object sender, EventArgs e) { notifyIcon.Dispose(); notifyIcon = null; } #endregion