progress bar – Powershell Progressbar partially hidden

I have a progress bar that is run when a job is done. Unfortunately, the progress bar is partially hidden. This is the code:

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$SAPIENTypes = @'
using System;
using System.Windows.Forms;
using System.Drawing;
        namespace SAPIENTypes
        {
    public class ProgressBarOverlay : System.Windows.Forms.ProgressBar
        {
                public ProgressBarOverlay() : base() { SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true); }
            protected override void WndProc(ref Message m)
            { 
                base.WndProc(ref m);
                if (m.Msg == 0x000F)// WM_PAINT
                {
                    if (Style != System.Windows.Forms.ProgressBarStyle.Marquee || !string.IsNullOrEmpty(this.Text))
                        {
                            using (Graphics g = this.CreateGraphics())
                            {
                                using (StringFormat stringFormat = new StringFormat(StringFormatFlags.NoWrap))
                                {
                                    stringFormat.Alignment = StringAlignment.Center;
                                    stringFormat.LineAlignment = StringAlignment.Center;
                                    if (!string.IsNullOrEmpty(this.Text))
                                        g.DrawString(this.Text, this.Font, Brushes.Black, this.ClientRectangle, stringFormat);
                                    else
                                    {
                                        int percent = (int)(((double)Value / (double)Maximum) * 100);
                                        g.DrawString(percent.ToString() + "%", this.Font, Brushes.Black, this.ClientRectangle, stringFormat);
                                    }
                                }
                            }
                        }
                }
            }
              
                public string TextOverlay
                {
                    get
                    {
                        return base.Text;
                    }
                    set
                    {
                        base.Text = value;
                        Invalidate();
                    }
                }
        }
        }
'@

Add-Type $SAPIENTypes -ReferencedAssemblies 'System.Windows.Forms', 'System.Drawing' -ErrorAction SilentlyContinue

