首先需要引用  System.Windows;  System.Drawing;

  1. public partial class MainWindow : Window
  2. {
  3. public MainWindow()
  4. {
  5. InitializeComponent();
  6. InitialTray();
  7. }
  8. private System.Windows.Forms.NotifyIcon notifyIcon = null;
  9. private void InitialTray()
  10. {
  11.  
  12. //设置托盘的各个属性
  13. notifyIcon = new System.Windows.Forms.NotifyIcon();
  14. notifyIcon.BalloonTipText = "程序开始运行";
  15. notifyIcon.Text = "托盘图标";
  16. notifyIcon.Icon = new System.Drawing.Icon("NotifyIcon.ico");
  17. notifyIcon.Visible = true;
  18. notifyIcon.ShowBalloonTip();
  19. notifyIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(notifyIcon_MouseClick);
  20.  
  21. //设置菜单项
  22. System.Windows.Forms.MenuItem menu1 = new System.Windows.Forms.MenuItem("菜单项1");
  23. System.Windows.Forms.MenuItem menu2 = new System.Windows.Forms.MenuItem("菜单项2");
  24. System.Windows.Forms.MenuItem menu = new System.Windows.Forms.MenuItem("菜单", new System.Windows.Forms.MenuItem[] { menu1 , menu2 });
  25.  
  26. //退出菜单项
  27. System.Windows.Forms.MenuItem exit = new System.Windows.Forms.MenuItem("exit");
  28. exit.Click += new EventHandler(exit_Click);
  29.  
  30. //关联托盘控件
  31. System.Windows.Forms.MenuItem[] childen = new System.Windows.Forms.MenuItem[] { menu , exit };
  32. notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(childen);
  33.  
  34. //窗体状态改变时候触发
  35. this.StateChanged += new EventHandler(SysTray_StateChanged);
  36. }
  37. /// <summary>
  38. /// 窗体状态改变时候触发
  39. /// </summary>
  40. /// <param name="sender"></param>
  41. /// <param name="e"></param>
  42. private void SysTray_StateChanged(object sender, EventArgs e)
  43. {
  44. if (this.WindowState == WindowState.Minimized)
  45. {
  46.  
  47. //this.Visibility = Visibility.Hidden;
  48. }
  49. }
  50.  
  51. /// <summary>
  52. /// 退出选项
  53. /// </summary>
  54. /// <param name="sender"></param>
  55. /// <param name="e"></param>
  56.  
  57. private void exit_Click(object sender, EventArgs e)
  58. {
  59. if (System.Windows.MessageBox.Show("确定要关闭吗?", "退出", MessageBoxButton.YesNo,MessageBoxImage.Question,MessageBoxResult.No) == MessageBoxResult.Yes)
  60. {
  61. notifyIcon.Dispose();
  62. System.Windows.Application.Current.Shutdown();
  63. }
  64. }
  65.  
  66. /// <summary>
  67. /// 鼠标单击
  68. /// </summary>
  69. /// <param name="sender"></param>
  70. /// <param name="e"></param>
  71.  
  72. private void notifyIcon_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
  73. {
  74. if (e.Button == System.Windows.Forms.MouseButtons.Left)
  75. {
  76. if (this.Visibility == Visibility.Visible)
  77. {
  78. //this.Visibility = Visibility.Hidden;
  79. }
  80. else
  81. {
  82. this.Visibility = Visibility.Visible;
  83. this.WindowState = WindowState.Normal;
  84. //this.Activate();
  85. }
  86. }
  87. }
  88.     // 改变窗体关闭按钮事件
  89. private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  90. {
  91. e.Cancel = true;//取消窗体关闭
  92. this.Visibility = Visibility.Hidden;//窗体隐藏
  93. }
  94. }

