title author date CreateTime categories
WPF 托盘显示
lindexi
2019-06-23 11:52:36 +0800
2018-11-21 11:19:33 +0800
WPF

本文告诉大家如何在 WPF 实现在托盘显示,同时托盘可以右击打开菜单,双击执行指定的代码

NotifyIcon WPF

通过 Nuget 安装 Hardcodet.NotifyIcon.Wpf 可以快速做到在 WPF 显示托盘

因为托盘是程序的托盘,不是窗口的,所以推荐代码是写在 App.xaml.cs 里面

先创建一个托盘的界面,界面在 App.xaml 创建

托盘是需要图标的,可以从 Iconfont-阿里巴巴矢量图标库 找到好看的图标。在托盘图标需要是 16x16 32位 的 ico 文件

将图片下载放在解决方案,修改为 Resource 就可以

在 App.xaml 定义资源 TaskbarIcon 请看代码

<Application x:Class="HouneaLeabeltezairKayballjachall.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:HouneaLeabeltezairKayballjachall"
xmlns:tb="http://www.hardcodet.net/taskbar"
StartupUri="MainWindow.xaml">
<Application.Resources>
<tb:TaskbarIcon x:Key="Taskbar"
IconSource="Taskbar.ico"> </tb:TaskbarIcon>
</Application.Resources>
</Application>

上面代码需要替换 Class="HouneaLeabeltezairKayballjachall.App" 为自己的项目

打开 App.xaml.cs 获取资源,资源只有在获取的时候才会创建,创建了 TaskbarIcon 就会在托盘显示

        protected override void OnStartup(StartupEventArgs e)
{
_taskbar = (TaskbarIcon) FindResource("Taskbar");
base.OnStartup(e);
} private TaskbarIcon _taskbar;

运行代码可以看到图片显示图标,下面的图片是我的图标

鼠标移动在托盘上面显示文字,可以在 TaskbarIcon 添加代码

        <tb:TaskbarIcon x:Key="Taskbar"
ToolTipText="鼠标移动上显示的文字"
IconSource="Taskbar.ico"> </tb:TaskbarIcon>

双击托盘运行代码需要添加命令,创建一个 ViewModel 用来绑定命令

    public class DelegateCommand : ICommand
{
public Action CommandAction { get; set; }
public Func<bool> CanExecuteFunc { get; set; } public void Execute(object parameter)
{
CommandAction();
} public bool CanExecute(object parameter)
{
return CanExecuteFunc == null || CanExecuteFunc();
} public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
}

添加一些功能,显示窗口,隐藏窗口

    public class NotifyIconViewModel
{
/// <summary>
/// 如果窗口没显示,就显示窗口
/// </summary>
public ICommand ShowWindowCommand
{
get
{
return new DelegateCommand
{
CanExecuteFunc = () => Application.Current.MainWindow == null,
CommandAction = () =>
{
Application.Current.MainWindow = new MainWindow();
Application.Current.MainWindow.Show();
}
};
}
} /// <summary>
/// 隐藏窗口
/// </summary>
public ICommand HideWindowCommand
{
get
{
return new DelegateCommand
{
CommandAction = () => Application.Current.MainWindow.Close(),
CanExecuteFunc = () => Application.Current.MainWindow != null
};
}
} /// <summary>
/// 关闭软件
/// </summary>
public ICommand ExitApplicationCommand
{
get
{
return new DelegateCommand { CommandAction = () => Application.Current.Shutdown() };
}
}
}

在界面绑定代码

        <tb:TaskbarIcon x:Key="Taskbar"
ToolTipText="鼠标移动上显示的文字"
DoubleClickCommand="{Binding ShowWindowCommand}"
IconSource="Taskbar.ico"> </tb:TaskbarIcon>

这时的 TaskbarIcon 还没有 ViewModel 可以通过下面的方式

        <tb:TaskbarIcon x:Key="Taskbar"
ToolTipText="鼠标移动上显示的文字"
DoubleClickCommand="{Binding ShowWindowCommand}"
IconSource="Taskbar.ico">
<tb:TaskbarIcon.DataContext>
<local:NotifyIconViewModel />
</tb:TaskbarIcon.DataContext>
</tb:TaskbarIcon>

现在双击就可以打开 MainWindow 因为默认 MainWindow 是打开的,比较难看到双击打开。在 App.xaml 去掉默认打开 MainWindow 需要找到下面的代码

