WPF 实现任务栏角徽

 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值