wpf下使用NotifyIcon

Posted lunawzh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了wpf下使用NotifyIcon相关的知识,希望对你有一定的参考价值。

以前在winForm下使用过NotifyIcon,到wpf找不到了,在wpf下还是直接用WinForm里的那个NotifyIcon实现最小到系统托盘

 

定义一个NotifyIcon成员 :

NotifyIcon notifyIcon = null;
WindowState ws; //记录窗体状态

加载窗体时初始化NotifyIcon:

            this.notifyIcon = new NotifyIcon();
            this.notifyIcon.BalloonTipText = "Hello, 文件监视器"; //设置程序启动时显示的文本
            this.notifyIcon.Text = "文件监视器";//最小化到托盘时,鼠标点击时显示的文本
            this.notifyIcon.Icon = new System.Drawing.Icon(@"b.ico");//程序图标
            this.notifyIcon.Visible = true;
            notifyIcon.MouseDoubleClick += OnNotifyIconDoubleClick;
            this.notifyIcon.ShowBalloonTip(1000);


同时添加右键菜单:

            System.Windows.Forms.MenuItem m1 = new System.Windows.Forms.MenuItem("open");
            m1.Click += m1_Click;
            System.Windows.Forms.MenuItem m2 = new System.Windows.Forms.MenuItem("close");
            m2.Click += m2_Click;
            System.Windows.Forms.MenuItem[] m = new System.Windows.Forms.MenuItem[] { m1, m2 };
            this.notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(m);

事件为:

        void m2_Click(object sender, EventArgs e)
        {
            if (System.Windows.MessageBox.Show("sure to exit?",
                                               "application",
                                                MessageBoxButton.YesNo,
                                                MessageBoxImage.Question,
                                                MessageBoxResult.No) == MessageBoxResult.Yes)
            {
                System.Windows.Application.Current.Shutdown();
            }
        }

        void m1_Click(object sender, EventArgs e)
        {
            this.Show();
            this.Activate();
        }

对窗体添加事件:

            this.SizeChanged += MainWindow_SizeChanged;
            this.Closing += MainWindow_Closing;
     void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (ws == WindowState.Minimized)
            {
                this.Hide();
                this.notifyIcon.Visible = true;

            }
        }
        void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            
                e.Cancel = true;
                this.WindowState = WindowState.Minimized;
                ws = WindowState.Minimized;
                this.notifyIcon.Visible = true;
                this.notifyIcon.ShowBalloonTip(30, "注意", "大家好,这是一个事例", ToolTipIcon.Info);
        }

双击拖盘弹出窗体

        private void OnNotifyIconDoubleClick(object sender, EventArgs e)
        {
            if (ws == WindowState.Minimized)
            {
                this.WindowState = WindowState.Normal;
                this.notifyIcon.Visible = false;
            }
        }

好,大概这些知识够用了,最后来一个完整的代码:

  public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            icon();
            //保证窗体显示在上方。
            wsl = WindowState;

            this.SizeChanged += MainWindow_SizeChanged;
            this.Closing += MainWindow_Closing;
        }
        void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (ws == WindowState.Minimized)
            {
                this.Hide();
                this.notifyIcon.Visible = true;

            }
        }
        void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            
                e.Cancel = true;
                this.WindowState = WindowState.Minimized;
                ws = WindowState.Minimized;
                this.notifyIcon.Visible = true;
                this.notifyIcon.ShowBalloonTip(30, "注意", "大家好,这是一个事例", ToolTipIcon.Info);
        }


        

        WindowState ws;
        WindowState wsl;

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


            System.Windows.Forms.MenuItem m1 = new System.Windows.Forms.MenuItem("open");
            m1.Click += m1_Click;
            System.Windows.Forms.MenuItem m2 = new System.Windows.Forms.MenuItem("close");
            m2.Click += m2_Click;
            System.Windows.Forms.MenuItem[] m = new System.Windows.Forms.MenuItem[] { m1, m2 };
            this.notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(m);
        }

        void m2_Click(object sender, EventArgs e)
        {
            if (System.Windows.MessageBox.Show("sure to exit?",
                                               "application",
                                                MessageBoxButton.YesNo,
                                                MessageBoxImage.Question,
                                                MessageBoxResult.No) == MessageBoxResult.Yes)
            {
                System.Windows.Application.Current.Shutdown();
            }
        }

        void m1_Click(object sender, EventArgs e)
        {
            this.Show();
            this.Activate();
        }

        private void OnNotifyIconDoubleClick(object sender, EventArgs e)
        {
            if (ws == WindowState.Minimized)
            {
                this.WindowState = WindowState.Normal;
                this.notifyIcon.Visible = false;
            }
        }

        private void Window_StateChanged(object sender, EventArgs e)
        {
            ws = WindowState;
            if (ws == WindowState.Minimized)
            {
                this.notifyIcon.Visible = true;
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.notifyIcon.BalloonTipText = "有新信息";
        }
    }

 

以上是关于wpf下使用NotifyIcon的主要内容,如果未能解决你的问题,请参考以下文章

WPF 基础控件之托盘

当应用程序具有 NotifyIcon 时,任务计划程序给出 0xE0434352

请问wpf中的GroupBox控件我想让他的Header位于右下角,注意是右下角,能实现吗?请详细说明,谢谢!

WPF 托盘图标右键弹出的ContextMenu如何关闭

WPF - 将应用程序从系统托盘置于最前面

托盘程序菜单位置 NotifyIcon menu position