如何使用Powershell在表格内显示带有百分比的进度条?

问题描述 投票:0回答:1

我有一个包含表格的Windows窗体。我需要在概述列内显示进度条。我期望的进度条是带有百分比数字的条。

但是我的代码是,一旦此进程“ Start-Process .\Utility.exe /getconfig:.\UUT.log -Windowstyle Minimized”完成,GUI 将弹出。 我的期望是一旦代码开始运行,GUI 将弹出,进度条将显示从 0 - 100%。

这是我的代码。

Add-Type -AssemblyName System.Windows.Forms

# Get the screen's working area size
$screen = [System.Windows.Forms.Screen]::PrimaryScreen.WorkingArea

# Create the main form
$form = New-Object System.Windows.Forms.Form

# Set form properties
$form.Text = "MYGUI"
$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
$form.WindowState = [System.Windows.Forms.FormWindowState]::Maximized
$form.Size = New-Object System.Drawing.Size($screen.Width, $screen.Height)

$form.BackColor = '#F2F2F2'

# Set form icon
$iconPath = "C:\1.0\xx.ico"  
if (Test-Path $iconPath) {
    $icon = New-Object System.Drawing.Icon($iconPath)
    $form.Icon = $icon
}

# Calculate dimensions for the sub-form (1/4 of the main form)
$subViewWidth = [math]::Round($form.Width / 1.5)
$subViewHeight = [math]::Round($form.Height / 1.5)
$subViewLeft = [math]::Round(($form.Width - $subViewWidth) / 2)
$subViewTop = [math]::Round(($form.Height - $subViewHeight) / 2.5)

# Create a Panel to act as the sub-form
$subView = New-Object System.Windows.Forms.Panel
$subView.Width = $subViewWidth
$subView.Height = $subViewHeight
$subView.BackColor = '#FFFFFF'
$subView.Location = New-Object System.Drawing.Point($subViewLeft, $subViewTop)

# Add the Panel to the main form
$form.Controls.Add($subView)

# Create a DataGridView
$dataGridView = New-Object System.Windows.Forms.DataGridView
$dataGridView.Width = $subViewWidth - 20  # Adjusting width for padding
$dataGridView.Height = $subViewHeight - 40  # Adjusting height for padding
$dataGridView.AutoSizeColumnsMode = [System.Windows.Forms.DataGridViewAutoSizeColumnsMode]::Fill
$dataGridView.ReadOnly = $true  # Disallow user input
$dataGridView.BackgroundColor = "#FFFFFF"
$dataGridView.ColumnHeadersDefaultCellStyle.BackColor = "#0096D6"

$dataGridView.ColumnHeadersDefaultCellStyle.ForeColor = "#FFFFFF" # Change header font color
$dataGridView.ColumnHeadersDefaultCellStyle.Font = New-Object System.Drawing.Font("Tahoma", 9, [System.Drawing.FontStyle]::Bold)  # Change header font style and size

# Define columns for the DataGridView (same as before)
$stepColumn = New-Object System.Windows.Forms.DataGridViewColumn
$stepColumn.HeaderText = "Step"
$stepColumn.Name = "Step"
$stepCellTemplate = New-Object System.Windows.Forms.DataGridViewTextBoxCell
$stepColumn.CellTemplate = $stepCellTemplate

$overviewColumn = New-Object System.Windows.Forms.DataGridViewColumn
$overviewColumn.HeaderText = "Overview"
$overviewColumn.Name = "Overview"
$overviewCellTemplate = New-Object System.Windows.Forms.DataGridViewTextBoxCell
$overviewColumn.CellTemplate = $overviewCellTemplate

$messageColumn = New-Object System.Windows.Forms.DataGridViewColumn
$messageColumn.HeaderText = "Message"
$messageColumn.Name = "Message"
$messageCellTemplate = New-Object System.Windows.Forms.DataGridViewTextBoxCell
$messageColumn.CellTemplate = $messageCellTemplate

# Create the progress bar 
$progressBar = New-Object System.Windows.Forms.ProgressBar
$progressBar.Width = $subViewWidth - 40  
$progressBar.Height = 20

# Add columns to the DataGridView
$dataGridView.Columns.Add($stepColumn)
$dataGridView.Columns.Add($overviewColumn)
$dataGridView.Columns.Add($messageColumn)

# Position the DataGridView in the middle of the sub-form
$dataGridView.Left = ($subViewWidth - $dataGridView.Width) / 2
$dataGridView.Top = ($subViewHeight - $dataGridView.Height) / 2

# Add progress bar to panel
$subView.Controls.Add($progressBar) 
$progressBar.Top = $dataGridView.Bottom + 10
$progressBar.Left = ($subViewWidth - $progressBar.Width) / 2

# Add the DataGridView to the sub-form
$subView.Controls.Add($dataGridView)


####### Start Process #######

