Skip to content

Commit fdbcc19

Browse files
author
donald
committed
new class interface and bugfix
1 parent 8027064 commit fdbcc19

File tree

3 files changed

+130
-39
lines changed

3 files changed

+130
-39
lines changed

Examples/Process_Killer.ps1

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
11
Import-Module "..\AnyBox.psd1"
22

3+
$anybox = New-Object AnyBox.AnyBox
4+
5+
$anybox.Title = 'Process Killer'
6+
$anybox.ResizeMode = 'CanResizeWithGrip'
7+
$anybox.MaxHeight = 800
8+
$anybox.MaxWidth = 600
9+
$anybox.Topmost = $true
10+
$anybox.AccentColor = 'Black'
11+
12+
$anybox.GridData = @(Get-WmiObject -Class Win32_Process -ea Stop | select ProcessId, ProcessName, CommandLine)
13+
$anybox.NoGridSearch = $true
14+
$anybox.SelectionMode = [AnyBox.DataGridSelectionMode]::MultiRow
15+
316
# Define the computer name prompt; the field must not be empty and the computer must be online.
4-
$p = @(New-Prompt -Group 0 -Name 'pcName' -Message 'Computer Name:' -MessagePosition 'Left' -DefaultValue 'Localhost' `
17+
$anybox.Prompts = @(New-AnyBoxPrompt -Group 0 -Name 'pcName' -Message 'Computer Name:' -MessagePosition 'Left' -DefaultValue 'Localhost' `
518
-ValidateNotEmpty -ValidateScript { Test-Connection $_ -Count 1 -Quiet -ea 0})
619

720
# Define the process filter prompt
8-
$p += @(New-Prompt -Group 0 -Name 'pFilter' -Message ' Process Name:' -MessagePosition 'Left' -DefaultValue '*' `
21+
$anybox.Prompts += @(New-AnyBoxPrompt -Group 0 -Name 'pFilter' -Message ' Process Name:' -MessagePosition 'Left' -DefaultValue '*' `
922
-ValidateNotEmpty)
1023

1124
# Define the 'Refresh' button.
12-
$b = @(New-Button -Text 'Refresh' -IsDefault -OnClick {
13-
25+
$anybox.Buttons = New-AnyBoxButton -Text 'Refresh' -IsDefault -OnClick {
1426
# Run 'Test-ValidateInput' to enforce
1527
# the validation parameters set on the 'pcName' prompt.
1628
$input_test = Test-ValidInput -Prompts $Prompts -Inputs $form.Result
@@ -45,11 +57,11 @@ $b = @(New-Button -Text 'Refresh' -IsDefault -OnClick {
4557
# Show a child window with @childWinParams.
4658
if ($msg) { Show-AnyBox @childWinParams -Message $msg -Buttons 'OK' }
4759
}
48-
})
60+
}
4961

50-
$b += New-AnyBoxButton -Template 'SaveGrid'
62+
$anybox.Buttons += New-AnyBoxButton -Template 'SaveGrid'
5163

52-
$b += @(New-Button -Text 'Kill' -OnClick {
64+
$anybox.Buttons += New-AnyBoxButton -Text 'Kill' -OnClick {
5365
# Run 'Test-ValidateInput' to enforce
5466
# the validation parameters set on the 'pcName' prompt.
5567
$input_test = Test-ValidInput -Prompts $Prompts -Inputs $form.Result
@@ -116,10 +128,6 @@ $b += @(New-Button -Text 'Kill' -OnClick {
116128
}
117129
}
118130
}
119-
})
120-
121-
$grid_data = @(Get-WmiObject -Class Win32_Process -ea Stop | select ProcessId, ProcessName, CommandLine)
131+
}
122132

123-
Show-AnyBox -ResizeMode 'CanResizeWithGrip' -Title 'Process Killer' -MaxHeight 1000 -MaxWidth 1200 -Topmost `
124-
-AccentColor 'Black' -Buttons $b -Prompts $p -GridData $grid_data -HideGridSearch -SelectionMode 'MultiRow' |
125-
Out-Null
133+
$anybox | Show-AnyBox | Out-Null

Public/Show-AnyBox.ps1

Lines changed: 61 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -77,66 +77,104 @@ function Show-AnyBox
7777
.OUTPUTS
7878
A hashtable of key-value pairs containing what input was received (e.g., text input, button clicked).
7979
#>
80-
[cmdletbinding()]
80+
[cmdletbinding(DefaultParameterSetName='obj')]
8181
param(
82-
[ValidateSet($null, 'Information', 'Warning', 'Error', 'Question')]
82+
[Parameter(ParameterSetName='obj', ValueFromPipeline=$true)]
83+
[AnyBox.AnyBox]$AnyBox,
84+
[Parameter(ParameterSetName='create')]
8385
[string]$Icon,
86+
[Parameter(ParameterSetName='create')]
8487
[string]$Title,
88+
[Parameter(ParameterSetName='create')]
8589
[string]$Image,
90+
[Parameter(ParameterSetName='create')]
8691
[string[]]$Message,
92+
[Parameter(ParameterSetName='create')]
8793
[Alias('Prompt')]
8894
[object[]]$Prompts,
95+
[Parameter(ParameterSetName='create')]
8996
[object[]]$Buttons,
97+
[Parameter(ParameterSetName='create')]
9098
[string]$CancelButton,
99+
[Parameter(ParameterSetName='create')]
91100
[string]$DefaultButton,
101+
[Parameter(ParameterSetName='create')]
92102
[ValidateScript({$_ -gt 0})]
93103
[uint16]$ButtonRows = 1,
104+
[Parameter(ParameterSetName='create')]
94105
[string[]]$Comment,
106+
[Parameter(ParameterSetName='create')]
95107
[ValidateSet('Left', 'Center')]
96108
[string]$ContentAlignment = 'Left',
109+
[Parameter(ParameterSetName='create')]
97110
[switch]$CollapsibleGroups,
111+
[Parameter(ParameterSetName='create')]
98112
[switch]$CollapsedGroups,
113+
[Parameter(ParameterSetName='create')]
99114
[scriptblock]$PrepScript,
100115

116+
[Parameter(ParameterSetName='create')]
101117
[ValidateNotNullOrEmpty()]
102-
[string]$FontFamily = 'Segoe UI',
118+
[System.Windows.Media.FontFamily]$FontFamily = 'Segoe UI',
119+
[Parameter(ParameterSetName='create')]
103120
[ValidateScript({$_ -gt 0})]
104121
[uint16]$FontSize = 12,
122+
[Parameter(ParameterSetName='create')]
105123
[ValidateNotNullOrEmpty()]
106-
[string]$FontColor = 'Black',
107-
[string]$BackgroundColor,
108-
[string]$AccentColor = 'Gainsboro',
109-
[ValidateSet('None', 'SingleBorderWindow', 'ThreeDBorderWindow', 'ToolWindow')]
124+
[System.Windows.Media.Brush]$FontColor = 'Black',
125+
[Parameter(ParameterSetName='create')]
126+
[System.Windows.Media.Brush]$BackgroundColor,
127+
[Parameter(ParameterSetName='create')]
128+
[System.Windows.Media.Brush]$AccentColor = 'Gainsboro',
129+
[Parameter(ParameterSetName='create')]
110130
[System.Windows.WindowStyle]$WindowStyle = 'SingleBorderWindow',
111-
[ValidateSet('NoResize', 'CanMinimize', 'CanResize', 'CanResizeWithGrip')]
131+
[Parameter(ParameterSetName='create')]
112132
[System.Windows.ResizeMode]$ResizeMode = 'CanMinimize',
133+
[Parameter(ParameterSetName='create')]
113134
[switch]$NoResize,
135+
[Parameter(ParameterSetName='create')]
114136
[ValidateScript({$_ -gt 0})]
115137
[uint16]$MinHeight = 50,
138+
[Parameter(ParameterSetName='create')]
116139
[ValidateScript({$_ -gt 0})]
117140
[uint16]$MinWidth = 50,
141+
[Parameter(ParameterSetName='create')]
118142
[uint16]$MaxHeight = 0,
143+
[Parameter(ParameterSetName='create')]
119144
[uint16]$MaxWidth = 0,
145+
[Parameter(ParameterSetName='create')]
120146
[switch]$Topmost,
147+
[Parameter(ParameterSetName='create')]
121148
[switch]$HideTaskbarIcon,
149+
[Parameter(ParameterSetName='create')]
122150
[uint32]$Timeout,
151+
[Parameter(ParameterSetName='create')]
123152
[switch]$Countdown,
153+
[Parameter(ParameterSetName='create')]
124154
[System.Windows.Window]$ParentWindow = $null,
125-
126-
[array]$GridData,
155+
[Parameter(ParameterSetName='create')]
156+
[object[]]$GridData,
157+
[Parameter(ParameterSetName='create')]
127158
[switch]$GridAsList,
128-
[ValidateSet('None', 'SingleCell', 'SingleRow', 'MultiRow')]
129-
[string]$SelectionMode = 'SingleCell',
159+
[Parameter(ParameterSetName='create')]
160+
[AnyBox.DataGridSelectionMode]$SelectionMode = 'SingleCell',
161+
[Parameter(ParameterSetName='create')]
130162
[Alias('HideGridSearch')]
131163
[switch]$NoGridSearch
132164
)
133165

166+
if ($AnyBox) {
167+
$AnyBox.psobject.properties | ForEach-Object {
168+
Set-Variable -Name $_.Name -Value $_.Value -Force
169+
}
170+
}
171+
134172
if ($NoResize -or ($HideTaskbarIcon -and $ResizeMode -ne 'NoResize' -and @('None', 'ToolWindow') -notcontains $WindowStyle)) {
135173
# No minimize button
136174
$ResizeMode = 'NoResize'
137175
}
138176

139-
$form = @{'Result'=@{}} # [hashtable]::Synchronized(@{ 'Result' = @{})
177+
$form = @{'Result'=@{}}
140178

141179
[xml]$xaml = @"
142180
<Window
@@ -150,7 +188,7 @@ function Show-AnyBox
150188
<ColumnDefinition Width="*" />
151189
</Grid.ColumnDefinitions>
152190
<Grid.RowDefinitions>
153-
<RowDefinition Height="*" />
191+
<RowDefinition Height="Auto" />
154192
<RowDefinition Height="*" />
155193
<RowDefinition Height="Auto" />
156194
</Grid.RowDefinitions>
@@ -282,7 +320,7 @@ function Show-AnyBox
282320
}
283321

284322
$tab_panel = $null
285-
if ($Prompts | where { $_.Tab }) {
323+
if ($Prompts | Where-Object { $_.Tab }) {
286324
$tab_panel = New-Object System.Windows.Controls.TabControl
287325
$tab_panel.HorizontalAlignment = 'Stretch'
288326
$tab_panel.VerticalAlignment = 'Stretch'
@@ -292,7 +330,7 @@ function Show-AnyBox
292330
$form.Add('Tabs', $tab_panel)
293331
}
294332

295-
$Prompts | Group-Object -Property 'Tab' | sort Name | foreach {
333+
$Prompts | Group-Object -Property 'Tab' | Sort-Object Name | ForEach-Object {
296334
$tabName = $_.Values[0]
297335

298336
$tab_stack = $null
@@ -304,7 +342,7 @@ function Show-AnyBox
304342
$tab_stack.Margin = '5, 0, 5, 0'
305343
}
306344

307-
$_.Group | Group-Object -Property 'Group' | sort Name | foreach {
345+
$_.Group | Group-Object -Property 'Group' | Sort-Object Name | ForEach-Object {
308346

309347
$groupName = $_.Values[0]
310348

@@ -368,12 +406,12 @@ function Show-AnyBox
368406
$inBox.VerticalAlignment = 'Center'
369407
$inBox.VerticalContentAlignment = 'Center'
370408

371-
$prmpt.ValidateSet | foreach {
409+
$prmpt.ValidateSet | ForEach-Object {
372410
$null = $inBox.Items.Add((New-TextBlock -RefForm ([ref]$form) -Text $_ -FontFamily $prmpt.FontFamily -FontSize $prmpt.FontSize -FontColor 'Black' -Margin 0 -ContentAlignment $prmpt.Alignment))
373411
}
374412

375413
if ($prmpt.DefaultValue) {
376-
$inBox.SelectedItem = $inBox.Items | where { $_.Text -eq $prmpt.DefaultValue } | select -First 1
414+
$inBox.SelectedItem = $inBox.Items | Where-Object { $_.Text -eq $prmpt.DefaultValue } | Select-Object -First 1
377415
}
378416

379417
$inBox.add_SelectionChanged({
@@ -388,7 +426,7 @@ function Show-AnyBox
388426
$inBox.Orientation = 'Horizontal'
389427
}
390428

391-
$prmpt.ValidateSet | foreach {
429+
$prmpt.ValidateSet | ForEach-Object {
392430
$r = New-Object System.Windows.Controls.RadioButton
393431
if ($prmpt.RadioGroup) {
394432
$r.GroupName = $prmpt.RadioGroup
@@ -942,7 +980,7 @@ function Show-AnyBox
942980
}
943981
}
944982

945-
$Buttons | where { $_ -is [string] -or -not $_.OnClick } | ForEach-Object {
983+
$Buttons | Where-Object { $_ -is [string] -or -not $_.OnClick } | ForEach-Object {
946984
if ($_ -is [AnyBox.Button]) {
947985
if ($_.Name) {
948986
$form.Result.Add($_.Name, $false)
@@ -960,7 +998,7 @@ function Show-AnyBox
960998

961999
[uint16]$c = 0
9621000

963-
1..$ButtonRows | foreach {
1001+
1..$ButtonRows | ForEach-Object {
9641002
# Create a horizontal stack-panel for buttons and populate it.
9651003
$btnStack = New-Object System.Windows.Controls.StackPanel
9661004
$btnStack.Orientation = 'Horizontal'
@@ -1073,7 +1111,7 @@ $form.Result | Foreach-Object -Process {{
10731111
}
10741112

10751113
if ($PrepScript) {
1076-
$null = $form | foreach -Process $PrepScript
1114+
$null = $form | ForEach-Object -Process $PrepScript
10771115
}
10781116

10791117
if ($GridData) {

Types/AnyBox.ps1

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,56 @@
1-
Add-Type -TypeDefinition @"
2-
namespace AnyBox {
1+
Add-Type -ReferencedAssemblies 'System.Management.Automation.dll','System.Drawing.dll','WPF\PresentationFramework.dll','WPF\PresentationCore.dll','WPF\WindowsBase.dll','System.Xaml.dll' -TypeDefinition @"
2+
using System.Management.Automation;
3+
using System.Windows.Media;
4+
using System.Drawing;
5+
6+
namespace AnyBox
7+
{
38
public enum InputType {
49
None, Text, FileOpen, FileSave, FolderOpen, Checkbox, Password, Date, Link
510
};
611
712
public enum MessagePosition { Top, Left };
813
public enum SetPresentation { ComboBox, Radio, Radio_Wide };
14+
public enum DataGridSelectionMode { None, SingleCell, SingleRow, MultiRow };
15+
16+
public class AnyBox
17+
{
18+
public string Icon;
19+
public string Title;
20+
public string Image;
21+
public string[] Message;
22+
public object[] Prompts;
23+
public object[] Buttons;
24+
public string CancelButton;
25+
public string DefaultButton;
26+
public System.UInt16 ButtonRows = 1;
27+
public string[] Comment;
28+
public string ContentAlignment = "Left";
29+
public bool CollapsibleGroups;
30+
public bool CollapsedGroups;
31+
public System.Management.Automation.ScriptBlock PrepScript;
32+
public System.Windows.Media.FontFamily FontFamily = new System.Windows.Media.FontFamily("Segoe UI");
33+
public System.UInt16 FontSize = 12;
34+
public System.Windows.Media.Brush FontColor = System.Windows.Media.Brushes.Black;
35+
public System.Windows.Media.Brush BackgroundColor;
36+
public System.Windows.Media.Brush AccentColor = System.Windows.Media.Brushes.Gainsboro;
37+
public System.Windows.WindowStyle WindowStyle = System.Windows.WindowStyle.SingleBorderWindow;
38+
public System.Windows.ResizeMode ResizeMode = System.Windows.ResizeMode.CanMinimize;
39+
public bool NoResize;
40+
public System.UInt16 MinHeight = 50;
41+
public System.UInt16 MinWidth = 50;
42+
public System.UInt16 MaxHeight = 0;
43+
public System.UInt16 MaxWidth = 0;
44+
public bool Topmost;
45+
public bool HideTaskbarIcon;
46+
public System.UInt32 Timeout;
47+
public bool Countdown;
48+
public System.Windows.Window ParentWindow;
49+
public object[] GridData;
50+
public bool GridAsList;
51+
public DataGridSelectionMode SelectionMode = DataGridSelectionMode.SingleCell;
52+
public bool NoGridSearch;
53+
}
954
1055
public class Prompt
1156
{
@@ -42,4 +87,4 @@ namespace AnyBox {
4287
public System.Management.Automation.ScriptBlock OnClick;
4388
}
4489
}
45-
"@
90+
"@ -ErrorAction 'Stop'

0 commit comments

Comments
 (0)