• 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 = "有新信息";
            }
        }
  • 相关阅读:
    什么是MSI文件?
    学习window系统下的注册表
    AngularJS学习手册
    学习ajax 总结
    jquery基础教程读书总结
    overflow:hidden清除浮动原理解析及清除浮动常用方法总结
    javascript进阶-原型prototype
    javascript-函数进阶
    小技巧之a标签自动解析URL
    Myeclipse出现 java文件中文乱码问题
  • 原文地址:https://www.cnblogs.com/lunawzh/p/6135065.html
Copyright © 2020-2023  润新知