以前在winForm下使用过NotifyIcon,到wpf找不到了,在wpf下还是直接用WinForm里的那个NotifyIcon实现最小到系统托盘

定义一个NotifyIcon成员 :

NotifyIcon notifyIcon = null;
WindowState ws; //记录窗体状态

加载窗体时初始化NotifyIcon:

            this.notifyIcon = new NotifyIcon();
this.notifyIcon.BalloonTipText = "Hello, 文件监视器"; //设置程序启动时显示的文本
this.notifyIcon.Text = "文件监视器";//最小化到托盘时,鼠标点击时显示的文本
this.notifyIcon.Icon = new System.Drawing.Icon(@"b.ico");//程序图标
this.notifyIcon.Visible = true;
notifyIcon.MouseDoubleClick += OnNotifyIconDoubleClick;
this.notifyIcon.ShowBalloonTip();

同时添加右键菜单:

            System.Windows.Forms.MenuItem m1 = new System.Windows.Forms.MenuItem("open");
m1.Click += m1_Click;
System.Windows.Forms.MenuItem m2 = new System.Windows.Forms.MenuItem("close");
m2.Click += m2_Click;
System.Windows.Forms.MenuItem[] m = new System.Windows.Forms.MenuItem[] { m1, m2 };
this.notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(m);

事件为:

        void m2_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();
}
} void m1_Click(object sender, EventArgs e)
{
this.Show();
this.Activate();
}

对窗体添加事件:

            this.SizeChanged += MainWindow_SizeChanged;
this.Closing += MainWindow_Closing;
     void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e)
{
if (ws == WindowState.Minimized)
{
this.Hide();
this.notifyIcon.Visible = true; }
}
void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{ e.Cancel = true;
this.WindowState = WindowState.Minimized;
ws = WindowState.Minimized;
this.notifyIcon.Visible = true;
this.notifyIcon.ShowBalloonTip(, "注意", "大家好,这是一个事例", ToolTipIcon.Info);
}

双击拖盘弹出窗体

        private void OnNotifyIconDoubleClick(object sender, EventArgs e)
{
if (ws == WindowState.Minimized)
{
this.WindowState = WindowState.Normal;
this.notifyIcon.Visible = false;
}
}

好,大概这些知识够用了,最后来一个完整的代码:

  public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
icon();
//保证窗体显示在上方。
wsl = WindowState; this.SizeChanged += MainWindow_SizeChanged;
this.Closing += MainWindow_Closing;
}
void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e)
{
if (ws == WindowState.Minimized)
{
this.Hide();
this.notifyIcon.Visible = true; }
}
void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{ e.Cancel = true;
this.WindowState = WindowState.Minimized;
ws = WindowState.Minimized;
this.notifyIcon.Visible = true;
this.notifyIcon.ShowBalloonTip(, "注意", "大家好,这是一个事例", ToolTipIcon.Info);
} WindowState ws;
WindowState wsl; NotifyIcon notifyIcon = null;
private void icon()
{
this.notifyIcon = new NotifyIcon();
this.notifyIcon.BalloonTipText = "Hello, 文件监视器"; //设置程序启动时显示的文本
this.notifyIcon.Text = "文件监视器";//最小化到托盘时,鼠标点击时显示的文本
this.notifyIcon.Icon = new System.Drawing.Icon(@"b.ico");//程序图标
this.notifyIcon.Visible = true;
notifyIcon.MouseDoubleClick += OnNotifyIconDoubleClick;
this.notifyIcon.ShowBalloonTip(); System.Windows.Forms.MenuItem m1 = new System.Windows.Forms.MenuItem("open");
m1.Click += m1_Click;
System.Windows.Forms.MenuItem m2 = new System.Windows.Forms.MenuItem("close");
m2.Click += m2_Click;
System.Windows.Forms.MenuItem[] m = new System.Windows.Forms.MenuItem[] { m1, m2 };
this.notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(m);
} void m2_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();
}
} void m1_Click(object sender, EventArgs e)
{
this.Show();
this.Activate();
} private void OnNotifyIconDoubleClick(object sender, EventArgs e)
{
if (ws == WindowState.Minimized)
{
this.WindowState = WindowState.Normal;
this.notifyIcon.Visible = false;
}
} private void Window_StateChanged(object sender, EventArgs e)
{
ws = WindowState;
if (ws == WindowState.Minimized)
{
this.notifyIcon.Visible = true;
}
} private void Button_Click(object sender, RoutedEventArgs e)
{
this.notifyIcon.BalloonTipText = "有新信息";
}
}

