右下角托盘程序

来源:互联网 发布:linux vim编辑器 编辑:程序博客网 时间:2024/04/29 20:30

在公共控件里拖一个NotifyIcon控件进来,命名为TrayIcon

 

  public partial class Form1 : Form

    {

 

 private Icon mNetTrayIcon = new Icon("icon.ico"); //在debug下放一个icon.ico文件

 

 private ContextMenu notifyiconMnu; //放一个托盘菜单

 

        public Form1()

        {

 

            InitializeComponent();

 

            //初始化托盘程序的各个要素

 

            Initializenotifyicon();

        }

 

        private void Initializenotifyicon()

        {

 

            MenuItem[] mnuItms = new MenuItem[3];

            mnuItms[0] = new MenuItem();

            mnuItms[0].Text = "主界面";

            mnuItms[0].Click += new System.EventHandler(showForm);


            mnuItms[1] = new MenuItem("-"); //放一个分隔符


            mnuItms[2] = new MenuItem();

            mnuItms[2].Text = "退出系统";

            mnuItms[2].Click += new System.EventHandler(ExitSelect);

            mnuItms[2].DefaultItem = true;

            notifyiconMnu = new ContextMenu(mnuItms);


            TrayIcon.Visible = false;

            TrayIcon.Icon = mNetTrayIcon;

            TrayIcon.Text = "煲电话时机提示器";

            TrayIcon.ContextMenu = notifyiconMnu;

 

 

        }

 

 

        void showForm(object o, EventArgs args)

        {

            this.Show();

            this.WindowState = FormWindowState.Normal;

            TrayIcon.Visible = false;

            this.ShowInTaskbar = true;

         

        }

 

        void ExitSelect(object o, EventArgs args)

        {

            this.TrayIcon.Visible = false;

            this.Close();

            this.Dispose();

           

            Application.Exit();

        }

 

         private void Form1_SizeChanged(object sender, EventArgs e)

        {

            //最小化时缩小至托盘

            if (this.WindowState == FormWindowState.Minimized)

            {

                HideMainForm();

            }

        }


void HideMainForm() //隐藏主菜单

        {

            TrayIcon.Visible = true;

            this.Hide();

        }

 

}

原创粉丝点击