Guest User

Untitled

a guest
Sep 16th, 2025
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Demo script to display a custom 'toast' notification
  2.  
  3. # Load required assemblies
  4. Add-Type -AssemblyName PresentationFramework, System.Windows.Forms
  5.  
  6. # User-populated variables
  7. $WindowHeight = 140
  8. $WindowWidth = 480
  9. $Title = "Title ?"
  10. $Text = "Toast Notification 123"
  11. $Timeout = 10
  12. $ImageHeight = 100
  13. $ImageWidth = 100
  14.  
  15. # Set screen working area, bounds and start and finish location of the window 'top' property (for animation)
  16. $workingArea = [System.Windows.Forms.Screen]::PrimaryScreen.WorkingArea
  17. $Bounds = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds
  18. $TopStart = $workingArea.Bottom
  19. $TopFinish = $workingArea.Bottom - ($WindowHeight + 10)
  20. $CloseFinish = $Bounds.Bottom
  21.  
  22. # Code to create a base64 string from an image file
  23. <#
  24. $File = "C:\Users\tjones\Pictures\smsagent.png"
  25. $Image = [System.Drawing.Image]::FromFile($File)
  26. $MemoryStream = New-Object System.IO.MemoryStream
  27. $Image.Save($MemoryStream, $Image.RawFormat)
  28. [System.Byte[]]$Bytes = $MemoryStream.ToArray()
  29. $Base64 = [System.Convert]::ToBase64String($Bytes)
  30. $Image.Dispose()
  31. $MemoryStream.Dispose()
  32. #>
  33.  
  34. <#
  35. data:image/jpeg;base64,
  36. #>
  37.  
  38. # Create the custom logo from Base64 string
  39. #$Base64 = "LONGSTRINGHEREFROMBASE64"
  40. $CustomImage = New-Object System.Windows.Media.Imaging.BitmapImage
  41. $CustomImage.BeginInit()
  42. $CustomImage.StreamSource = [System.IO.MemoryStream][System.Convert]::FromBase64String($Base64)
  43. $CustomImage.EndInit()
  44.  
  45. # Calculate element dimensions
  46. $MainStackWidth = $WindowWidth - 10
  47. $SecondStackWidth = $WindowWidth - $ImageWidth -10
  48. $TextBoxWidth = $SecondStackWidth - 30
  49.  
  50. # Define the notification UI in Xaml
  51. [XML]$Xaml = "
  52. <Window
  53.    xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
  54.    xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
  55.    Title='Druva Notification' Width='$WindowWidth' Height='$WindowHeight'
  56.    WindowStyle='None' AllowsTransparency='True' Background='Transparent' Topmost='True' Opacity='0.9'>
  57.    <Window.Resources>
  58.        <Storyboard x:Name='ClosingAnimation' x:Key='ClosingAnimation' >
  59.            <DoubleAnimation Duration='0:0:.5' Storyboard.TargetProperty='Top' From='$TopFinish' To='$CloseFinish' AccelerationRatio='.1'/>
  60.        </Storyboard>
  61.    </Window.Resources>
  62.    <Window.Triggers>
  63.        <EventTrigger RoutedEvent='Window.Loaded'>
  64.            <BeginStoryboard>
  65.                <Storyboard >
  66.                    <DoubleAnimation Duration='0:0:.5' Storyboard.TargetProperty='Top' From='$TopStart' To='$TopFinish' AccelerationRatio='.1'/>
  67.                </Storyboard>
  68.            </BeginStoryboard>
  69.        </EventTrigger>
  70.    </Window.Triggers>
  71.    <Grid>
  72.    <Border BorderThickness='0' Background='#333333'>
  73.      <StackPanel Margin='20,10,20,10' Orientation='Horizontal' Width='$MainStackWidth'>
  74.        <Image x:Name='Logo' Width='$ImageWidth' Height='$ImageHeight'/>
  75.        <StackPanel Width='$SecondStackWidth'>
  76.            <TextBox Margin='5' MaxWidth='$TextBoxWidth' Background='#333333' BorderThickness='0' IsReadOnly='True' Foreground='White' FontSize='20' Text='$Title' FontWeight='Bold' HorizontalContentAlignment='Center' Width='Auto' HorizontalAlignment='Stretch' IsHitTestVisible='False'/>
  77.            <TextBox Margin='5' MaxWidth='$TextBoxWidth' Background='#333333' BorderThickness='0' IsReadOnly='True' Foreground='LightGray' FontSize='16' Text='$Text' HorizontalContentAlignment='Left' TextWrapping='Wrap' IsHitTestVisible='False'/>
  78.        </StackPanel>
  79.      </StackPanel>
  80.    </Border>
  81.  </Grid>
  82. </Window>
  83. "
  84.  
  85. # Create a global hash table to add dispatcher to
  86. $Global:UI = @{}
  87.  
  88. # Create the window
  89. $Window = [Windows.Markup.XamlReader]::Load((New-Object -TypeName System.Xml.XmlNodeReader -ArgumentList $xaml))
  90.  
  91. # Set the image
  92. $Logo = $Window.FindName('Logo')
  93. $Logo.Source = $CustomImage
  94.  
  95. # Add the closing animation to the global variable
  96. $UI.ClosingAnimation = $Window.FindName('ClosingAnimation')
  97.  
  98. # Window loaded
  99. $Window.Add_Loaded({
  100.  
  101.     # Activate
  102.     $This.Activate()
  103.    
  104.     # Play a sound
  105.     $SoundFile = "$env:SystemDrive\Windows\Media\Windows Notify.wav"
  106.     $SoundPlayer = New-Object System.Media.SoundPlayer -ArgumentList $SoundFile
  107.     $SoundPlayer.Add_LoadCompleted({
  108.         $This.Play()
  109.         $This.Dispose()
  110.     })
  111.     $SoundPlayer.LoadAsync()
  112.  
  113.     # Set the location of the left property
  114.     $workingArea = [System.Windows.Forms.Screen]::PrimaryScreen.WorkingArea
  115.     $this.Left = $workingarea.Width - ($this.ActualWidth + 10)
  116.  
  117.     # Create a dispatcher timer to begin notification closure after x seconds
  118.     $UI.DispatcherTimer = New-Object -TypeName System.Windows.Threading.DispatcherTimer
  119.     $UI.DispatcherTimer.Interval = [TimeSpan]::FromSeconds($Timeout)
  120.     $UI.DispatcherTimer.Add_Tick({
  121.         $UI.ClosingAnimation.Begin($Window)
  122.     })
  123.     $UI.DispatcherTimer.Start()
  124.  
  125. })
  126.  
  127. # Window closing
  128. $Window.Add_Closing({
  129.     # Stop the dispatcher timer
  130.     $UI.DispatcherTimer.Stop()
  131. })
  132.  
  133. # Closing animation is completed
  134. $UI.ClosingAnimation.Add_Completed({
  135.     $Window.Close()
  136. })
  137.  
  138. # Window Mouse enter
  139. $Window.Add_MouseEnter({
  140.     # Change cursor to a hand
  141.     $This.Cursor = 'Hand'
  142. })
  143.  
  144. # Window mouse up (simulate click)
  145. $Window.Add_MouseUp({
  146.     # Open a web page, stop the dispatcher timer and close the notification
  147.     Start-Process "https://google.com"
  148.     $UI.DispatcherTimer.Stop()
  149.     $This.Close()
  150. })
  151.  
  152. # Display the notification
  153. $null = $window.Dispatcher.InvokeAsync{$window.ShowDialog()}.Wait()
Add Comment
Please, Sign In to add comment