WPF控件--NotifyIcon

 

运行界面如下所示:

          

图1                                             图2

代码很少,如下所示:

 Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->
 using System;
 using System.Windows;
 using System.Windows.Forms;
 using System.Drawing;

 namespace Royen
 {
     public partial class SysTray : Window
     {
         private NotifyIcon notifyIcon=null;

         public SysTray()
         {
             InitializeComponent();

             InitialTray();
         }

         private void InitialTray()
         {
             //隐藏主窗体
             this.Visibility = Visibility.Hidden;

             //设置托盘的各个属性
             notifyIcon = new NotifyIcon();
             notifyIcon.BalloonTipText = "systray runnning...";
             notifyIcon.Text = "systray";
             notifyIcon.Icon = new System.Drawing.Icon("http://www.cnblogs.com/res/spring.ico");
             notifyIcon.Visible = true;
             notifyIcon.ShowBalloonTip();
             notifyIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(notifyIcon_MouseClick);

             //设置菜单项
             MenuItem setting1 = new MenuItem("setting1");
             MenuItem setting2 = new MenuItem("setting2");
             MenuItem setting = new MenuItem("setting", new MenuItem[]{setting1,setting2});

             //帮助选项
             MenuItem help = new MenuItem("help");

             //关于选项
             MenuItem about = new MenuItem("about");

             //退出菜单项
             MenuItem exit = new MenuItem("exit");
             exit.Click += new EventHandler(exit_Click);

             //关联托盘控件
             MenuItem[] childen = new MenuItem[] {setting,help,about,exit};
             notifyIcon.ContextMenu = new ContextMenu(childen);

             //窗体状态改变时候触发
             this.StateChanged += new EventHandler(SysTray_StateChanged);
         }

         /// <summary>
         /// 鼠标单击
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
         private void notifyIcon_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
         {
             //如果鼠标左键单击
             if (e.Button == MouseButtons.Left)
             {
                 if (this.Visibility == Visibility.Visible)
                 {
                     this.Visibility = Visibility.Hidden;
                 }
                 else
                 {
                     this.Visibility = Visibility.Visible;
                     this.Activate();
                 }
             }
         }

         /// <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("sure to exit?",
                                                "application",
                                                 MessageBoxButton.YesNo,
                                                 MessageBoxImage.Question,
                                                 MessageBoxResult.No) == MessageBoxResult.Yes)
             {
                 System.Windows.Application.Current.Shutdown();
             }
         }
     }
 }

工程源码下载:/Files/royenhome/Royen.rar 。真诚的希望和大家学习交流~

支持正版,版权来自 http://www.cnblogs.com/royenhome/archive/2010/02/02/1662243.html  园主:royen

