Skip to content

Commit 51bad5f

Browse files
Merge branch '1.7.0'
2 parents 946d19a + dda7498 commit 51bad5f

14 files changed

+910
-47
lines changed

ConvertTo-WPFGrid.ps1

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Function ConvertTo-WPFGrid {
88
[cmdletbinding()]
99
[Alias("cwg")]
1010
[outputtype("none")]
11-
11+
1212
Param(
1313
[Parameter(ValueFromPipeline)]
1414
[psobject]$InputObject,
@@ -19,47 +19,47 @@ Function ConvertTo-WPFGrid {
1919
[int]$Height = 768,
2020
[switch]$CenterScreen
2121
)
22-
22+
2323
Begin {
24-
24+
2525
Write-Verbose "Starting $($MyInvocation.MyCommand)"
26-
27-
# ! It may not be necessary to add these types but it doesn't hurt to include them
26+
27+
# It may not be necessary to add these types but it doesn't hurt to include them
2828
Add-Type -AssemblyName PresentationFramework
2929
Add-Type -assemblyName PresentationCore
3030
Add-Type -AssemblyName WindowsBase
31-
31+
3232
# define a timer to automatically dismiss the form. The timer uses a 5 second interval tick
3333
if ($Timeout -gt 0) {
3434
Write-Verbose "Creating a timer"
3535
$timer = new-object System.Windows.Threading.DispatcherTimer
3636
$terminate = (Get-Date).AddSeconds($timeout)
3737
Write-verbose "Form will close at $terminate"
3838
$timer.Interval = [TimeSpan]"0:0:5.00"
39-
39+
4040
$timer.add_tick( {
4141
if ((Get-Date) -ge $terminate) {
4242
$timer.stop()
4343
$form.Close()
4444
}
4545
})
4646
}
47-
47+
4848
Write-Verbose "Defining form: $Title ($width x $height)"
49-
49+
5050
$form = New-Object System.Windows.Window
5151
#define what it looks like
5252
$form.Title = $Title
5353
$form.Height = $Height
5454
$form.Width = $Width
55-
55+
5656
if ($CenterScreen) {
5757
Write-Verbose "Form will be center screen"
5858
$form.WindowStartupLocation = [System.Windows.WindowStartupLocation]::CenterScreen
5959
}
6060
#define a handler when the form is loaded. The scriptblock uses variables defined later
6161
#in the script
62-
$form.add_Loaded( {
62+
$form.add_Loaded( {
6363
foreach ($col in $datagrid.Columns) {
6464
#because of the way I am loading data into the grid
6565
#it appears I need to set the sorting on each column
@@ -71,10 +71,10 @@ Function ConvertTo-WPFGrid {
7171
})
7272
#Create a stack panel to hold the datagrid
7373
$stack = New-object System.Windows.Controls.StackPanel
74-
74+
7575
#create a datagrid
7676
$datagrid = New-Object System.Windows.Controls.DataGrid
77-
77+
7878
$datagrid.VerticalAlignment = "Bottom"
7979
#adjust the size of the grid based on the form dimensions
8080
$datagrid.Height = $form.Height - 50
@@ -85,35 +85,35 @@ Function ConvertTo-WPFGrid {
8585
$datagrid.AutoGenerateColumns = $True
8686
#enable alternating color rows
8787
$datagrid.AlternatingRowBackground = "gainsboro"
88-
88+
8989
$stack.AddChild($datagrid)
9090
$form.AddChild($stack)
91-
91+
9292
#initialize an array to hold all processed objects
9393
$data = @()
9494
} #begin
95-
95+
9696
Process {
9797
#add each incoming object to the data array
98-
$data += $Inputobject
98+
$data += $Inputobject
9999
} #process
100-
100+
101101
End {
102102
Write-Verbose "Preparing form"
103103
$DataGrid.ItemsSource = $data
104-
104+
105105
#show the form
106106
If ($Timeout -gt 0) {
107107
Write-Verbose "Starting timer"
108108
$timer.IsEnabled = $True
109109
$Timer.Start()
110110
}
111-
111+
112112
Write-Verbose "Displaying form"
113113
$form.ShowDialog() | Out-Null
114-
114+
115115
write-verbose "Ending $($MyInvocation.MyCommand)"
116-
116+
117117
} #end
118-
118+
119119
} #close function

New-WPFMessageBox.ps1

Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
# display WPF equivalent of the VBSCriptMessageBox
2+
3+
4+
#todo: dynamic parameter to set button default?
5+
6+
Function New-WPFMessageBox {
7+
8+
[cmdletbinding(DefaultParameterSetName = "standard")]
9+
[alias("nmb")]
10+
[Outputtype([int], [boolean], [string])]
11+
12+
Param(
13+
[Parameter(Position = 0, Mandatory, HelpMessage = "Enter the text message to display.")]
14+
[string]$Message,
15+
[string]$Title = "Message",
16+
[ValidateSet("Information", "Warning", "Error", "Question", "Shield")]
17+
[string]$Icon = "Information",
18+
[Parameter(ParameterSetName = "standard")]
19+
[ValidateSet("OK", "OKCancel", "YesNo")]
20+
[string]$ButtonSet = "OK",
21+
[Parameter(ParameterSetName = "custom")]
22+
[System.Collections.Specialized.OrderedDictionary]$CustomButtonSet,
23+
[string]$Background,
24+
[switch]$Quiet
25+
)
26+
27+
# It may not be necessary to add these types but it doesn't hurt to include them
28+
# but if they can't be laoded then this function will never work anwyway
29+
Try {
30+
31+
Add-Type -AssemblyName PresentationFramework -ErrorAction stop
32+
Add-Type -assemblyName PresentationCore -ErrorAction stop
33+
Add-Type -AssemblyName WindowsBase -ErrorAction stop
34+
}
35+
Catch {
36+
Throw $_
37+
#make sure we abort
38+
return
39+
}
40+
$form = New-Object System.Windows.Window
41+
#define what it looks like
42+
$form.Title = $Title
43+
$form.Height = 200
44+
$form.Width = 300
45+
$form.WindowStartupLocation = [System.Windows.WindowStartupLocation]::CenterScreen
46+
47+
if ($Background) {
48+
Try {
49+
$form.Background = $Background
50+
}
51+
Catch {
52+
Write-Warning "See https://docs.microsoft.com/en-us/dotnet/api/system.windows.media.brushes?view=netframework-4.7.2 for help on selecting a proper color. You can enter a color by name or X11 color value."
53+
Throw $_
54+
}
55+
}
56+
57+
$grid = New-Object System.Windows.Controls.Grid
58+
59+
$img = New-Object System.Windows.Controls.Image
60+
$img.Source = Join-Path "$psscriptroot\icons" -ChildPath "$icon.png"
61+
$img.Width = 50
62+
$img.Height = 50
63+
$img.HorizontalAlignment = "left"
64+
$img.VerticalAlignment = "top"
65+
$img.Margin = "15,5,0,0"
66+
67+
$grid.AddChild($img)
68+
69+
$text = New-Object System.Windows.Controls.Textblock
70+
$text.text = $Message
71+
$text.Padding = 10
72+
$text.Width = 200
73+
$text.Height = 80
74+
$text.Margin = "75,5,0,0"
75+
$text.TextWrapping = [System.Windows.TextWrapping]::Wrap
76+
$text.VerticalAlignment = "top"
77+
78+
$text.HorizontalAlignment = "left"
79+
$grid.AddChild($text)
80+
81+
if ($PSCmdlet.ParameterSetName -eq 'standard') {
82+
83+
Switch ($ButtonSet) {
84+
85+
"OK" {
86+
$btn = New-Object System.Windows.Controls.Button
87+
$btn.Content = "_OK"
88+
$btn.Width = 75
89+
$btn.Height = 25
90+
$btn.Margin = "0,80,0,0"
91+
$btn.Add_click( {
92+
$form.close()
93+
$script:r = 1
94+
})
95+
$grid.AddChild($btn)
96+
$form.add_Loaded( {$btn.Focus()})
97+
} #ok
98+
"OKCancel" {
99+
$btnOK = New-Object System.Windows.Controls.Button
100+
$btnOK.Content = "_OK"
101+
$btnOK.Width = 75
102+
$btnOK.Height = 25
103+
104+
$btnOK.Margin = "-75,75,0,0"
105+
$btnOK.Add_click( {
106+
$form.close()
107+
$script:r = 1
108+
})
109+
$btnCan = New-Object System.Windows.Controls.Button
110+
$btnCan.Content = "_Cancel"
111+
$btnCan.Width = 75
112+
$btnCan.Height = 25
113+
$btnCan.Margin = "120,75,0,0"
114+
$btnCan.Add_click( {
115+
$form.close()
116+
$script:r = 0
117+
})
118+
$grid.AddChild($btnOK)
119+
$grid.AddChild($btnCan)
120+
$form.add_Loaded( { $btnOK.Focus()})
121+
} #okcancel
122+
"YesNo" {
123+
$btnY = New-Object System.Windows.Controls.Button
124+
$btnY.Content = "_Yes"
125+
$btnY.Width = 75
126+
$btnY.Height = 25
127+
$btnY.Margin = "-75,75,0,0"
128+
$btnY.Add_click( {
129+
$form.close()
130+
$script:r = $True
131+
})
132+
$btnNo = New-Object System.Windows.Controls.Button
133+
$btnNo.Content = "_No"
134+
$btnNo.Width = 75
135+
$btnNo.Height = 25
136+
$btnNo.Margin = "120,75,0,0"
137+
$btnNo.Add_click( {
138+
$form.close()
139+
$script:r = $False
140+
})
141+
$grid.AddChild($btnY)
142+
$grid.AddChild($btnNo)
143+
$form.add_Loaded( {$btnY.Focus()})
144+
} #yesno
145+
}
146+
}
147+
else {
148+
#create a custom set of buttons from the hashtable
149+
switch ($CustomButtonSet.keys.count) {
150+
1 {
151+
$btn = New-Object System.Windows.Controls.Button
152+
$btn.Content = "_$($customButtonSet.keys[0])"
153+
$btn.Width = 75
154+
$btn.Height = 25
155+
$btn.Margin = "0,80,0,0"
156+
$btn.Add_click( {
157+
$form.close()
158+
$script:r = $customButtonSet.values[0]
159+
})
160+
$grid.AddChild($btn)
161+
$form.add_Loaded( {$btn.Focus()})
162+
}
163+
2 {
164+
$btn1 = New-Object System.Windows.Controls.Button
165+
$btn1.Content = "_$($customButtonSet.GetEnumerator().name[0])"
166+
$btn1.Width = 75
167+
$btn1.Height = 25
168+
$btn1.Margin = "-75,75,0,0"
169+
$btn1.Add_click( {
170+
$form.close()
171+
$script:r = $customButtonSet[0]
172+
})
173+
$btn2 = New-Object System.Windows.Controls.Button
174+
$btn2.Content = "_$($customButtonSet.GetEnumerator().name[1])"
175+
$btn2.Width = 75
176+
$btn2.Height = 25
177+
$btn2.Margin = "120,75,0,0"
178+
$btn2.Add_click( {
179+
$form.close()
180+
$script:r = $customButtonSet[1]
181+
})
182+
$grid.AddChild($btn1)
183+
$grid.AddChild($btn2)
184+
$form.add_Loaded( {$btn1.Focus()})
185+
}
186+
3 {
187+
$btn1 = New-Object System.Windows.Controls.Button
188+
$btn1.Content = "_$($customButtonSet.GetEnumerator().name[0])"
189+
$btn1.Width = 75
190+
$btn1.Height = 25
191+
$btn1.Margin = "-175,75,0,0"
192+
$btn1.Add_click( {
193+
$form.close()
194+
$script:r = $customButtonSet[0]
195+
})
196+
$btn2 = New-Object System.Windows.Controls.Button
197+
$btn2.Content = "_$($customButtonSet.GetEnumerator().name[1])"
198+
$btn2.Width = 75
199+
$btn2.Height = 25
200+
$btn2.Margin = "0,75,0,0"
201+
$btn2.Add_click( {
202+
$form.close()
203+
$script:r = $customButtonSet[1]
204+
})
205+
$btn3 = New-Object System.Windows.Controls.Button
206+
$btn3.Content = "_$($customButtonSet.GetEnumerator().name[2])"
207+
$btn3.Width = 75
208+
$btn3.Height = 25
209+
$btn3.Margin = "175,75,0,0"
210+
$btn3.Add_click( {
211+
$form.close()
212+
$script:r = $customButtonSet[2]
213+
})
214+
$grid.AddChild($btn1)
215+
$grid.AddChild($btn2)
216+
$grid.AddChild($btn3)
217+
$form.add_Loaded( {$btn1.Focus()})
218+
}
219+
Default {
220+
Write-Warning "The form cannot accomodate more than 3 buttons."
221+
#bail out
222+
Return
223+
}
224+
}
225+
}
226+
227+
#display the form
228+
$form.AddChild($grid)
229+
$form.ShowDialog() | Out-Null
230+
231+
#write the button result to the pipeline if not using -Quiet
232+
if (-Not $Quiet) {
233+
$script:r
234+
}
235+
236+
} #end function

PSScriptTools.psd1

138 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)