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
0 commit comments