private void CreateContextMenu() { if (_notifyIcon == null) { _notifyIcon = new System.Windows.Forms.NotifyIcon(); _notifyIcon.ContextMenuStrip = new System.Windows.Forms.ContextMenuStrip(); _startMenuItem = new Forms.ToolStripMenuItem("Start Server"); _startMenuItem.Click += _startMenuItem_Click; _notifyIcon.ContextMenuStrip.Items.Add(_startMenuItem); _stopMenuItem = new Forms.ToolStripMenuItem("Stop Server"); _stopMenuItem.Click += _stopMenuItem_Click; _notifyIcon.ContextMenuStrip.Items.Add(_stopMenuItem); var seperator = new Forms.ToolStripSeparator(); _notifyIcon.ContextMenuStrip.Items.Add(seperator); _exitMenuItem = new Forms.ToolStripMenuItem("Exit"); _exitMenuItem.Click += _exitMenuItem_Click; _notifyIcon.ContextMenuStrip.Items.Add(_exitMenuItem); _notifyIcon.ContextMenuStrip.Opening += ContextMenuStrip_Opening; _notifyIcon.Icon = SmartControlServer.Properties.Resources.trayIcon; _notifyIcon.Visible = true; } }
private void initNotifyIcon() { this.notifyIcon = new System.Windows.Forms.NotifyIcon(); this.notifyIcon.BalloonTipText = "LC 服务正在运行..."; //设置程序启动时显示的文本 this.notifyIcon.Text = "LC 服务"; //最小化到托盘时,鼠标点击时显示的文本 this.notifyIcon.Icon = new System.Drawing.Icon( System.Windows.Application.GetResourceStream(new Uri("/cs.fa.linecollector;component/favicon.ico", UriKind.Relative)).Stream ); this.notifyIcon.Visible = true; ////右键菜单--打开菜单项 //System.Windows.Forms.MenuItem open = new System.Windows.Forms.MenuItem("打开"); //open.Click += new EventHandler(ShowWindow); ////右键菜单--退出菜单项 //System.Windows.Forms.MenuItem exit = new System.Windows.Forms.MenuItem("Exit"); //exit.Click += new EventHandler(CloseWindow); //关联托盘控件 //System.Windows.Forms.MenuItem[] childen = new System.Windows.Forms.MenuItem[] { open, exit }; //notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(childen); notifyIcon.MouseDoubleClick += mouseShowWindow; this.notifyIcon.ShowBalloonTip(1000); }