WPF高级任务栏

WPF高级任务栏

基本使用

因为任务栏是程序级别的,所以要在App.xaml中进行设置

<Application x:Class="WpfApp10.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApp10"
             StartupUri="MainWindow.xaml">
    <JumpList.JumpList>
        <JumpList
            ShowFrequentCategory="True" 
            ShowRecentCategory="True" 
            JumpItemsRejected="JumpList_JumpItemsRejected" 
            JumpItemsRemovedByUser="JumpList_JumpItemsRemovedByUser">
            <JumpTask
                Title="标题1"
                ApplicationPath="C:\Windows\notepad.exe"
                Arguments="readme.txt"
                CustomCategory="组别"
                Description="描述"
                IconResourceIndex="14" 
                IconResourcePath="C:\Windows\System32\imageres.dll"
                WorkingDirectory="C:\Users\54302\Desktop" />
            <JumpTask
                Title="标题2"
                ApplicationPath="C:\Windows\notepad.exe"
                Arguments="readme.txt"
                CustomCategory="DEMO 2"
                Description="Open readme.txt in Notepad."
                IconResourceIndex="15"
                IconResourcePath="C:\Windows\System32\imageres.dll"
                WorkingDirectory="C:\Users\54302\Desktop" />
        </JumpList>
    </JumpList.JumpList>
</Application>

在任务栏上右击

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-AjyUJNVx-1669516778387)(C:\Users\54302\Desktop\md笔记\WPF高级任务栏.assets\image-20221126133400742.png)]

同样也可以使用C#来实现,在主窗口中设置

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    JumpList jumpList = new JumpList();

    JumpTask jumpTask1 = new JumpTask();
    jumpTask1.ApplicationPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.SystemX86), "calc.exe");
    jumpTask1.IconResourcePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.SystemX86), "calc.exe");
    jumpTask1.Title = "Calculator";
    jumpTask1.Description = "Open Calculator.";
    jumpTask1.CustomCategory = "User Added Tasks";
    jumpList.JumpItems.Add(jumpTask1);

    JumpTask jumpTask2 = new JumpTask();
    jumpTask2.ApplicationPath = @"C:\Windows\notepad.exe";
    jumpTask2.Arguments = "readme.txt";
    jumpTask2.CustomCategory = "User Added Tasks";
    jumpTask2.Description = "Open readme.txt in Notepad.";
    jumpTask2.IconResourcePath = @"C:\Windows\System32\imageres.dll";
    jumpTask2.IconResourceIndex = 14;
    jumpTask2.WorkingDirectory = @"C:\Users\Public\Documents";
    jumpTask2.Title = "Open Notepad";
    jumpList.JumpItems.Add(jumpTask2);

    jumpList.Apply();
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-aS6YHNDe-1669516778388)(C:\Users\54302\Desktop\md笔记\WPF高级任务栏.assets\image-20221126134029568.png)]

动态添加多个项

private void Btn_Click(object sender, RoutedEventArgs e)
{
    //获取已有的jmpList
    JumpList jumpList = JumpList.GetJumpList(Application.Current);
    if (jumpList == null)
    {
        //如果还没有,则创建
        jumpList = new JumpList();
    }

    JumpTask jumpTask2 = new JumpTask();
    jumpTask2.ApplicationPath = @"C:\Windows\notepad.exe";
    jumpTask2.Arguments = "readme.txt";
    jumpTask2.CustomCategory = "User Added Tasks";
    jumpTask2.Description = "Open readme.txt in Notepad.";
    jumpTask2.IconResourcePath = @"C:\Windows\System32\imageres.dll";
    jumpTask2.IconResourceIndex = 14;
    jumpTask2.WorkingDirectory = @"C:\Users\Public\Documents";
    jumpTask2.Title = "打开记事本";
    jumpList.JumpItems.Add(jumpTask2);

    jumpList.Apply();
    //保存,下次打开软件还会存在
    JumpList.SetJumpList(Application.Current, jumpList);
}

上面代码关闭软件后上次加入的列表还会存在,但是在点击新增后,之前添加的列表项会被清空,原因在于jumpList = new JumpList();这句代码。可以将jumpList进行序列化进行存储,每次打开存储的文件。

