2024-01-15 11:32:19 -06:00
function Show-CustomDialog {
<#
. SYNOPSIS
Displays a custom dialog box with an image , heading , message , and an OK button .
2024-06-28 17:15:39 -05:00
2024-01-15 11:32:19 -06:00
. DESCRIPTION
This function creates a custom dialog box with the specified message and additional elements such as an image , heading , and an OK button . The dialog box is designed with a green border , rounded corners , and a black background .
2024-06-28 17:15:39 -05:00
2024-10-07 09:19:42 -05:00
. PARAMETER Title
The Title to use for the dialog window ' s Title Bar , this will not be visible by the user , as window styling is set to None .
2024-01-15 11:32:19 -06:00
. PARAMETER Message
The message to be displayed in the dialog box .
. PARAMETER Width
The width of the custom dialog window .
. PARAMETER Height
The height of the custom dialog window .
2024-06-28 17:15:39 -05:00
2024-07-08 14:59:58 -05:00
. PARAMETER FontSize
2024-10-07 09:19:42 -05:00
The Font Size of message shown inside custom dialog window .
2024-07-08 14:59:58 -05:00
. PARAMETER HeaderFontSize
2024-10-07 09:19:42 -05:00
The Font Size for the Header of custom dialog window .
. PARAMETER LogoSize
The Size of the Logo used inside the custom dialog window .
. PARAMETER ForegroundColor
The Foreground Color of dialog window title & message .
. PARAMETER BackgroundColor
The Background Color of dialog window .
. PARAMETER BorderColor
The Color for dialog window border .
. PARAMETER ButtonBackgroundColor
The Background Color for Buttons in dialog window .
. PARAMETER ButtonForegroundColor
The Foreground Color for Buttons in dialog window .
. PARAMETER ShadowColor
The Color used when creating the Drop-down Shadow effect for dialog window .
. PARAMETER LogoColor
The Color of WinUtil Text found next to WinUtil ' s Logo inside dialog window .
. PARAMETER LinkForegroundColor
The Foreground Color for Links inside dialog window .
2024-07-08 14:59:58 -05:00
2024-10-07 09:19:42 -05:00
. PARAMETER LinkHoverForegroundColor
The Foreground Color for Links when the mouse pointer hovers over them inside dialog window .
2024-07-08 14:59:58 -05:00
2024-07-14 18:50:40 -05:00
. PARAMETER EnableScroll
A flag indicating whether to enable scrolling if the content exceeds the window size .
2024-01-15 11:32:19 -06:00
. EXAMPLE
2024-10-07 09:19:42 -05:00
Show-CustomDialog -Title " My Custom Dialog " -Message " This is a custom dialog with a message and an image above. " -Width 300 -Height 200
Makes a new Custom Dialog with the title 'My Custom Dialog' and a message 'This is a custom dialog with a message and an image above.' , with dimensions of 300 by 200 pixels .
Other styling options are grabbed from '$sync.Form.Resources' global variable .
. EXAMPLE
$foregroundColor = New-Object System . Windows . Media . SolidColorBrush ( " #0088e5 " )
$backgroundColor = New-Object System . Windows . Media . SolidColorBrush ( " #1e1e1e " )
$linkForegroundColor = New-Object System . Windows . Media . SolidColorBrush ( " #0088e5 " )
$linkHoverForegroundColor = New-Object System . Windows . Media . SolidColorBrush ( " #005289 " )
Show-CustomDialog -Title " My Custom Dialog " -Message " This is a custom dialog with a message and an image above. " -Width 300 -Height 200 -ForegroundColor $foregroundColor -BackgroundColor $backgroundColor -LinkForegroundColor $linkForegroundColor -LinkHoverForegroundColor $linkHoverForegroundColor
Makes a new Custom Dialog with the title 'My Custom Dialog' and a message 'This is a custom dialog with a message and an image above.' , with dimensions of 300 by 200 pixels , with a link foreground ( and general foreground ) colors of '#0088e5' , background color of '#1e1e1e' , and Link Color on Hover of '005289' , all of which are in Hexadecimal ( the '#' Symbol is required by SolidColorBrush Constructor ) .
Other styling options are grabbed from '$sync.Form.Resources' global variable .
2024-06-28 17:15:39 -05:00
2024-01-15 11:32:19 -06:00
#>
param (
2024-10-07 09:19:42 -05:00
[ string ] $Title ,
2024-01-15 11:32:19 -06:00
[ string ] $Message ,
2024-09-20 08:34:10 -05:00
[ int ] $Width = $sync . Form . Resources . CustomDialogWidth ,
[ int ] $Height = $sync . Form . Resources . CustomDialogHeight ,
2024-10-07 09:19:42 -05:00
[ System.Windows.Media.FontFamily ] $FontFamily = $sync . Form . Resources . FontFamily ,
2024-09-20 08:34:10 -05:00
[ int ] $FontSize = $sync . Form . Resources . CustomDialogFontSize ,
[ int ] $HeaderFontSize = $sync . Form . Resources . CustomDialogFontSizeHeader ,
2024-10-07 09:19:42 -05:00
[ int ] $LogoSize = $sync . Form . Resources . CustomDialogLogoSize ,
[ System.Windows.Media.Color ] $ShadowColor = " #AAAAAAAA " ,
[ System.Windows.Media.SolidColorBrush ] $LogoColor = $sync . Form . Resources . LabelboxForegroundColor ,
[ System.Windows.Media.SolidColorBrush ] $BorderColor = $sync . Form . Resources . BorderColor ,
[ System.Windows.Media.SolidColorBrush ] $ForegroundColor = $sync . Form . Resources . MainForegroundColor ,
[ System.Windows.Media.SolidColorBrush ] $BackgroundColor = $sync . Form . Resources . MainBackgroundColor ,
[ System.Windows.Media.SolidColorBrush ] $ButtonForegroundColor = $sync . Form . Resources . ButtonInstallForegroundColor ,
[ System.Windows.Media.SolidColorBrush ] $ButtonBackgroundColor = $sync . Form . Resources . ButtonInstallBackgroundColor ,
[ System.Windows.Media.SolidColorBrush ] $LinkForegroundColor = $sync . Form . Resources . LinkForegroundColor ,
[ System.Windows.Media.SolidColorBrush ] $LinkHoverForegroundColor = $sync . Form . Resources . LinkHoverForegroundColor ,
2024-07-14 18:50:40 -05:00
[ bool ] $EnableScroll = $false
2024-01-15 11:32:19 -06:00
)
# Create a custom dialog window
$dialog = New-Object Windows . Window
2024-10-07 09:19:42 -05:00
$dialog . Title = $Title
2024-01-15 11:32:19 -06:00
$dialog . Height = $Height
$dialog . Width = $Width
$dialog . Margin = New-Object Windows . Thickness ( 10 ) # Add margin to the entire dialog box
$dialog . WindowStyle = [ Windows.WindowStyle ] :: None # Remove title bar and window controls
$dialog . ResizeMode = [ Windows.ResizeMode ] :: NoResize # Disable resizing
$dialog . WindowStartupLocation = [ Windows.WindowStartupLocation ] :: CenterScreen # Center the window
2024-10-07 09:19:42 -05:00
$dialog . Foreground = $ForegroundColor
$dialog . Background = $BackgroundColor
$dialog . FontFamily = $FontFamily
2024-07-08 14:59:58 -05:00
$dialog . FontSize = $FontSize
2024-01-15 11:32:19 -06:00
# Create a Border for the green edge with rounded corners
$border = New-Object Windows . Controls . Border
2024-10-07 09:19:42 -05:00
$border . BorderBrush = $BorderColor
2024-01-15 11:32:19 -06:00
$border . BorderThickness = New-Object Windows . Thickness ( 1 ) # Adjust border thickness as needed
$border . CornerRadius = New-Object Windows . CornerRadius ( 10 ) # Adjust the radius for rounded corners
# Create a drop shadow effect
$dropShadow = New-Object Windows . Media . Effects . DropShadowEffect
$dropShadow . Color = $shadowColor
$dropShadow . Direction = 270
$dropShadow . ShadowDepth = 5
$dropShadow . BlurRadius = 10
# Apply drop shadow effect to the border
$dialog . Effect = $dropShadow
$dialog . Content = $border
# Create a grid for layout inside the Border
$grid = New-Object Windows . Controls . Grid
$border . Child = $grid
2024-10-07 09:19:42 -05:00
# Uncomment the following line to show gridlines
2024-01-15 11:32:19 -06:00
#$grid.ShowGridLines = $true
# Add the following line to set the background color of the grid
$grid . Background = [ Windows.Media.Brushes ] :: Transparent
# Add the following line to make the Grid stretch
$grid . HorizontalAlignment = [ Windows.HorizontalAlignment ] :: Stretch
$grid . VerticalAlignment = [ Windows.VerticalAlignment ] :: Stretch
# Add the following line to make the Border stretch
$border . HorizontalAlignment = [ Windows.HorizontalAlignment ] :: Stretch
$border . VerticalAlignment = [ Windows.VerticalAlignment ] :: Stretch
# Set up Row Definitions
$row0 = New-Object Windows . Controls . RowDefinition
$row0 . Height = [ Windows.GridLength ] :: Auto
$row1 = New-Object Windows . Controls . RowDefinition
$row1 . Height = [ Windows.GridLength ] :: new ( 1 , [ Windows.GridUnitType ] :: Star )
$row2 = New-Object Windows . Controls . RowDefinition
$row2 . Height = [ Windows.GridLength ] :: Auto
# Add Row Definitions to Grid
$grid . RowDefinitions . Add ( $row0 )
$grid . RowDefinitions . Add ( $row1 )
$grid . RowDefinitions . Add ( $row2 )
2024-06-28 17:15:39 -05:00
2024-01-15 11:32:19 -06:00
# Add StackPanel for horizontal layout with margins
$stackPanel = New-Object Windows . Controls . StackPanel
$stackPanel . Margin = New-Object Windows . Thickness ( 10 ) # Add margins around the stack panel
$stackPanel . Orientation = [ Windows.Controls.Orientation ] :: Horizontal
$stackPanel . HorizontalAlignment = [ Windows.HorizontalAlignment ] :: Left # Align to the left
$stackPanel . VerticalAlignment = [ Windows.VerticalAlignment ] :: Top # Align to the top
$grid . Children . Add ( $stackPanel )
[ Windows.Controls.Grid ] :: SetRow ( $stackPanel , 0 ) # Set the row to the second row (0-based index)
# Add SVG path to the stack panel
2024-10-07 09:19:42 -05:00
$stackPanel . Children . Add ( ( Invoke-WinUtilAssets -Type " logo " -Size $LogoSize ) )
2024-01-15 11:32:19 -06:00
# Add "Winutil" text
$winutilTextBlock = New-Object Windows . Controls . TextBlock
$winutilTextBlock . Text = " Winutil "
2024-07-08 14:59:58 -05:00
$winutilTextBlock . FontSize = $HeaderFontSize
2024-10-07 09:19:42 -05:00
$winutilTextBlock . Foreground = $LogoColor
2024-09-09 20:19:34 -05:00
$winutilTextBlock . Margin = New-Object Windows . Thickness ( 10 , 10 , 10 , 5 ) # Add margins around the text block
2024-01-15 11:32:19 -06:00
$stackPanel . Children . Add ( $winutilTextBlock )
# Add TextBlock for information with text wrapping and margins
$messageTextBlock = New-Object Windows . Controls . TextBlock
2024-10-07 09:19:42 -05:00
$messageTextBlock . FontSize = $FontSize
2024-01-15 11:32:19 -06:00
$messageTextBlock . TextWrapping = [ Windows.TextWrapping ] :: Wrap # Enable text wrapping
$messageTextBlock . HorizontalAlignment = [ Windows.HorizontalAlignment ] :: Left
$messageTextBlock . VerticalAlignment = [ Windows.VerticalAlignment ] :: Top
$messageTextBlock . Margin = New-Object Windows . Thickness ( 10 ) # Add margins around the text block
2024-06-25 13:54:18 -05:00
# Define the Regex to find hyperlinks formatted as HTML <a> tags
$regex = [ regex ] :: new ( '<a href="([^"]+)">([^<]+)</a>' )
$lastPos = 0
# Iterate through each match and add regular text and hyperlinks
foreach ( $match in $regex . Matches ( $Message ) ) {
# Add the text before the hyperlink, if any
$textBefore = $Message . Substring ( $lastPos , $match . Index - $lastPos )
if ( $textBefore . Length -gt 0 ) {
$messageTextBlock . Inlines . Add ( ( New-Object Windows . Documents . Run ( $textBefore ) ) )
}
# Create and add the hyperlink
$hyperlink = New-Object Windows . Documents . Hyperlink
$hyperlink . NavigateUri = New-Object System . Uri ( $match . Groups [ 1 ] . Value )
$hyperlink . Inlines . Add ( $match . Groups [ 2 ] . Value )
$hyperlink . TextDecorations = [ Windows.TextDecorations ] :: None # Remove underline
2024-10-07 09:19:42 -05:00
$hyperlink . Foreground = $LinkForegroundColor
2024-08-06 15:50:36 -05:00
2024-06-25 13:54:18 -05:00
$hyperlink . Add_Click ( {
param ( $sender , $args )
Start-Process $sender . NavigateUri . AbsoluteUri
} )
$hyperlink . Add_MouseEnter ( {
param ( $sender , $args )
2024-10-07 09:19:42 -05:00
$sender . Foreground = $LinkHoverForegroundColor
$sender . FontSize = ( $FontSize + ( $FontSize / 4 ) )
$sender . FontWeight = " SemiBold "
2024-06-25 13:54:18 -05:00
} )
$hyperlink . Add_MouseLeave ( {
param ( $sender , $args )
2024-10-07 09:19:42 -05:00
$sender . Foreground = $LinkForegroundColor
$sender . FontSize = $FontSize
$sender . FontWeight = " Normal "
2024-06-25 13:54:18 -05:00
} )
2024-06-28 17:15:39 -05:00
2024-06-25 13:54:18 -05:00
$messageTextBlock . Inlines . Add ( $hyperlink )
# Update the last position
$lastPos = $match . Index + $match . Length
}
# Add any remaining text after the last hyperlink
if ( $lastPos -lt $Message . Length ) {
$textAfter = $Message . Substring ( $lastPos )
$messageTextBlock . Inlines . Add ( ( New-Object Windows . Documents . Run ( $textAfter ) ) )
}
# If no matches, add the entire message as a run
if ( $regex . Matches ( $Message ) . Count -eq 0 ) {
$messageTextBlock . Inlines . Add ( ( New-Object Windows . Documents . Run ( $Message ) ) )
}
2024-07-14 18:50:40 -05:00
# Create a ScrollViewer if EnableScroll is true
if ( $EnableScroll ) {
$scrollViewer = New-Object System . Windows . Controls . ScrollViewer
$scrollViewer . VerticalScrollBarVisibility = 'Auto'
$scrollViewer . HorizontalScrollBarVisibility = 'Disabled'
$scrollViewer . Content = $messageTextBlock
$grid . Children . Add ( $scrollViewer )
[ Windows.Controls.Grid ] :: SetRow ( $scrollViewer , 1 ) # Set the row to the second row (0-based index)
} else {
$grid . Children . Add ( $messageTextBlock )
[ Windows.Controls.Grid ] :: SetRow ( $messageTextBlock , 1 ) # Set the row to the second row (0-based index)
}
2024-06-25 13:54:18 -05:00
2024-01-15 11:32:19 -06:00
# Add OK button
$okButton = New-Object Windows . Controls . Button
$okButton . Content = " OK "
2024-07-08 14:59:58 -05:00
$okButton . FontSize = $FontSize
2024-01-15 11:32:19 -06:00
$okButton . Width = 80
$okButton . Height = 30
$okButton . HorizontalAlignment = [ Windows.HorizontalAlignment ] :: Center
$okButton . VerticalAlignment = [ Windows.VerticalAlignment ] :: Bottom
$okButton . Margin = New-Object Windows . Thickness ( 0 , 0 , 0 , 10 )
$okButton . Background = $buttonBackgroundColor
$okButton . Foreground = $buttonForegroundColor
2024-10-07 09:19:42 -05:00
$okButton . BorderBrush = $BorderColor
2024-01-15 11:32:19 -06:00
$okButton . Add_Click ( {
$dialog . Close ( )
} )
$grid . Children . Add ( $okButton )
[ Windows.Controls.Grid ] :: SetRow ( $okButton , 2 ) # Set the row to the third row (0-based index)
# Handle Escape key press to close the dialog
$dialog . Add_KeyDown ( {
if ( $_ . Key -eq 'Escape' ) {
$dialog . Close ( )
}
} )
# Set the OK button as the default button (activated on Enter)
$okButton . IsDefault = $true
# Show the custom dialog
$dialog . ShowDialog ( )
}