以前在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. 20155229 2016-2017-2 《Java程序设计》第二周学习总结

    20155229 2016-2017-2 <Java程序设计>第二周学习总结 教材学习内容总结 布尔:boolean类型可表示true和false %符号被用来作为控制符号前置,所以规定用 ...

  2. 单线程的redis为什么吞吐量可以这么大

    原文:单线程的redis为什么吞吐量可以这么大 一.Redis的高并发和快速原因 1.redis是基于内存的,内存的读写速度非常快: 2.redis是单线程的,省去了很多上下文切换线程的时间: 3.r ...

  3. Centos安装man功能

    CentOS接触很久了,但是一直作为服务器端使用.这次之所以安装man,是由于开始学习Nginx了. 言归正传,安装man,首先得下载包.由于我们天朝的原因,代码包几乎下不到的.首先你得FQ,再下载, ...

  4. SaltStack入门篇(七)之架构部署实战

    模块:https://docs.saltstack.com/en/2016.11/ref/states/all/index.html 实战架构图: 实验环境设置: 主机名 IP地址 角色 linux- ...

  5. Training: MySQL I (MySQL, Exploit, Training)

    题目链接:http://www.wechall.net/challenge/training/mysql/auth_bypass1/index.php?highlight=christmas 的确是非 ...

  6. pycharm设置github

    1.打开file,选择settings,找到Version Contorl,打开找到GitHub ,HOST填github.com,用户名,密码,test,稍等一会,会提示成功 2. 设置好以后 打开 ...

  7. appium自动化环境搭建

    1.java开发环境JDK 2.android SDK(platform/platform tools/tools/build tools) 3.python下载安装(pip) 4.appium下载安 ...

  8. JavaScript学习笔记(五)——类型、转换、相等、字符串

    第六章 类型 相等 转换等 一.类型 1 typeof(); typeof是一个内置的JavaScript运算符,可用于探测其操作数的类型. 例: <script language=" ...

  9. 广东ACM省赛 E题

    题意: 输入一个P 使得存在一个一个N大于等于P, 并且存在m 等于 m/n * (m-1)/(n-1)=1/2. 思路 此题可以利用佩尔方程求解, 也可以打表解决.本次我解决利用的是佩尔方程(其实也 ...

  10. HTML/CSS的基本使用

    本篇博客主要介绍一下HTML/CSS的基本使用,关于它们的介绍便不在赘述,读者可自行google或百度. 一.HTML 先来简单介绍一下HTML标签: HTML 标签是由尖括号包围的关键词,比如 &l ...