欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 艺术 > powershell:使用IProgressDialog接口创建windows标准进度对话框。

powershell:使用IProgressDialog接口创建windows标准进度对话框。

2025/2/24 0:33:00 来源:https://blog.csdn.net/2401_82910308/article/details/139566320  浏览:    关键词:powershell:使用IProgressDialog接口创建windows标准进度对话框。

禁止任何形式的抄录,转载请附上本文章地址
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 }
}

效果:

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词