$frmFonts = New-Object 'System.Windows.Forms.Form'
$chkRecurse = New-Object 'System.Windows.Forms.CheckBox'
$btnExit = New-Object 'System.Windows.Forms.Button'
$chkMachineWide = New-Object 'System.Windows.Forms.CheckBox'
$chkAdmin = New-Object 'System.Windows.Forms.CheckBox'
$grpFolder = New-Object 'System.Windows.Forms.GroupBox'
$ProgressBar = New-Object 'SAPIENTypes.ProgressBarOverlay'
$chkAllItems = New-Object 'System.Windows.Forms.CheckBox'
$btnUninstallSelectedFonts = New-Object 'System.Windows.Forms.Button'
$btnInstallFonts = New-Object 'System.Windows.Forms.Button'
$btnGetFolderContent = New-Object 'System.Windows.Forms.Button'
$dgvFontFiles = New-Object 'System.Windows.Forms.DataGridView'
#
# frmFonts
#
$frmFonts.Controls.Add($chkRecurse)
$frmFonts.Controls.Add($btnExit)
$frmFonts.Controls.Add($chkMachineWide)
$frmFonts.Controls.Add($chkAdmin)
$frmFonts.Controls.Add($grpFolder)
$frmFonts.AutoScaleDimensions = New-Object System.Drawing.SizeF(6, 13)
$frmFonts.AutoScaleMode="Font"
$frmFonts.ClientSize = New-Object System.Drawing.Size(1029, 667)
$frmFonts.FormBorderStyle="Fixed3D"
$frmFonts.Name="frmFonts"
$frmFonts.StartPosition = 'CenterScreen'
$frmFonts.Text="Add or Remove Fonts"
$frmFonts.add_Load($frmFonts_Load)
#
# chkRecurse
#
$chkRecurse.Location = New-Object System.Drawing.Point(680, 73)
$chkRecurse.Name="chkRecurse"
$chkRecurse.Size = New-Object System.Drawing.Size(337, 24)
$chkRecurse.TabIndex = 8
$chkRecurse.Text="Include sub folders"
$chkRecurse.UseVisualStyleBackColor = $True
#
# btnExit
#
$btnExit.Location = New-Object System.Drawing.Point(870, 621)
$btnExit.Name="btnExit"
$btnExit.Size = New-Object System.Drawing.Size(147, 40)
$btnExit.TabIndex = 7
$btnExit.Text="Exit"
$btnExit.UseVisualStyleBackColor = $True
$btnExit.add_Click({[void]$frmFonts.Close()})
#
# chkMachineWide
#
$chkMachineWide.Location = New-Object System.Drawing.Point(680, 43)
$chkMachineWide.Name="chkMachineWide"
$chkMachineWide.Size = New-Object System.Drawing.Size(337, 24)
$chkMachineWide.TabIndex = 5
$chkMachineWide.Text="Install the fonts for all users."
$chkMachineWide.UseVisualStyleBackColor = $True
#
# chkAdmin
#
$chkAdmin.Location = New-Object System.Drawing.Point(680, 13)
$chkAdmin.Name="chkAdmin"
$chkAdmin.Size = New-Object System.Drawing.Size(337, 24)
$chkAdmin.TabIndex = 4
$chkAdmin.Text="User has local admin rights."
$chkAdmin.UseVisualStyleBackColor = $True
#
# grpFolder
#
$grpFolder.Controls.Add($ProgressBar)
$grpFolder.Controls.Add($chkAllItems)
$grpFolder.Controls.Add($btnUninstallSelectedFonts)
$grpFolder.Controls.Add($btnInstallFonts)
$grpFolder.Controls.Add($btnGetFolderContent)
$grpFolder.Controls.Add($dgvFontFiles)
$grpFolder.Location = New-Object System.Drawing.Point(12, 12)
$grpFolder.Name="grpFolder"
$grpFolder.Size = New-Object System.Drawing.Size(650, 649)
$grpFolder.TabIndex = 3
$grpFolder.TabStop = $False
$grpFolder.Text="Install from folder"
#
# ProgressBar
#
$ProgressBar.BackColor = [System.Drawing.SystemColors]::ControlLightLight 
$ProgressBar.Location = New-Object System.Drawing.Point(20, 275)
$ProgressBar.Margin = '5, 5, 5, 5'
$ProgressBar.Name="ProgressBar"
$ProgressBar.Size = New-Object System.Drawing.Size(970, 75)
$ProgressBar.Style="Continuous"
$ProgressBar.TabIndex = 5
$ProgressBar.TextOverlay = 'This is a text'
$ProgressBar.Value = 90
#
# chkAllItems
#
$chkAllItems.Location = New-Object System.Drawing.Point(7, 20)
$chkAllItems.Name="chkAllItems"
$chkAllItems.Size = New-Object System.Drawing.Size(432, 24)
$chkAllItems.TabIndex = 4
$chkAllItems.Text="Select or unselect all items"
$chkAllItems.UseVisualStyleBackColor = $True
#
# btnUninstallSelectedFonts
#
$btnUninstallSelectedFonts.Location = New-Object System.Drawing.Point(432, 599)
$btnUninstallSelectedFonts.Name="btnUninstallSelectedFonts"
$btnUninstallSelectedFonts.Size = New-Object System.Drawing.Size(200, 40)
$btnUninstallSelectedFonts.TabIndex = 3
$btnUninstallSelectedFonts.Text="Uninstall selected fonts"
$btnUninstallSelectedFonts.UseVisualStyleBackColor = $True
#
# btnInstallFonts
#
$btnInstallFonts.Location = New-Object System.Drawing.Point(226, 599)
$btnInstallFonts.Name="btnInstallFonts"
$btnInstallFonts.Size = New-Object System.Drawing.Size(200, 40)
$btnInstallFonts.TabIndex = 2
$btnInstallFonts.Text="Install selected fonts"
$btnInstallFonts.UseVisualStyleBackColor = $True
#
# btnGetFolderContent
#
$btnGetFolderContent.Location = New-Object System.Drawing.Point(20, 599)
$btnGetFolderContent.Name="btnGetFolderContent"
$btnGetFolderContent.Size = New-Object System.Drawing.Size(200, 40)
$btnGetFolderContent.TabIndex = 1
$btnGetFolderContent.Text="Get folder content"
$btnGetFolderContent.UseVisualStyleBackColor = $True
#
# dgvFontFiles
#
$dgvFontFiles.AutoSizeColumnsMode="AllCells"
$dgvFontFiles.AutoSizeRowsMode="AllCells"
$dgvFontFiles.ColumnHeadersHeightSizeMode="AutoSize"
$System_Windows_Forms_DataGridViewCellStyle_1 = New-Object 'System.Windows.Forms.DataGridViewCellStyle'
$System_Windows_Forms_DataGridViewCellStyle_1.Alignment="MiddleLeft"
$System_Windows_Forms_DataGridViewCellStyle_1.BackColor = [System.Drawing.SystemColors]::Window 
$System_Windows_Forms_DataGridViewCellStyle_1.Font = [System.Drawing.Font]::new('Microsoft Sans Serif', '8.25')
$System_Windows_Forms_DataGridViewCellStyle_1.ForeColor = [System.Drawing.SystemColors]::ControlText 
$System_Windows_Forms_DataGridViewCellStyle_1.SelectionBackColor = [System.Drawing.SystemColors]::Highlight 
$System_Windows_Forms_DataGridViewCellStyle_1.SelectionForeColor = [System.Drawing.SystemColors]::HighlightText 
$System_Windows_Forms_DataGridViewCellStyle_1.WrapMode="True"
$dgvFontFiles.DefaultCellStyle = $System_Windows_Forms_DataGridViewCellStyle_1
$dgvFontFiles.Location = New-Object System.Drawing.Point(6, 50)
$dgvFontFiles.Name="dgvFontFiles"
$dgvFontFiles.Size = New-Object System.Drawing.Size(626, 543)
$dgvFontFiles.TabIndex = 0

[void]$frmFonts.ShowDialog()

I already created a second form with only the progess bar. But in that case I could not read the datagridview.

What can I do to have the progressbar to front so the whole progress bar is visible? Thanks for your help.

Regards… TheStingPilot

Leave a Comment