StartupUri="MainWindow.xaml"

现在尝试不让默认打开 MainWindow 运行软件,可以看到托盘显示图标,双击图标才可以打开界面

如果要右击显示菜单,需要在 App.xaml 添加定义

        <ContextMenu x:Shared="false" x:Key="SysTrayMenu">
<MenuItem Header="显示窗口" Command="{Binding ShowWindowCommand}" />
<MenuItem Header="关闭窗口" Command="{Binding HideWindowCommand}" />
<Separator />
<MenuItem Header="退出" Command="{Binding ExitApplicationCommand}" />
</ContextMenu>

在 TaskbarIcon 使用菜单

        <tb:TaskbarIcon x:Key="Taskbar"
ContextMenu="{StaticResource SysTrayMenu}"
ToolTipText="鼠标移动上显示的文字"
DoubleClickCommand="{Binding ShowWindowCommand}"
IconSource="Taskbar.ico">
<tb:TaskbarIcon.DataContext>
<local:NotifyIconViewModel />
</tb:TaskbarIcon.DataContext>
</tb:TaskbarIcon>

因为在 ViewModel 已经写好代码,所以现在就可以运行

默认的软件设置是关闭最后一个窗口的时候应用就关闭,通过设置 App.ShutdownMode 可以在调用退出才关闭,打开 App.xaml 添加代码

 ShutdownMode="OnExplicitShutdown"

本文的代码

WPF 托盘显示 右击打开菜单,双击打开软件-CSDN下载

下面还有一些高级的使用

定义托盘鼠标移动上去的文字颜色

            <tb:TaskbarIcon.TrayToolTip>
里面可以添加控件
</tb:TaskbarIcon.TrayToolTip>
        <tb:TaskbarIcon x:Key="Taskbar"
ContextMenu="{StaticResource SysTrayMenu}"
DoubleClickCommand="{Binding ShowWindowCommand}"
IconSource="Taskbar.ico">
<tb:TaskbarIcon.TrayToolTip>
<Border
Background="White"
BorderBrush="Orange"
BorderThickness="2"
CornerRadius="4"
Opacity="0.8"
Width="160"
Height="40">
<TextBlock
Text="hello world"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Border>
</tb:TaskbarIcon.TrayToolTip>
<tb:TaskbarIcon.DataContext>
<local:NotifyIconViewModel />
</tb:TaskbarIcon.DataContext>
</tb:TaskbarIcon>

因为可以添加控件,发挥你的想法,如添加按钮

        <tb:TaskbarIcon x:Key="Taskbar"
ContextMenu="{StaticResource SysTrayMenu}"
DoubleClickCommand="{Binding ShowWindowCommand}"
IconSource="Taskbar.ico">
<tb:TaskbarIcon.TrayToolTip>
<Border
Background="White"
BorderBrush="Orange"
BorderThickness="2"
CornerRadius="4"
Opacity="0.8"
Width="160">
<Grid>
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="可以放文字"></TextBlock>
<Button Margin="10,100,10,10" Content="可以放按钮"></Button>
</Grid>
</Border>
</tb:TaskbarIcon.TrayToolTip>
<tb:TaskbarIcon.DataContext>
<local:NotifyIconViewModel />
</tb:TaskbarIcon.DataContext>
</tb:TaskbarIcon>

显示气泡

通过下面的代码可以显示气泡

Taskbar.ShowBalloonTip("标题", "内容", BalloonIcon.Info);

如果需要自定义气泡,定义一个继承 UIElement 的类,然后传入 TaskbarIcon.ShowCustomBalloon 就可以

如已经定义了 FancyBalloon 气泡,可以通过下面的代码

 FancyBalloon balloon = new FancyBalloon();
TaskbarIcon.ShowCustomBalloon(balloon, PopupAnimation.Slide, 4000 /*多少时间就隐藏*/);

代码:NotifyIcon WPF — Bitbucket

