⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 trayicon.cs

📁 Gibphone is CSharp Program, it can tell you how to design p2p chat.
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Collections;

namespace GPCore
{
    /// <summary>
    /// The system tray icon.
    /// </summary>
    public class TrayIcon : IDisposable
    {
        private NotifyIcon icon;
        /// <summary>
        /// Gets the <see cref="NotifyIcon"/> that appears in the system tray.
        /// </summary>
        public NotifyIcon Icon
        {
            get { return icon; }
        }

        private ContextMenuStrip menu;
        /// <summary>
        /// This is the context menu of the NotifyIcon. Use this to
        /// add the contents of the menu to your own form.
        /// </summary>
        public ContextMenuStrip Menu
        {
            get { return this.menu; }
        }

        private ToolStripItem topbar;
        internal TrayIcon()
        {
            icon = new NotifyIcon();
            Icon g = new Icon(typeof(TrayIcon), "GPIcon.ico");
            icon.MouseClick += new MouseEventHandler(Icon_MouseClick);

            this.menu = new ContextMenuStrip();

            this.menu.ItemAdded += new ToolStripItemEventHandler(menu_ItemAdded);
            this.menu.ItemRemoved += new ToolStripItemEventHandler(menu_ItemRemoved);

            topbar = this.menu.Items.Add("-");
            topbar.Visible = false;
            this.menu.Items.Add("Plugins", null, new EventHandler(Plugin_Click));
            this.menu.Items.Add("Accounts", null, new EventHandler(Account_Click));
            this.menu.Items.Add("Report Bug", null, new EventHandler(Bug_Click));
            this.menu.Items.Add("-");
            this.menu.Items.Add("Close", null, new EventHandler(Close_Click));

            icon.ContextMenuStrip = menu;
            icon.Icon = g;
            icon.Visible = true;
        }

        void menu_ItemRemoved(object sender, ToolStripItemEventArgs e)
        {
            if (onMenuItemDeleted != null)
                onMenuItemDeleted(this, e);
        }

        void menu_ItemAdded(object sender, ToolStripItemEventArgs e)
        {
            if (onMenuItemAdded != null)
                onMenuItemAdded(this, e);
        }

        /// <summary>
        /// This delegate is used when menu items are added or removed from the icon's context menu.
        /// </summary>
        /// <param name="sender">The <see cref="TrayIcon"/> that threw the event.</param>
        /// <param name="e">The <see cref="ToolStripItemEventArgs"/> describing the item affected.</param>
        public delegate void MenuItemDelegate(object sender, ToolStripItemEventArgs e);
        /// <summary>
        /// Happens when someone adds a Menu Item to the Notify Menu Icon
        /// </summary>
        public event MenuItemDelegate onMenuItemAdded;
        /// <summary>
        /// Happens when someone removes a Menu Item to the Notify Menu Icon
        /// </summary>
        public event MenuItemDelegate onMenuItemDeleted;


        void Close_Click(object sender, EventArgs e)
        {
            Core.Close();
        }

        /// <summary>
        /// Use this to add your own menus to the <see cref="TrayIcon"/>.
        /// </summary>
        /// <param name="caption">The caption to display.</param>
        /// <param name="image">The <see cref="Image"/> to display.</param>
        /// <param name="onClick">The EventHandler of what to do when clicked.</param>
        public ToolStripMenuItem AddToMenu(string caption, Image image, EventHandler onClick)
        {
            topbar.Visible = true;
            ToolStripMenuItem mi = new ToolStripMenuItem(caption, image, onClick);
            this.menu.Items.Insert(0, mi);
            return mi;
        }

        /// <summary>
        /// Inserts <paramref name="item"/> at the top of the <see cref="Menu"/>.
        /// </summary>
        /// <param name="item">The ToolStripMenuItem to add.</param>
        public void AddToMenu(ToolStripMenuItem item)
        {
            topbar.Visible = true;
            this.menu.Items.Insert(0, item);
        }

        /// <summary>
        /// Use this to add a line to the menu.
        /// </summary>
        public void AddLineToMenu()
        {
            this.menu.Items.Add("-");
        }
        /// <summary>
        /// Sets whether the <see cref="TrayIcon"/> is visible or not.
        /// </summary>
        public bool Visible
        {
            get { return this.Icon.Visible; }
            set { this.Icon.Visible = value; }
        }

        void Plugin_Click(object sender, EventArgs e)
        {
            Core.ShowPluginForm();
        }

        void Account_Click(object sender, EventArgs e)
        {
            Core.ShowAccountForm();
        }

        void Icon_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Right) return;
        }

        void Bug_Click(object sender, EventArgs e)
        {
            Core.ShowBugForm();
        }

        #region IDisposable Members
        /// <summary>
        /// Disposes of the Icon and the menu
        /// </summary>
        public void Dispose()
        {
            icon.Dispose();
            menu.Dispose();
        }

        #endregion
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -