106 lines
3.6 KiB
PowerShell
106 lines
3.6 KiB
PowerShell
# PromptHelper.ps1 — shared foreground/system-modal confirmation dialog helper.
|
|
|
|
function Show-ConfirmationDialog {
|
|
[CmdletBinding()]
|
|
param(
|
|
[Parameter(Mandatory)][string]$Title,
|
|
[Parameter(Mandatory)][string]$Message,
|
|
[string]$Feature = 'PromptHelper',
|
|
[string]$Default = 'No'
|
|
)
|
|
|
|
try {
|
|
$shell = New-Object -ComObject WScript.Shell
|
|
# 4=YesNo, 32=Question icon, 4096=System modal, 65536=Foreground
|
|
$result = $shell.Popup($Message, 0, $Title, 4 + 32 + 4096 + 65536)
|
|
|
|
switch ($result) {
|
|
6 { return 'Yes' }
|
|
7 { return 'No' }
|
|
default { return $Default }
|
|
}
|
|
}
|
|
catch {
|
|
Write-Log -Level Error -Message "Failed to show confirmation dialog '$Title': $_" -Feature $Feature
|
|
return $Default
|
|
}
|
|
}
|
|
|
|
function Show-MultiChoiceDialog {
|
|
[CmdletBinding()]
|
|
param(
|
|
[Parameter(Mandatory)][string]$Title,
|
|
[Parameter(Mandatory)][string]$Message,
|
|
[Parameter(Mandatory)][object[]]$Choices,
|
|
[string]$Feature = 'PromptHelper',
|
|
[string]$DefaultValue = ''
|
|
)
|
|
|
|
try {
|
|
Add-Type -AssemblyName System.Windows.Forms -ErrorAction SilentlyContinue | Out-Null
|
|
Add-Type -AssemblyName System.Drawing -ErrorAction SilentlyContinue | Out-Null
|
|
|
|
if ($Choices.Count -lt 1 -or $Choices.Count -gt 4) {
|
|
throw 'Show-MultiChoiceDialog expects 1 to 4 choices.'
|
|
}
|
|
|
|
$form = New-Object System.Windows.Forms.Form
|
|
$form.Text = $Title
|
|
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
|
|
$form.TopMost = $true
|
|
$form.Width = 480
|
|
$form.Height = 190
|
|
$form.MinimizeBox = $false
|
|
$form.MaximizeBox = $false
|
|
$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog
|
|
|
|
$label = New-Object System.Windows.Forms.Label
|
|
$label.AutoSize = $false
|
|
$label.Left = 12
|
|
$label.Top = 12
|
|
$label.Width = 440
|
|
$label.Height = 80
|
|
$label.Text = $Message
|
|
$form.Controls.Add($label)
|
|
|
|
$form.Tag = $DefaultValue
|
|
$gap = 10
|
|
$availableWidth = $form.ClientSize.Width - 24 - ($gap * ($Choices.Count - 1))
|
|
$buttonWidth = [Math]::Min(130, [Math]::Floor($availableWidth / $Choices.Count))
|
|
$totalWidth = ($buttonWidth * $Choices.Count) + ($gap * ($Choices.Count - 1))
|
|
$startX = [Math]::Max(12, [int](($form.ClientSize.Width - $totalWidth) / 2))
|
|
|
|
for ($i = 0; $i -lt $Choices.Count; $i++) {
|
|
$choice = $Choices[$i]
|
|
$button = New-Object System.Windows.Forms.Button
|
|
$button.Left = $startX + ($i * ($buttonWidth + $gap))
|
|
$button.Top = 105
|
|
$button.Width = $buttonWidth
|
|
$button.Height = 30
|
|
$button.Text = [string]$choice.Label
|
|
$button.Tag = [string]$choice.Value
|
|
$button.Add_Click({
|
|
param($sender, $eventArgs)
|
|
$dlg = $sender.FindForm()
|
|
$dlg.Tag = [string]$sender.Tag
|
|
$dlg.DialogResult = [System.Windows.Forms.DialogResult]::OK
|
|
$dlg.Close()
|
|
})
|
|
$form.Controls.Add($button)
|
|
if ($i -eq 0) {
|
|
$form.AcceptButton = $button
|
|
}
|
|
}
|
|
|
|
[void]$form.ShowDialog()
|
|
$result = [string]$form.Tag
|
|
$form.Dispose()
|
|
|
|
return $result
|
|
}
|
|
catch {
|
|
Write-Log -Level Error -Message "Failed to show multi-choice dialog '$Title': $_" -Feature $Feature
|
|
return $DefaultValue
|
|
}
|
|
}
|