# Create the progress bar 
$progressBar = New-Object System.Windows.Forms.ProgressBar
$progressBar.Width = $subViewWidth - 40  
$progressBar.Height = 20

# Other form initialization code 

# Add progress bar to panel
$subView.Controls.Add($progressBar) 
$progressBar.Top = $dataGridView.Bottom + 10
$progressBar.Left = ($subViewWidth - $progressBar.Width) / 2

####### Start Process #######

# Get UUT info
$Step_1 = "Get UUT info"

# Show progress bar  
$progressBar.Value = 0
$progressBar.Visible = $true

try {
    # Update progress bar while command runs
    Start-Process .\Utility.exe /getconfig:.\UUT.log -Windowstyle Minimized
    
    for ($i = 1; $i -le 100; $i++) {
       Start-Sleep -Milliseconds 50  
       $progressBar.Value = $i
    }

    # Check if UUT.log exists
    $logFilePath = ".\UUT.log"
    if (Test-Path $logFilePath) {
        # Add result row 
        $dataGridView.Rows.Add("$Step_1", "Passed", "Success") 
    }
} catch {
    $dataGridView.Rows.Add("$Step_1", "Failed", "Error message")
}

# Hide progress bar
$progressBar.Visible = $false 

$form.ShowDialog()

任何人都可以帮我提供一个想法吗?谢谢!

powershell winforms progress-bar
1个回答
0
投票

请参阅:https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/write-progress?view=powershell-7.4

您最好的选择是

Write-Progress
,但如果这给您带来问题,您可以创建自定义
DataGridViewProgressBarCell
DataGridViewProgressBarColumn

# Add-Type -AssemblyName System.Windows.Forms

# ... (your existing code)

# Create a DataGridViewProgressBarCell
Add-Type -TypeDefinition @"
using System;
using System.Drawing;
using System.Windows.Forms;

public class DataGridViewProgressBarCell : DataGridViewTextBoxCell
{
    public int Value { get; set; }

    protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
    {
        int progressVal = (int)value;
        float percentage = ((float)progressVal / 100.0f); 
        Brush backColorBrush = new SolidBrush(cellStyle.BackColor);
        Brush foreColorBrush = new SolidBrush(cellStyle.ForeColor);

        base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);

        graphics.FillRectangle(new SolidBrush(Color.FromArgb(22, 160, 133)), cellBounds.X + 2, cellBounds.Y + 2, Convert.ToInt32((percentage * cellBounds.Width - 4)), cellBounds.Height - 4);
        graphics.DrawString(progressVal + "%", cellStyle.Font, foreColorBrush, cellBounds.X+(cellBounds.Width/2), cellBounds.Y+2);
    }
}
"@ -ReferencedAssemblies System.Windows.Forms

# Create a DataGridViewProgressBarColumn
Add-Type -TypeDefinition @"
using System;
using System.Windows.Forms;

public class DataGridViewProgressBarColumn : DataGridViewColumn
{
    public DataGridViewProgressBarColumn()
    {
        this.CellTemplate = new DataGridViewProgressBarCell();
    }
}
"@ -ReferencedAssemblies System.Windows.Forms

# ... (your existing code)

# Define columns for the DataGridView
$stepColumn = New-Object System.Windows.Forms.DataGridViewColumn
$stepColumn.HeaderText = "Step"
$stepColumn.Name = "Step"
$stepCellTemplate = New-Object System.Windows.Forms.DataGridViewTextBoxCell
$stepColumn.CellTemplate = $stepCellTemplate

$overviewColumn = New-Object DataGridViewProgressBarColumn
$overviewColumn.HeaderText = "Overview"
$overviewColumn.Name = "Overview"

$messageColumn = New-Object System.Windows.Forms.DataGridViewColumn
$messageColumn.HeaderText = "Message"
$messageColumn.Name = "Message"
$messageCellTemplate = New-Object System.Windows.Forms.DataGridViewTextBoxCell
$messageColumn.CellTemplate = $messageCellTemplate

# ... (your existing code)

# Start Process
$Step_1 = "Get UUT info"

# Show progress bar
$progressBar.Value = 0
$progressBar.Visible = $true

try {
    # Update progress bar while command runs
    $process = Start-Process .\Utility.exe /getconfig:.\UUT.log -Windowstyle Minimized -PassThru
    while (!$process.HasExited) {
        Start-Sleep -Milliseconds 50
        $progressBar.Value = [math]::Min($progressBar.Value + 1, 100)
        $dataGridView.Rows.Add("$Step_1", $progressBar.Value, "Running")
    }

    # Check if UUT.log exists
    $logFilePath = ".\UUT.log"
    if (Test-Path $logFilePath) {
        # Add result row
        $dataGridView.Rows.Add("$Step_1", 100, "Success")
    }
} catch {
    $dataGridView.Rows.Add("$Step_1", 0, "Error message")
}

# Hide progress bar
$progressBar.Visible = $false 

$form.ShowDialog()

尝试一下,如果有帮助请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.