在Windows任务栏中显示进度 (PowerShell)

5

我正在尝试通过PowerShell在任务栏上显示进度。

enter image description here

据我所知,我应该使用类似这样的东西:
$Progress = [System.Windows.Shell.TaskbarItemInfo]::New()

$Progress.ProgressState = 'Normal'
$Progress.ProgressValue = 0.3

但是这样做不起作用。我做错了什么?如何在任务栏上显示进度?谢谢你。
2个回答

7

对于那些正在寻找答案的人。事实证明,.NET Framework不支持在任务栏上显示进度条。Windows Forms也不支持此功能。它现在支持WPF。

然而,有一个解决方案。该解决方案由Ravikanth Chaganti编写。 https://www.ravichaganti.com/blog/programming-windows-7-taskbar-using-windows-api-code-pack-and-powershell/。要做到这一点,您需要下载和安装WindowsAPICodePack库(它是为Windows 7设计的)。:

打开PowerShell并加载程序集

[Reflection.Assembly]::LoadFrom(“D:\API\Microsoft.WindowsAPICodePack.Shell.Dll”)

创建TaskBarManager实例

$TaskBarObject = [Microsoft.WindowsAPICodePack.TaskBar.TaskBarManager]::Instance

设置ProgressBar状态

$TaskBarObject.SetProgressState(“Normal”)

这不是强制性的。但是,了解可能的值很重要。以下是每个进度条状态的含义:

无进度 - 不显示进度条 不确定 - 进度不确定(跑马灯) 正常 - 显示正常进度 错误 - 发生错误(红色) 暂停 - 操作已暂停(黄色)

要设置进度条值

$TaskBarObject.SetProgressValue(50,100)


1
您可以在WPF中使用XAML创建新的窗口对象来实现此操作,我认为您目前的做法不可能实现。
# Add required assemblies
Add-Type -AssemblyName PresentationFramework, System.Drawing, System.Windows.Forms, WindowsFormsIntegration

# Setup the XAML
[xml]$script:xaml = '<Window 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        Title="MyWindow" Height="240" Width="320" Background="Gray">
 <Window.TaskbarItemInfo>
         <TaskbarItemInfo/>
 </Window.TaskbarItemInfo>
<Grid>
        <Image Name="image" Height="64" Width="64"/>
    </Grid>
</Window>'

# Create the form and set variables
$script:window = [Windows.Markup.XamlReader]::Load((New-Object System.Xml.XmlNodeReader $xaml))
$xaml.SelectNodes("//*[@Name]") | ForEach-Object { Set-Variable -Name ($_.Name) -Value $window.FindName($_.Name) -Scope Script }



# This is the toolbar icon and description
$window.TaskbarItemInfo.Description = $window.Title
$window.TaskbarItemInfo.ProgressValue = 0.8
$window.TaskbarItemInfo.ProgressState = "Normal"


$window.ShowDialog()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接