WPF实现最小化到任务栏

WPF实现最小化到任务栏

 private winform.NotifyIcon notifyIcon;//声明
//在主程序窗口调用
 public MainWindow()
        {
            InitializeComponent();
            NotifyButton();
        }


        //任务栏按钮实现
        private void NotifyButton()
        {
            winform.MenuItem showApp = new winform.MenuItem() { Text = "显示主页面" };//定义菜单按钮
            winform.MenuItem exitApp = new winform.MenuItem() { Text = "退出程序" };//定义菜单按钮
            showApp.Click += ShowWindow;//绑定显示事件
            exitApp.Click += ExitApp;//绑定退出事件

            winform.ContextMenu iconMenu = new winform.ContextMenu();//定义菜单
            iconMenu.MenuItems.AddRange(new winform.MenuItem[] { showApp, exitApp });//添加菜单按钮

            notifyIcon = new winform.NotifyIcon();//实例化
            notifyIcon.Text = "NetTool";//鼠标在托盘图标上面时显示的文本
            System.IO.Stream ManifestStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("NetTool.icon.ico");//读取图标文件流
            notifyIcon.Icon = new System.Drawing.Icon(ManifestStream);//定义程序图标
            notifyIcon.Visible = true;//是否显示
            notifyIcon.MouseDoubleClick += ShowWindow;//鼠标双击事件
            notifyIcon.ContextMenu = iconMenu;//绑定菜单

            //notifyIcon.BalloonTipText = "NetTool: 在通知栏显示和隐藏操作界面"; //设置程序启动时显示的文本
            //notifyIcon.ShowBalloonTip(500);
        }

        private void ShowWindow(object sender, EventArgs e)
        {
            Visibility = Visibility.Visible;
        }

        private void ExitApp(object sender, EventArgs e)
        {
            Application.Current.Shutdown();
        }

演示程序

你可能感兴趣的:(开发笔记,wpf,c#)