WPF 程序启动显示为通知区域的图标方法的更多相关文章

  1. wpf 程序启动显示图片

    一.设置图片的生成操作 程序启动时会出现0.5秒的图片显示,再显示程序界面. 二.写代码实现相同效果 /// <summary> /// App.xaml 的交互逻辑 /// </s ...

  2. Win7通知区域的图标怎么去除?

    由于本人有洁癖,最近在用win7的时候,很收不了已经卸载了的一些软件,在win7右下角的通知区域图标中还留有痕迹,于是上网查找了下解决方案. 用以下方法完美解决问题. 这里依然是以注册表的修改方法为主 ...

  3. 关于WPF程序启动性能

    项目里WPF的启动时间太久(>1分钟),显然是不能接受的.超过10秒,连Loading的等待框都会让用户感到厌烦. 1. 症状 项目的结构是1个WPF主进程,启动3个WPF子进程.子进程在启动时 ...

  4. WPF 程序中启动和关闭外部.exe程序

    当需要在WPF程序启动时,启动另一外部程序(.exe程序)时,可以按照下面的例子来: C#后台代码如下: using System; using System.Collections.Generic; ...

  5. 处理win7任务栏通知区域图标异常问题

    故障现象:安装的某软件比如QQ,应用程序运行图标始终没有在win7任务栏通知区域显示出来,经观查发现win7任务栏通知区域有几个已删除应用的图标出现,应该是有故障了. 故障现象一:已经卸载的程序,还在 ...

  6. WPF程序在Windows 7下应用Windows 8主题

    这篇博客介绍如何在Windows 7下应用Windows 8的主题. 首先我们先看一个很常见的场景,同样的WPF程序(样式未重写)在不同的操作系统上展示会有些不同.这是为什么呢?WPF程序启动时会加载 ...

  7. PreApplicationStartMethodAttribute程序启动扩展

    一.PreApplicationStartMethodAttribute 类简介 应用程序启动时提供的扩展的支持. 命名空间:   System.Web程序集:  System.Web(位于 Syst ...

  8. WPF ListView 居中显示

    原文:WPF ListView 居中显示 今天遇到的问题: 方法1:设置GridViewColumn的ActualWidth <ListView > <ListView.View&g ...

  9. Win10任务栏通知区域上已卸载程序无效图标选项如何清除?

    在Win10系统中,大部分用户都已经知道在“选择在任务栏上显示哪些图标”来让一些软年图标显示,一些隐藏,不过使用Win10系统久了之后发现,在设置通知区域图标中有很多已经卸载程序的无效选项!这让设置时 ...

随机推荐

  1. 关于移动端1px边框问题

    <div class="z_nei_list"> <div class="z_name_left font-size3">身份证号:&l ...

  2. android如何实现文件按时间先后顺序排列显示

    <span style="font-size:18px;">File[] files =parentFile.listFiles(fileFilter);//通过fil ...

  3. WPF点补间、拟合回归直线

    1,path画刷,绘制正弦 点,线: 生成正弦点 profilePoint.Value = * ( - Math.Sin(i * Math.PI / )); profilePoint.Type = ; ...

  4. log4j.xml 日志只输出指定类配置

    1.日志增加appender 指定日志生成时间.格式.间隔时间. 2.category指定哪些或哪个类日志生成在文件中. 3.自定义logger避免不要将userBehavior定义到root中避免继 ...

  5. Codeforce 493c

    H - Vasya and Basketball Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  6. Git Commands

    Show ssh key file: ssh -v git@github.com

  7. 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 ...

  8. Java classpath 如何自动添加web-content /lib下的jar包

    右键属性--Java build path--Libraries--add library--Web app libraries -- next --next -- ok

  9. Mybatis Generator insert useGeneratedKeys keyProperty

    Mybatis自动生成代码,需要用到mybatis Generator,详见http://mybatis.github.io/generator/configreference/generatedKe ...

  10. 拓展企业VR培训业务,这家VR训练公司StriVR完成500万美元融资!

    虚拟现实初创公司StriVR最近发布了新的企业VR训练产品项目,并宣布在刚刚结束的首轮融资中获得500万美元投资.由Signia Venture Partners领投,宝马i Venturesi.Ad ...