C# 杂货

后台运行

this.ShowInTaskbar = false;//不在任务栏中显示
this.Visibility = Visibility.Hidden;//不显示窗口

设置右下角托盘

     //托盘小图标
        NotifyIcon notifyIcon;
        void icon() {
            string path = System.IO.Path.GetFullPath(@"Iconfavicon.ico");
            if (File.Exists(path))
            {
                this.notifyIcon = new NotifyIcon();
                this.notifyIcon.BalloonTipText = "Hello, 文件监视器"; //设置程序启动时显示的文本
                this.notifyIcon.Text = "文件监视器";//最小化到托盘时,鼠标点击时显示的文本
                System.Drawing.Icon icon = new System.Drawing.Icon(path);//程序图标 
                this.notifyIcon.Icon = icon;
                this.notifyIcon.Visible = true;
                //notifyIcon.MouseDoubleClick += OnNotifyIconDoubleClick;
                this.notifyIcon.ShowBalloonTip(1000);
            }

            contextMenu();
        }
        #region 托盘右键菜单
        private void contextMenu()
        {
            ContextMenuStrip cms = new ContextMenuStrip();


            //关联 NotifyIcon 和 ContextMenuStrip
            notifyIcon.ContextMenuStrip = cms;

            System.Windows.Forms.ToolStripMenuItem exitMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            exitMenuItem.Text = "退出";
            exitMenuItem.Click += new EventHandler(exitMenuItem_Click);


            System.Windows.Forms.ToolStripMenuItem hideMenumItem = new System.Windows.Forms.ToolStripMenuItem();
            hideMenumItem.Text = "隐藏";
            hideMenumItem.Click += new EventHandler(hideMenumItem_Click);


            System.Windows.Forms.ToolStripMenuItem showMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            showMenuItem.Text = "显示";
            showMenuItem.Click += new EventHandler(showMenuItem_Click);


            cms.Items.Add(exitMenuItem);
            cms.Items.Add(hideMenumItem);
            cms.Items.Add(showMenuItem);
        }

        private void exitMenuItem_Click(object sender, EventArgs e)
        {
            notifyIcon.Visible = false;

            System.Windows.Application.Current.Shutdown();
        }

        private void hideMenumItem_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void showMenuItem_Click(object sender, EventArgs e)
        {
            this.Show();
            this.Activate();
        }
        #endregion
原文地址:https://www.cnblogs.com/lingLuoChengMi/p/14030355.html