以前在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. 20155307 实验一《Java开发环境的熟悉》实验报告

    (一)命令行下Java程序开发 题目:实现Fibonacci数列功能,并进行测试 命令行下的程序截图 上传到了码云: 遇到的问题? 可以直接使用Javac,不加环境变量也行,但是文件的名字与类名必须一 ...

  2. 20155325实验四 Android程序设计

    实验四 Android程序设计-1 Android Stuidio的安装测试: 参考<Java和Android开发学习指南(第二版)(EPUBIT,Java for Android 2nd)&g ...

  3. 实验一 Java开发环境的熟悉(Linux+Eclipse)

    实验一 Java开发环境的熟悉(Linux+Eclipse) 在Linux或Window或macOS中命令行下运行Java 在Linux 或Window或 macOS环境中 IDEA中调试设置断点 实 ...

  4. python 逆波兰式

    逆波兰式,也叫后缀表达式 技巧:为简化代码,引入一个不存在的运算符#,优先级最低.置于堆栈底部 class Stack(object): '''堆栈''' def __init__(self): se ...

  5. underscore.js 分析 第四天

    查看underscore包含多少属性和方法 通过阅读JavaScript 获取对象的键的数组 var a = _; var arr = Object.keys(a); console.log(arr) ...

  6. 你不需要jQuery You Don't Need jQuery

    转载:https://github.com/oneuijs/You-Dont-Need-jQuery/blob/master/README.zh-CN.md You Don't Need jQuery ...

  7. pager-taglib分页注意事项

    必须先导包,尤其是 jsp 这种工具类和标签库的

  8. MySQL授权root

    1. 改表法. 可能是你的帐号不允许从远程登陆,只能在localhost.这个时候只要在localhost的那台电脑,登入mysql后,更改 "mysql" 数据库里的 " ...

  9. 「Leetcode」14. Longest Common Prefix(Java)

    分析 与其说是算法题,不如说是语言特性题. 这题要是对Java的String相关函数掌握的比较熟练,写起来的速度(各种意义上)就会很快. 大致的思路都是一致的,差不到哪里去,无非是枚举长度.值得一提的 ...

  10. 180729-Quick-Task 动态脚本支持框架之任务动态加载

    Quick-Task 动态脚本支持框架之任务动态加载 前面几篇博文分别介绍了整个项目的基本架构,使用说明,以及整体框架的设计与实现初稿,接下来则进入更细节的实现篇,将整个工程中核心实现捞出来,从为什么 ...