任务栏进度条

<Window x:Class="WpfApp10.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp10"
        mc:Ignorable="d"
        Title="MainWindow" Height="201.22" Width="300" >
    <Window.TaskbarItemInfo>
        <TaskbarItemInfo x:Name="taskBarInfo"/>
    </Window.TaskbarItemInfo>
    <UniformGrid Columns="3">
        <Button x:Name="btn" Content="开始" Click="Btn_Click" Width="60" Height="40" />
        <Button x:Name="pause" Content="暂停" Click="Pause_Click" Width="60" Height="40" />
        <Button x:Name="cancel" Content="取消" Click="Cancel_Click" Width="60" Height="40"/>
    </UniformGrid>
</Window>
DispatcherTimer dispatcherTimer;

private void Start_Click(object sender, RoutedEventArgs e)
        {
            // taskBarInfo.ProgressState = TaskbarItemProgressState.Normal;
            //Process();
            //  taskBarInfo.ProgressState = TaskbarItemProgressState.Error;
            taskBarInfo.ProgressState = TaskbarItemProgressState.Paused;

        }
private void Process()
{
    dispatcherTimer = new DispatcherTimer();
    dispatcherTimer.Interval = TimeSpan.FromSeconds(0.1);
    dispatcherTimer.Tick += DispatcherTimer_Tick;
    dispatcherTimer.Start();
}

int i = 1;
private void DispatcherTimer_Tick(object sender, EventArgs e)
{
    taskBarInfo.ProgressValue = i++ / 10.0;
}

private void Pause_Click(object sender, RoutedEventArgs e)
{
    dispatcherTimer.Stop();
    taskBarInfo.ProgressState = TaskbarItemProgressState.Paused;

}

private void Cancel_Click(object sender, RoutedEventArgs e)
{
    taskBarInfo.ProgressState = TaskbarItemProgressState.Indeterminate;
}

private void Btn_Click(object sender, RoutedEventArgs e)
{
    taskBarInfo.ProgressState = TaskbarItemProgressState.Normal;
     Process();
}

在这里插入图片描述

任务栏重叠图标

在已有任务栏图标上加上下图像,如开始暂停,来描述不同的状态。

taskBarItem.Overlay = new BitmapImage( new Uri("play.png"));

或者在xaml中设置Overlay<TaskbarItemInfo x:Name="taskBarInfo" Overlay="icon.png" >

本人在Win10下测试,没有效果,有知道原因的小伙伴在评论区说一下

缩略图按钮

鼠标悬浮在任务栏上,提供一种快速控制的方式。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9eMtxsZt-1669516778390)(C:\Users\54302\Desktop\md笔记\WPF高级任务栏.assets\image-20221127102650957.png)]

<Window.TaskbarItemInfo>
    <TaskbarItemInfo x:Name="taskBarItem">
        <TaskbarItemInfo.ThumbButtonInfos>
            <ThumbButtonInfo ImageSource="play.png" Description="Play"
 Click="cmdPlay_Click"></ThumbButtonInfo>
            <ThumbButtonInfo ImageSource="pause.png" Description="Pause"
 Click="cmdPause_Click"></ThumbButtonInfo>
        </TaskbarItemInfo.ThumbButtonInfos>
    </TaskbarItemInfo>
</Window.TaskbarItemInfo>

缩略图裁剪

某些情况下,缩略图不需要显示整个窗口,只需要聚焦在窗口中的某一区域,则可以使用缩略图裁剪。

//获得按钮的坐标
Button cmd = (Button)sender;
Point locationFromWindow = cmd.TranslatePoint(new Point(0, 0), this);
//获得四个边的距离边界的长度
double left = locationFromWindow.X;
double top = locationFromWindow.Y;
double right = LayoutRoot.ActualWidth - cmd.ActualWidth - left;
double bottom = LayoutRoot.ActualHeight - cmd.ActualHeight - top;

//应用裁剪区域
taskBarItem.ThumbnailClipMargin = new Thickness(left, top, right, bottom);
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

步、步、为营

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值