2019-6-23-WPF-托盘显示的更多相关文章

  1. WPF 托盘显示

    本文告诉大家如何在 WPF 实现在托盘显示,同时托盘可以右击打开菜单,双击执行指定的代码 NotifyIcon WPF 通过 Nuget 安装 Hardcodet.NotifyIcon.Wpf 可以快 ...

  2. 在WPF中显示动态GIF

    在我们寻求帮助的时候,最不愿意听到的答复是:很抱歉,在当前版本的产品中还没有实现该功能... 在WPF中显示动态的GIF图像时便遇到了这样的问题,WPF中强大的Image控件却不支持动态的GIF(其只 ...

  3. WPF 图片显示中的保留字符问题

    在WPF中显示一张图片,本是一件再简单不过的事情.一张图片,一行XAML代码即可. 但是前段时间遇到了一件奇怪的事: 开发机上运行正常的程序,在某些客户机器上却显示不了图片,而且除了这个问题,其它运行 ...

  4. C# winForm程序开机启动和托盘显示 (转http://blog.csdn.net/xinyue3054/article/details/6599508)

    这段时间一直进行cs项目,故整理下开机自动运行.托盘.显示.隐藏. (1).在窗口上点击关闭按钮或者最小化时将托盘显示: (2).双击托盘图标显示窗口: (3).右键点击托盘图标提供三个菜单选项,“退 ...

  5. C# 开机自启动和最小化托盘显示

    C# 开机自启动和最小化托盘显示 一.      C# 开机自启动 C# 开机自启动,这个功能是大多数服务型软件很常用一个功能,但是这个功能确是不 太好做,花了两天想对策.不过最终呢,结果还是很满意的 ...

  6. Winform设置托盘程序,托盘显示

    1.拖一个NotifyIcon,一个ContextMenuStrip控件到主窗体中 2.设置notifyIcon1,一个contextMenuStrip1(如下图) Icon为托盘图标,Text托盘显 ...

  7. WPF 循环显示列表

    原文:WPF 循环显示列表 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/SANYUNI/article/details/79423707 项目需要 ...

  8. WPF 窗体显示最前端

    原文:WPF 窗体显示最前端 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/jjx0224/article/details/8782845 如何做一 ...

  9. Beta冲刺(2/7)——2019.5.23

    所属课程 软件工程1916|W(福州大学) 作业要求 Beta冲刺(2/7)--2019.5.23 团队名称 待就业六人组 1.团队信息 团队名称:待就业六人组 团队描述:同舟共济扬帆起,乘风破浪万里 ...

  10. WPF 远程显示原图 当前主页面 工具栏 一个Window页面的元素适用一个效果

    http://www.jb51.net/article/98384.htm 1.wpf远程显示原图: Stretch="Fill" + ; 主要是因为那个950和650,据显示位置 ...

随机推荐

  1. MySQL WAL

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11447794.html WAL: Write-Ahead Logging 先写日志,再写磁盘.具体说, ...

  2. JavaWeb(七):EL表达式、自定义标签和JSTL

    一.EL表达式 语法 el.jsp <%@page import="java.util.Date"%> <%@page import="com.atgu ...

  3. 【leetcode】1091. Shortest Path in Binary Matrix

    题目如下: In an N by N square grid, each cell is either empty (0) or blocked (1). A clear path from top- ...

  4. 【leetcode】1029. Two City Scheduling

    题目如下: There are 2N people a company is planning to interview. The cost of flying the i-th person to ...

  5. 对getBoundingClientRect属性的研究

    1.getBoundingClientRect用于获取某个元素相对于视窗的位置集合.集合中有top, right, bottom, left等属性. .top:元素上边到视窗上边的距离; right: ...

  6. 父工程 pom版本

    <!-- 集中定义依赖版本号 --> <properties> <junit.version>4.12</junit.version> <spri ...

  7. Jmeter的JDBC请求执行多条SQL语句

    注:有mysqlconnector/j 3.1.1以上版本才支持执行多条sql语句 1.     下载jdbc驱动为了连接Mysql数据库,还需要有个jdbc驱动:mysql-connector-ja ...

  8. ueditor 复制word里面带图文的文章,图片可以直接显示

    图片的复制无非有两种方法,一种是图片直接上传到服务器,另外一种转换成二进制流的base64码 目前限chrome浏览器使用,但是项目要求需要支持所有的浏览器,包括Windows和macOS系统.没有办 ...

  9. Cluster基础(五):配置tracker、配置storage、文件测试及web访问

    一.配置tracker 目标: FastDFS是一个分布式文件系统,主要的服务器角色有Tracker和Storage.本例安装一台Tracker,实现以下功能: 接受客户端的访问 检索存储节点,为客户 ...

  10. 30 August

    DP 复习. 参考 redbag 博客 提供的题表. P2858 [USACO06FEB] Treats for the Cows 区间 DP. 转换思路,题面从外往里递推,我们采用从里往外递推,权值 ...