Advertisement
Ubidibity

Gui-Form-Test

Jan 25th, 2025
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Add-Type -AssemblyName System.Windows.Forms
  2.  
  3. # Inspired by https://pastebin.com/UsftjYPK this is the outline for a status bar window for more complex powershell scripts.
  4.  
  5. # Create a custom PowerShell object for the form
  6. $formObject = [PSCustomObject]@{
  7.     Form = $null
  8.     Label = $null
  9.     ProgressBar = $null
  10.     UpdateProgress = {
  11.         param($progress)
  12.         if ($this.ProgressBar -ne $null) {
  13.             $this.ProgressBar.Value = $progress
  14.         }
  15.     }
  16.     UpdateLabel = {
  17.         param($text)
  18.         if ($this.Label -ne $null) {
  19.             $this.Label.Text = $text
  20.         }
  21.     }
  22. }
  23.  
  24. # Method to initialize the form
  25. function Initialize-Form {
  26.     $formObject.Form = New-Object System.Windows.Forms.Form
  27.     $formObject.Form.Text = 'Progress Window'
  28.     $formObject.Form.Size = New-Object System.Drawing.Size(300,200)
  29.     $formObject.Form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
  30.     $formObject.Form.TopMost = $true
  31.  
  32.     $formObject.Label = New-Object System.Windows.Forms.Label
  33.     $formObject.Label.Text = 'Starting...'
  34.     $formObject.Label.AutoSize = $true
  35.     $formObject.Label.Location = New-Object System.Drawing.Point(10,20)
  36.     $formObject.Form.Controls.Add($formObject.Label)
  37.  
  38.     $formObject.ProgressBar = New-Object System.Windows.Forms.ProgressBar
  39.     $formObject.ProgressBar.Style = "Continuous"
  40.     $formObject.ProgressBar.Minimum = 0
  41.     $formObject.ProgressBar.Maximum = 100
  42.     $formObject.ProgressBar.Location = New-Object System.Drawing.Point(10,50)
  43.     $formObject.ProgressBar.Size = New-Object System.Drawing.Size(260,23)
  44.     $formObject.Form.Controls.Add($formObject.ProgressBar)
  45.  
  46.     $formObject.Form.Show()
  47. }
  48.  
  49. # Method to dispose of the form
  50. function Dispose-Form {
  51.     if ($formObject.Form -ne $null) {
  52.         $formObject.Form.Close()
  53.         $formObject.Form.Dispose()
  54.     }
  55. }
  56.  
  57. # Initialize the form
  58. Initialize-Form
  59.  
  60. # Example usage: Simulate some work and update the UI
  61. for ($i = 0; $i -le 100; $i += 10) {
  62.     Start-Sleep -Seconds 1
  63.     # Instead of using Invoke, define helper functions for better scope handling
  64.     $updateLabel = { param($text) $formObject.Label.Text = $text }
  65.     $updateProgress = { param($progress) $formObject.ProgressBar.Value = $progress }
  66.    
  67.     & $updateLabel -text "Processing: $i%"
  68.     & $updateProgress -progress $i
  69.     [System.Windows.Forms.Application]::DoEvents()
  70. }
  71.  
  72. # Cleanup
  73. Dispose-Form
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement