wpf下使用NotifyIcon
以前在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的更多相关文章
- WPF 学习笔记-在WPF下创建托盘图标
原文:WPF 学习笔记-在WPF下创建托盘图标 首先需要在项目中引用System.Windows.Forms,System.Drawing; using System; using System.Co ...
- WinForm与WPF下跨线程调用控件
Winform下: public delegate void UpadataTextCallBack(string str,TextBox text); public void UpadtaText( ...
- WPF下的视频录制界面设计
原文:WPF下的视频录制界面设计 在去年12月份,我曾经写过三篇文章讨论C#下视频录制.播放界面的设计.这三篇文章是:利用C#画视频录制及播放的界面(一) 利用C#画视频录制及播放的界面(二)利用C# ...
- WPF 下两种图片合成或加水印的方式(转载)
来源:http://www.cnblogs.com/lxblog/ 最近项目中应用多次应用了图片合成,为了今后方便特此记下. 在WPF下有两种图片合成的方式,一种还是用原来C#提供的GDI+方式,命名 ...
- WPF 下 label 的刷新
WPF下,label控件并没有什么 Refresh() 的方法.那么现在问题就来了. 假设有这么个场景:WPF窗体上有一个按钮,一个Label,按下按钮,触发一些耗时的操作:在操作之前,Label显示 ...
- WPF下字体模糊的问题
原文:WPF下字体模糊的问题 一直以来,发现WPF中的小字体下的文字变得比较模糊,比如: WPF与Winform字体显示比较: 为了看到更清楚,我们放大点显示: 放得更大些: 中文.日文等亚洲文字的 ...
- WPF下Itemscontrol分组 样式
原文 WPF下Itemscontrol分组 样式 <ItemsControl Grid.Row="1" DataContext="{Binding Layouts} ...
- 一、从GitHub浏览Prism示例代码的方式入门WPF下的Prism
最近这段时间一直在看一个开源软件PowerToys的源码,里面使用Modules的开发风格让我特别着迷,感觉比我现在写代码的风格好了太多太多.我尝试把PowerToys的架构分离了出来,但是发现代码维 ...
- WPF下如何使用TTF字体
之前再写代码的时候如果遇到了图标,我都喜欢再资源文件下创建JPG或者PNG来作为图片. 但是随着TTF字体图标的普及,图标类型的的图片越来越多的被放入到TTF中. 这篇也主要是写再WPF下如何使用TT ...
随机推荐
- 20155211 《Java程序设计》实验一(Java开发环境的熟悉)实验报告
20155211 <Java程序设计>实验一(Java开发环境的熟悉)实验报告 一.实验内容及步骤 (一)使用JDK编译.运行简单的java程序 命令行下的程序开发 步骤一(新建文件夹): ...
- 20155213 《JAVA程序设计》实验二(JAVA面向对象程序设计)实验报告
20155213 <JAVA程序设计>实验二(JAVA面向对象程序设计)实验报告 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S ...
- 20155313 实验一《Java开发环境的熟悉》实验报告
一.实验内容 1.使用JDK编译.运行简单的Java程序 2.使用IDEA 编辑.编译.运行.调试Java程序. 二.练习 题目:实现学生信息管理. 具体代码: import java.util.*; ...
- 20155332 2016-2017-2 《Java程序设计》实验一 Java开发环境的熟悉
实验内容 使用JDK编译.运行简单的Java程序: 使用IDEA 编辑.编译.运行.调试Java程序. 实验知识点 JVM.JRE.JDK的安装位置与区别: 命令行运行javac:java:javac ...
- 4709: [Jsoi2011]柠檬
4709: [Jsoi2011]柠檬 https://www.lydsy.com/JudgeOnline/problem.php?id=4709 分析: 决策单调性+栈+二分. 首先挖掘性质:每个段选 ...
- Scrapy爬取美女图片续集 (原创)
上一篇咱们讲解了Scrapy的工作机制和如何使用Scrapy爬取美女图片,而今天接着讲解Scrapy爬取美女图片,不过采取了不同的方式和代码实现,对Scrapy的功能进行更深入的运用.(我的新书< ...
- 我们一起学习WCF 第七篇会话模式
会话:就是客户端和服务端之间的谈话.比喻A和B去登陆网站,那么A用户登陆进去肯定显示A的用户详情,那么这就是A和服务器之间的交流.同样B用户登陆之后显示B的详情,这就表示这是B和服务器之间的交流. 如 ...
- python-面向对象-内置方法补充
__del__item系列 __getitem__ __setitem__ __delitem____hash____eq__ 构造方法 申请一个空间析构方法 释放一个空间之前执行某对象借用了操作系统 ...
- JavaScript(js)处理的HTML事件、键盘事件、鼠标事件
示例代码: HTML文件: <!DOCTYPE html><html lang="en"><head> <meta charset=&qu ...
- EasyUI 效果还不错的数据处理等待效果
$("#form").form("submit",{ url:url, onSubmit: function(){ parent.$.messager.prog ...