禁止任何形式的抄录,转载请附上本文章地址
Add-Type @'
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
// 定义ProgressDialog类,实现IProgressDialog接口
[ComImport]
[Guid("F8383852-FCD3-11D1-A6B9-006097DF5BD4")]
public class ProgressDialog { }
public class SHA
{
[ComImport]
[Guid("EBBC7C04-315E-11d2-B62F-006097DF5BD4")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IProgressDialog
{
[PreserveSig]
int StartProgressDialog(IntPtr hwndParent, [MarshalAs(UnmanagedType.IUnknown)] object punkEnableModless, ProgressDialogFlags dwFlags, IntPtr pvResevered);
[PreserveSig]
int StopProgressDialog();
[PreserveSig]
int SetTitle([MarshalAs(UnmanagedType.LPWStr)] string pwzTitle);
[PreserveSig]
int SetAnimation(IntPtr hInstAnimation, ushort idAnimation);
[PreserveSig]
int HasUserCancelled();
[PreserveSig]
int SetProgress(uint dwCompleted, uint dwTotal);
[PreserveSig]
void unknow_yzx();
[PreserveSig]
int SetLine(uint dwLineNum, string pwzString, bool fCompactPath, IntPtr pvResevered);
[PreserveSig]
int Timer(uint pdTimer, [MarshalAs(UnmanagedType.IUnknown)] object mustnull);
}
[Flags]
public enum ProgressDialogFlags : uint
{
Normal = 0x00000000,
Modal = 0x00000001,
AutoTime = 0x00000002,
NoTime = 0x00000004,
NoMinimize = 0x00000008,
NoProgressBar = 0x00000010,
MarqueeProgress = 0x00000020,
NoCancel = 0x00000040
}
}
public class yzx_ProgressDialog
{
public SHA.IProgressDialog Dialog;
public int StartProgressDialog(IntPtr handle, SHA.ProgressDialogFlags ProgressDialogFlag)
{
int back = Dialog.StartProgressDialog(handle, null, ProgressDialogFlag, IntPtr.Zero);
return back;
}
public int Stop()
{
int back = Dialog.StopProgressDialog();
return back;
}
public void Dispose()
{
Marshal.FinalReleaseComObject(Dialog);
}
public yzx_ProgressDialog()
{
Dialog = new ProgressDialog() as SHA.IProgressDialog;
}
public int SetTitle(string title)
{
int back = Dialog.SetTitle(title);
return back;
}
public int HasUserCancelled()
{
return Dialog.HasUserCancelled();
}
public int SetLine(uint line, string text)
{
return Dialog.SetLine(line, text, false, IntPtr.Zero);
}
public int Timer(uint stade)
{
return Dialog.Timer(stade, null);
}
public int SetProgress(uint value, uint max)
{
return Dialog.SetProgress(value, max);
}
}
'@
$dlg = New-Object yzx_ProgressDialog
$dlg.StartProgressDialog(0,[SHA+ProgressDialogFlags]::AutoTime) #创建并发起对话框
$dlg.SetTitle("对话框标题") #设置对话框标题
$dlg.SetLine(1,"字段1标题<XXX项正在处理中>") #设置字段1标题
$dlg.SetLine(2,"字段2标题<信息>") #设置字段2标题
#$dlg.SetLine(3,"字段3标题<保留用于估计时间>") #设置字段3标题
$dlg.SetProgress(0,100)
$dlg.HasUserCancelled() #判断用户是否点击“取消”按钮
#$dlg.Stop()
$i = 0
for(0){
sleep 1
if($i -le 100 -and $dlg.HasUserCancelled() -ne 1){
[void]$dlg.SetLine(3,"字段3标题<保留用于估计时间>【$i %】") #设置字段3标题
[void]$dlg.SetProgress($i,100)
$i++
}else{
sleep 1
$dlg.Stop()
return }
}
效果: