WPF 程序启动显示为通知区域的图标方法
首先需要引用 System.Windows; System.Drawing;
- 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("NotifyIcon.ico");
- notifyIcon.Visible = true;
- notifyIcon.ShowBalloonTip();
- 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);
- }
- /// <summary>
- /// 窗体状态改变时候触发
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void SysTray_StateChanged(object sender, EventArgs e)
- {
- if (this.WindowState == WindowState.Minimized)
- {
- //this.Visibility = Visibility.Hidden;
- }
- }
- /// <summary>
- /// 退出选项
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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();
- }
- }
- /// <summary>
- /// 鼠标单击
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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.WindowState = WindowState.Normal;
- //this.Activate();
- }
- }
- }
- // 改变窗体关闭按钮事件
- private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
- {
- e.Cancel = true;//取消窗体关闭
- this.Visibility = Visibility.Hidden;//窗体隐藏
- }
- }
WPF 程序启动显示为通知区域的图标方法的更多相关文章
- wpf 程序启动显示图片
一.设置图片的生成操作 程序启动时会出现0.5秒的图片显示,再显示程序界面. 二.写代码实现相同效果 /// <summary> /// App.xaml 的交互逻辑 /// </s ...
- Win7通知区域的图标怎么去除?
由于本人有洁癖,最近在用win7的时候,很收不了已经卸载了的一些软件,在win7右下角的通知区域图标中还留有痕迹,于是上网查找了下解决方案. 用以下方法完美解决问题. 这里依然是以注册表的修改方法为主 ...
- 关于WPF程序启动性能
项目里WPF的启动时间太久(>1分钟),显然是不能接受的.超过10秒,连Loading的等待框都会让用户感到厌烦. 1. 症状 项目的结构是1个WPF主进程,启动3个WPF子进程.子进程在启动时 ...
- WPF 程序中启动和关闭外部.exe程序
当需要在WPF程序启动时,启动另一外部程序(.exe程序)时,可以按照下面的例子来: C#后台代码如下: using System; using System.Collections.Generic; ...
- 处理win7任务栏通知区域图标异常问题
故障现象:安装的某软件比如QQ,应用程序运行图标始终没有在win7任务栏通知区域显示出来,经观查发现win7任务栏通知区域有几个已删除应用的图标出现,应该是有故障了. 故障现象一:已经卸载的程序,还在 ...
- WPF程序在Windows 7下应用Windows 8主题
这篇博客介绍如何在Windows 7下应用Windows 8的主题. 首先我们先看一个很常见的场景,同样的WPF程序(样式未重写)在不同的操作系统上展示会有些不同.这是为什么呢?WPF程序启动时会加载 ...
- PreApplicationStartMethodAttribute程序启动扩展
一.PreApplicationStartMethodAttribute 类简介 应用程序启动时提供的扩展的支持. 命名空间: System.Web程序集: System.Web(位于 Syst ...
- WPF ListView 居中显示
原文:WPF ListView 居中显示 今天遇到的问题: 方法1:设置GridViewColumn的ActualWidth <ListView > <ListView.View&g ...
- Win10任务栏通知区域上已卸载程序无效图标选项如何清除?
在Win10系统中,大部分用户都已经知道在“选择在任务栏上显示哪些图标”来让一些软年图标显示,一些隐藏,不过使用Win10系统久了之后发现,在设置通知区域图标中有很多已经卸载程序的无效选项!这让设置时 ...
随机推荐
- 关于移动端1px边框问题
<div class="z_nei_list"> <div class="z_name_left font-size3">身份证号:&l ...
- android如何实现文件按时间先后顺序排列显示
<span style="font-size:18px;">File[] files =parentFile.listFiles(fileFilter);//通过fil ...
- WPF点补间、拟合回归直线
1,path画刷,绘制正弦 点,线: 生成正弦点 profilePoint.Value = * ( - Math.Sin(i * Math.PI / )); profilePoint.Type = ; ...
- log4j.xml 日志只输出指定类配置
1.日志增加appender 指定日志生成时间.格式.间隔时间. 2.category指定哪些或哪个类日志生成在文件中. 3.自定义logger避免不要将userBehavior定义到root中避免继 ...
- Codeforce 493c
H - Vasya and Basketball Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- Git Commands
Show ssh key file: ssh -v git@github.com
- iOS 'The sandbox is not sync with the Podfile.lock错误
出现以下错误时, diff: /../Podfile.lock: No such file or directory diff: Manifest.lock: No such file or dire ...
- Java classpath 如何自动添加web-content /lib下的jar包
右键属性--Java build path--Libraries--add library--Web app libraries -- next --next -- ok
- Mybatis Generator insert useGeneratedKeys keyProperty
Mybatis自动生成代码,需要用到mybatis Generator,详见http://mybatis.github.io/generator/configreference/generatedKe ...
- 拓展企业VR培训业务,这家VR训练公司StriVR完成500万美元融资!
虚拟现实初创公司StriVR最近发布了新的企业VR训练产品项目,并宣布在刚刚结束的首轮融资中获得500万美元投资.由Signia Venture Partners领投,宝马i Venturesi.Ad ...