wpf下使用NotifyIcon的更多相关文章

  1. WPF 学习笔记-在WPF下创建托盘图标

    原文:WPF 学习笔记-在WPF下创建托盘图标 首先需要在项目中引用System.Windows.Forms,System.Drawing; using System; using System.Co ...

  2. WinForm与WPF下跨线程调用控件

    Winform下: public delegate void UpadataTextCallBack(string str,TextBox text); public void UpadtaText( ...

  3. WPF下的视频录制界面设计

    原文:WPF下的视频录制界面设计 在去年12月份,我曾经写过三篇文章讨论C#下视频录制.播放界面的设计.这三篇文章是:利用C#画视频录制及播放的界面(一) 利用C#画视频录制及播放的界面(二)利用C# ...

  4. WPF 下两种图片合成或加水印的方式(转载)

    来源:http://www.cnblogs.com/lxblog/ 最近项目中应用多次应用了图片合成,为了今后方便特此记下. 在WPF下有两种图片合成的方式,一种还是用原来C#提供的GDI+方式,命名 ...

  5. WPF 下 label 的刷新

    WPF下,label控件并没有什么 Refresh() 的方法.那么现在问题就来了. 假设有这么个场景:WPF窗体上有一个按钮,一个Label,按下按钮,触发一些耗时的操作:在操作之前,Label显示 ...

  6. WPF下字体模糊的问题

    原文:WPF下字体模糊的问题 一直以来,发现WPF中的小字体下的文字变得比较模糊,比如: WPF与Winform字体显示比较: 为了看到更清楚,我们放大点显示:  放得更大些: 中文.日文等亚洲文字的 ...

  7. WPF下Itemscontrol分组 样式

    原文 WPF下Itemscontrol分组 样式 <ItemsControl Grid.Row="1" DataContext="{Binding Layouts} ...

  8. 一、从GitHub浏览Prism示例代码的方式入门WPF下的Prism

    最近这段时间一直在看一个开源软件PowerToys的源码,里面使用Modules的开发风格让我特别着迷,感觉比我现在写代码的风格好了太多太多.我尝试把PowerToys的架构分离了出来,但是发现代码维 ...

  9. WPF下如何使用TTF字体

    之前再写代码的时候如果遇到了图标,我都喜欢再资源文件下创建JPG或者PNG来作为图片. 但是随着TTF字体图标的普及,图标类型的的图片越来越多的被放入到TTF中. 这篇也主要是写再WPF下如何使用TT ...

随机推荐

  1. 2016-2017-2 《Java程序设计》第1周学习问题汇总

    201552-53 <Java程序设计>第一周问题汇总 关于软件安装以及配置中出现的问题,附上教程及讨论组网址: JDK/intelljIDEA安装及配置: http://www.cnbl ...

  2. # 20155224 实验四 Android程序设计

    20155224 实验四 Android程序设计 任务一 Android Stuidio的安装测试: 参考<Java和Android开发学习指南(第二版)(EPUBIT,Java for And ...

  3. 20145226夏艺华 《Java程序设计》实验报告五

    实验五 Java网络编程及安全 实验内容 运行下载的TCP代码,结对进行 利用加解密代码包,编译运行代码,结对进行 集成代码,加密后通过TCP发送 结对伙伴:20145203 马超 实验步骤 (一)中 ...

  4. Android:反编译apk

    一.所需工具 1. apktool (1)作用:获取资源文件,例如图片.布局文件 (2)下载地址:https://bitbucket.org/iBotPeaches/apktool/downloads ...

  5. Drupal views 学习之初识

    1. 简介 用过Yii框架的同学,应该都会用到过GridView和ListView组件.可以很方便的用网格或列表展示内容. 例如淘宝: 网格显示 列表显示 2. 使用view可以方便的配出类似上面的展 ...

  6. R的数据库访问-MySQL

    目录 1 RMySQL 2 环境与安装 3 建立通信 1 RMySQL R作为一款数据分析的工具,,而MySQL是一款常用的开源关系型数据库软件,非常适用于中小型的数据存储,当二者相互结合时才能爆发出 ...

  7. SQL基本的45题

    -- 查询Student表中的所有记录的Sname.Ssex和Class列.SELECT Sname,Ssex,Class from student -- 查询教师所有的单位即不重复的Depart列. ...

  8. 三、利用EnterpriseFrameWork快速开发Winform系统(C/S)

    EnterpriseFrameWork框架实例源代码下载: 实例下载 上一章讲解了开发Web系统的详细步骤,以书籍的管理作实例实现对书籍的增.删.改.查功能,本章接着上面的实例继续补充用Winform ...

  9. (转载)C#提取汉字拼音首字母的方法

    今天突然要用到提取汉字拼音首字母的功能,去网上找了找,发现没有几个好用的,决定自己写一个,效果还不错,发出来大家一起研究下,分享给大家!直接入主题: 1.首先对编码进行定义 #region 编码定义 ...

  10. maven 手动安装jar包

    1.问题 maven有时候在pom文件引入jar包会报错,所以可以通过手动导入jar包的方式导入. 2.解决: 通过maven命令导入jar包, mvn install:install-file -D ...