Wpf程序显示在任务栏

后台代码如下:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Demo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private NotifyIcon TrayIcon;
        WindowState ws;
        WindowState wsl;
        private System.Windows.Forms.ContextMenu notifyiconMnu;
        public MainWindow()
        {
            InitializeComponent();
            Initializenotifyicon();
            //保证窗体显示在上方。
            wsl = WindowState;

        }
        private void OnNotifyIconDoubleClick(object sender, EventArgs e)
        {
            this.Show();
            WindowState = wsl;
        }

        private void Window_StateChanged(object sender, EventArgs e)
        {
            ws = WindowState;
            if (ws == WindowState.Minimized)
            {
                this.Hide();
            }
        }
        public void TopAllmost(object sender, System.EventArgs e)
        {
            try
            {
                System.Windows.Forms.MenuItem item = (System.Windows.Forms.MenuItem)sender;
                bool check = item.Checked;
                item.Checked = !check;
                this.Topmost = item.Checked;
            }
            catch
            {
            }
        }


        public void ExitSelect(object sender, System.EventArgs e)
        {
            TrayIcon.Visible = false;
            this.Close();
        }
        private void Initializenotifyicon()
        {
            Assembly assem = Assembly.GetExecutingAssembly();
            Stream stream = assem.GetManifestResourceStream(
             "Tray.ico");

            TrayIcon = new NotifyIcon();
            TrayIcon.Icon = new System.Drawing.Icon("Tray.ico");//程序图标
            this.TrayIcon.BalloonTipText = "Hello, 文件监视器"; //设置程序启动时显示的文本
            TrayIcon.Text = "MutiMenu " + "Copyright: Frank Zhao";
            TrayIcon.Visible = true;
            TrayIcon.MouseDoubleClick += OnNotifyIconDoubleClick;
            this.TrayIcon.ShowBalloonTip(1000);
            //  TrayIcon.Click += new System.EventHandler(this.click);

            //tray menu
            System.Windows.Forms.MenuItem[] mnuItms = new System.Windows.Forms.MenuItem[5];
            mnuItms[0] = new System.Windows.Forms.MenuItem();
            mnuItms[0].Text = "Aways Top";
            mnuItms[0].Click += new System.EventHandler(this.TopAllmost);
            mnuItms[0].Checked = true;

            mnuItms[1] = new System.Windows.Forms.MenuItem("-");

            mnuItms[2] = new System.Windows.Forms.MenuItem();
            mnuItms[2].Text = "Settings...";
          //  mnuItms[2].Click += new System.EventHandler(this.Settings);

            mnuItms[3] = new System.Windows.Forms.MenuItem("-");

            mnuItms[4] = new System.Windows.Forms.MenuItem();
            mnuItms[4].Text = "Exit";
            mnuItms[4].Click += new System.EventHandler(this.ExitSelect);
            mnuItms[4].DefaultItem = true;


            notifyiconMnu = new System.Windows.Forms.ContextMenu(mnuItms);
            TrayIcon.ContextMenu = notifyiconMnu;
        }
    }
}
原文地址:https://www.cnblogs.com/3xiaolonglong/p/11577990.html