博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WPF之托盘图标的设定
阅读量:6988 次
发布时间:2019-06-27

本文共 3565 字,大约阅读时间需要 11 分钟。

原文:

首先需要在项目中引用System.Windows.Forms,System.Drawing;

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;using System.Drawing;namespace WpfApplication1{    ///    /// Interaction logic for MainWindow.xaml    ///    public partial class MainWindow : Window    {        public MainWindow()        {            InitializeComponent();            InitialTray();        }        private System.Windows.Forms.NotifyIcon notifyIcon = null;        private void InitialTray()        {            //设置托盘的各个属性            notifyIcon = new System.Windows.Forms.NotifyIcon();            notifyIcon.BalloonTipText = \"程序开始运行\";            notifyIcon.Text = \"托盘图标\";            notifyIcon.Icon = new System.Drawing.Icon(System.Windows.Forms.Application.StartupPath + \"\\\\wp.ico\");            notifyIcon.Visible = true;            notifyIcon.ShowBalloonTip(2000);            notifyIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(notifyIcon_MouseClick);            //设置菜单项            System.Windows.Forms.MenuItem menu1 = new System.Windows.Forms.MenuItem(\"菜单项1\");            System.Windows.Forms.MenuItem menu2 = new System.Windows.Forms.MenuItem(\"菜单项2\");            System.Windows.Forms.MenuItem menu = new System.Windows.Forms.MenuItem(\"菜单\", new System.Windows.Forms.MenuItem[] { menu1 , menu2 });            //退出菜单项            System.Windows.Forms.MenuItem exit = new System.Windows.Forms.MenuItem(\"exit\");            exit.Click += new EventHandler(exit_Click);            //关联托盘控件            System.Windows.Forms.MenuItem[] childen = new System.Windows.Forms.MenuItem[] { menu , exit };            notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(childen);            //窗体状态改变时候触发            this.StateChanged += new EventHandler(SysTray_StateChanged);        }        ///        /// 窗体状态改变时候触发        ///        ///        ///        private void SysTray_StateChanged(object sender, EventArgs e)        {            if (this.WindowState == WindowState.Minimized)            {                this.Visibility = Visibility.Hidden;            }        }        ///        /// 退出选项        ///        ///        ///        private void exit_Click(object sender, EventArgs e)        {            if (System.Windows.MessageBox.Show(\"确定要关闭吗?\",                                               \"退出\",                                                MessageBoxButton.YesNo,                                                MessageBoxImage.Question,                                                MessageBoxResult.No) == MessageBoxResult.Yes)            {                notifyIcon.Dispose();                System.Windows.Application.Current.Shutdown();            }        }        ///        /// 鼠标单击        ///        ///        ///        private void notifyIcon_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)        {            if (e.Button == System.Windows.Forms.MouseButtons.Left)            {                if (this.Visibility == Visibility.Visible)                {                    this.Visibility = Visibility.Hidden;                }                else                {                    this.Visibility = Visibility.Visible;                    this.Activate();                }            }        }    }}
 
以上代码并非用户控件代码,只需加在主窗体中即可。

转载地址:http://kowvl.baihongyu.com/

你可能感兴趣的文章
mysql的sql文件的备份与还原
查看>>
Java API —— 泛型
查看>>
十三周进度报告
查看>>
「APIO2018」选圆圈
查看>>
单例模式的那些事
查看>>
Canvas - 时钟绘制
查看>>
linux-vsftp
查看>>
modelsim 中如何加载多个对比波形文件
查看>>
Linux内核抢占与中断返回【转】
查看>>
Linux 文件操作监控inotify功能及实现原理【转】
查看>>
linux arm的存储分布那些事之一
查看>>
Spring下redis的配置
查看>>
vs2010在进行数据架构比较时报'text lines should not be null'错误
查看>>
jeecg入门操作—表单界面
查看>>
网页音乐制作器(网页钢琴)-- MusicMaker
查看>>
oracle优化:避免全表扫描(高水位线)
查看>>
对超级课程表产品的一些个人小看法
查看>>
词频统计 效能分析
查看>>
Linux终极shell-zsh的完美配置方案!——oh-my-zsh
查看>>
MYSQL 函数、自定义函数 function
查看>>