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 ...
随机推荐
- 20155216 2016-2017-2 《Java程序设计》第二周学习总结
教材学习内容总结 类型 short占2字节 int占4字节 long占8字节 byte占1字节,可表示-128~127的整数 char占2字节 boolean不考虑占字节 float占4字节 doub ...
- 虚拟机与Linux
VirtualBox与Ubuntu的下载 对于VirtualBox的下载,网络上的资源非常之多,并且软件也并不是很大,所以并没有耗费很多时间.但是对于Ubuntu的下载来说,一个操作系统,正版的下载肯 ...
- 20155235 2016-2017-2 《Java程序设计》第9周学习总结
20155235 2016-2017-2 <Java程序设计>第9周学习总结 教材学习内容总结 第十六章 整合数据库 JDBC入门 JDBC简介 连接数据库 使用Statement.Res ...
- BZOJ1597_土地购买_KEY
题目传送门 一道斜率优化的题目. 但暴力方程很关键. 我们先将x作为关键字Sort一遍,再将y处理成单调递减,即把无用的土地去除. f[i]=f[j]+a[i]*b[j+] -a[i]*b[j+]+f ...
- python 多线程笔记(4)-- 车站售票模拟
import threading import time import random class Worker(threading.Thread): '''售票员''' def __init__(se ...
- NLP的12条精髓
NLP是神经语言程序学 (Neuro-Linguistic Programming) 的英文缩写.一.没有两个人是一样的 No two persons are the same. 1.没有两个人的人生 ...
- Zigbee系列(end device)
End device设备分为睡眠和非睡眠两种(RxOnWhenIdle标记不同). 入网时的association请求,会使用这个标记. 共同特性 子节点多次发送数据失败(无回应),发送孤点扫描(re ...
- katalon系列十二:自动化上传文件、下载文件
一.下载文件1.下载文件时,需要先设置好Chrome/Firefox下载路径.不弹出下载框等,大家先学习下在selenium下如何设置:https://www.cnblogs.com/fnng/p/7 ...
- linux部署MantisBT(一)部署apache
一.部署apache 1.下载apache安装包及依赖包 http://httpd.apache.org/download.cgi#apache24(apache2)http://apr.apache ...
- php 操作 oracle lob 数据
http://www.oracle.com/technetwork/articles/fuecks-lobs-095315.html Working with LOBs in Oracle and P ...