WPF控件--利用Winform库中的NotifyIcon实现托盘小程序的更多相关文章

  1. 关于WinForm引用WPF窗体---在Winform窗体中使用WPF控件

    项目中有个界面展示用WPF实现起来比较简单,并且能提供更酷炫的效果,但是在WinForm中使用WPF窗体出现了问题,在网上找了一下有些人说Winform不能引用WPF的窗体,我就很纳闷,Win32都能 ...

  2. 在Winform窗体中使用WPF控件(附源码)

    原文:在Winform窗体中使用WPF控件(附源码) 今天是礼拜6,下雨,没有外出,闲暇就写一篇博文讲下如何在Winform中使用WPF控件.原有是我在百度上搜索相关信息无果,遂干脆动手自己实现. W ...

  3. 反爬虫:利用ASP.NET MVC的Filter和缓存(入坑出坑) C#中缓存的使用 C#操作redis WPF 控件库——可拖动选项卡的TabControl 【Bootstrap系列】详解Bootstrap-table AutoFac event 和delegate的分别 常见的异步方式async 和 await C# Task用法 c#源码的执行过程

    反爬虫:利用ASP.NET MVC的Filter和缓存(入坑出坑)   背景介绍: 为了平衡社区成员的贡献和索取,一起帮引入了帮帮币.当用户积分(帮帮点)达到一定数额之后,就会“掉落”一定数量的“帮帮 ...

  4. 在WinForm应用程序中嵌入WPF控件

    我们知道,在WPF界面上添加WinForm的控件需要使用WindowsFormHost类.而在WinForm界面上添加WPF控件该如何做呢?有没有类似的类呢?明显是有的,ElementHost就是为了 ...

  5. WPF 控件库——仿制Chrome的ColorPicker

    WPF 控件库系列博文地址: WPF 控件库——仿制Chrome的ColorPicker WPF 控件库——仿制Windows10的进度条 WPF 控件库——轮播控件 WPF 控件库——带有惯性的Sc ...

  6. 《Dotnet9》系列-开源C# WPF控件库2《Panuon.UI.Silver》强力推荐

    时间如流水,只能流去不流回! 点赞再看,养成习惯,这是您给我创作的动力! 本文 Dotnet9 https://dotnet9.com 已收录,站长乐于分享dotnet相关技术,比如Winform.W ...

  7. 《Dotnet9》系列-开源C# WPF控件库1《MaterialDesignInXAML》强力推荐

    时间如流水,只能流去不流回! 点赞再看,养成习惯,这是您给我创作的动力! 本文 Dotnet9 https://dotnet9.com 已收录,站长乐于分享dotnet相关技术,比如Winform.W ...

  8. WPF 精修篇 Winform 嵌入WPF控件

    原文:WPF 精修篇 Winform 嵌入WPF控件 首先 创建WPF控件库 这样就有了一个WPF界面 在wpf中增加界面等 在winform中增加WPFDLL 重新生成解决方案 在左侧工具栏 出现W ...

  9. [WPF]是时候将WPF控件库从.Net Framework升级到.NET Core 3.1

    1. 升级到Core的好处 去年中我曾考虑将我的控件库项目Kino.Toolkit.Wpf升级到.NET Core,不过很快放弃了,因为当时.NET Core是预览版,编译WPF还需要使用最新的Vis ...

随机推荐

  1. [转载]两个半小时学会Perl

    Learn Perl in about 2 hours 30 minutes By Sam Hughes Perl is a dynamic, dynamically-typed, high-leve ...

  2. LeetCode 3 Longest Substring Without Repeating Characters 解题报告

    LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...

  3. java如果读取xml内容

    本文介绍的是使用dom4j方式读取,如需要其他方式可自行百度. 1.首先导入dom4j的jar包:http://www.dom4j.org/dom4j-1.6.1/ 2.准备xml文件 <?xm ...

  4. leetcode-Spiral Matrix II 螺旋矩阵2之python大法好,四行就搞定,你敢信?

    Spiral Matrix II 螺旋矩阵 Given an integer n, generate a square matrix filled with elements from 1 to n2 ...

  5. php file_get_contents 绕过

    http://www.shiyanbar.com/ctf/1837 想到了经常出现的残留文件问题,于是尝试了一下:index.php~,index.php.bak, $flag='xxx';extra ...

  6. Android TextView中文字通过SpannableString来设置超链接、颜色、字体等属性

    在Android中,TextView是我们最常用的用来显示文本的控件. 一般情况下,TextView中的文本都是一个样式.那么如何对于TextView中各个部分的文本来设置字体,大小,颜色,样式,以及 ...

  7. Android优化—— Google 发布 Android 性能优化典范

    阅读目录 0)Render Performance 1)Understanding Overdraw 2)Understanding VSYNC 3)Tool:Profile GPU Renderin ...

  8. Sublime Text2 安装Package Control

    Sublime Text2是一款轻量级的妖娆的编辑器,想要更多私人定制功能的第一步就是安装Package  Control 这是官网的安装方法: Click the Preferences >  ...

  9. MySQL server has gone away的解决方法

    用Python写了一个http服务,需要从mysql读数据库,第一天还好好的,第二天突然不行了.报错如下: pymysql.err.OperationalError: (2006, 'MySQL se ...

  10. js对象的两种写法

    <script>     //定义一个对象,提供对应的方法或者属性     var s = {         sd1: function () { },         sd2: fun ...