WPF开发者QQ群: 340500857 | 微信群 -> 进入公众号主页 加入组织
欢迎转发、分享、点赞、在看,谢谢~。
01
—
效果预览
效果预览(更多效果请下载源码体验):
一、TaskbarItemInfoExample.xaml代码如下
<Window x:Class="WPFDevelopers.Samples.ExampleViews.TaskbarItemInfoExample"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:WPFDevelopers.Samples"Owner="{x:Static local:App.CurrentMainWindow}"WindowStartupLocation="CenterOwner" Title="TaskbarItemInfoExample"mc:Ignorable="d" Height="211" Width="363"d:DesignHeight="450" d:DesignWidth="800"><Window.TaskbarItemInfo><TaskbarItemInfo /></Window.TaskbarItemInfo><Window.Resources><DataTemplate x:Key="TaskbarIcon"><Border Width="20" Height="20"Background="{StaticResource PrimaryPressedSolidColorBrush}"BorderBrush="{StaticResource WhiteSolidColorBrush}" BorderThickness="2"><TextBlock Text="{Binding}" TextAlignment="Center" Foreground="White" VerticalAlignment="Center"/></Border></DataTemplate></Window.Resources><WrapPanel VerticalAlignment="Center" HorizontalAlignment="Center"><Button Content="增加" Style="{StaticResource PrimaryButton}" Height="30" Width="120" Click="Button_Click" Tag="add"/><Button Content="移除" Style="{StaticResource PrimaryButton}" Height="30" Width="120" Click="Button_Click" Tag="remove"/></WrapPanel>
</Window>
二、TaskbarItemInfoExample.xaml.cs 代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace WPFDevelopers.Samples.ExampleViews
{/// <summary>/// TaskbarItemInfoExample.xaml 的交互逻辑/// </summary>public partial class TaskbarItemInfoExample : Window{int iconWidth = 20;int iconHeight = 20;RenderTargetBitmap renderTargetBitmap;ContentControl contentControl;public TaskbarItemInfoExample(){InitializeComponent();this.Loaded += (s, e) =>{renderTargetBitmap = new RenderTargetBitmap(iconWidth, iconHeight, 96, 96, PixelFormats.Default);contentControl = new ContentControl();contentControl.ContentTemplate = ((DataTemplate)Resources["TaskbarIcon"]);};}private void Button_Click(object sender, RoutedEventArgs e){var btn = sender as Button;switch (btn.Tag.ToString()){case "add":CurrentTaskbarItemInfo("99");break;case "remove":TaskbarItemInfo.Overlay = null;break;default:break;}}void CurrentTaskbarItemInfo(string value){contentControl.Content = value;contentControl.Arrange(new Rect(0, 0, iconWidth, iconHeight));renderTargetBitmap.Render(contentControl);TaskbarItemInfo.Overlay = (ImageSource)renderTargetBitmap;}}
}
源码地址
github:https://github.com/yanjinhuagood/WPFDevelopers.git
gitee:https://gitee.com/yanjinhua/WPFDevelopers.git
WPF开发者QQ群: 340500857
blogs: https://www.cnblogs.com/yanjinhua
Github:https://github.com/yanjinhuagood
出处:https://www.cnblogs.com/yanjinhua
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
转载请著名作者 出处 https://github.com/yanjinhuagood