Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for capslockicon.ps

(view source code of capslockicon.ps as plain text)

  1. param (
  2. 	[parameter( Mandatory = $False, ValueFromPipeline = $True )]
  3. 	[string] $text
  4. )
  5.  
  6. if ( $text -eq "/?" ) {
  7. 	Clear-Host
  8. 	Write-Host "`nCapsLockIcon.ps1,  Version 1.01 for Windows"
  9. 	Write-Host "CapsLock key indicator in the System tray`n"
  10. 	Write-Host "Usage:`t. 'CapsLockIcon.ps1' [text]`n"
  11. 	Write-Host "Where:`ttext`tis the text on the indicator"
  12. 	Write-Host "`t`t(default: `"C`"; maximum length: 2)`n"
  13. 	Write-Host "Credit:`tCode to dynamically generate icons by Joshua Flanagan"
  14. 	Write-Host "`thttps://www.codeproject.com/Articles/7122/Dynamically-Generating-Icons-safely`n"
  15. 	Write-Host "Written by Rob van der Woude"
  16. 	Write-Host "https://www.robvanderwoude.com`n"
  17. 	exit
  18. } else {
  19. 	# Remove whitespace and special characters
  20. 	$text = $text -replace '\W', ''
  21. 	# Use no more than 2 characters
  22. 	if ( $text.Length -gt 2 )
  23. 	{
  24. 		$text = $text.Substring( 0, 2 )
  25. 	}
  26. }
  27. # Use default in case no valid text was supplied
  28. if ( [string]::IsNullOrWhiteSpace( $text ) ) {
  29. 	$text = "C"
  30. }
  31.  
  32. [void] [System.Reflection.Assembly]::LoadWithPartialName( "System.Drawing" )
  33. [void] [System.Reflection.Assembly]::LoadWithPartialName( "System.Windows.Forms" )
  34.  
  35. # Create bitmap
  36. $bitmap = [System.Drawing.Bitmap]::new( 16, 16 )
  37. # Define font
  38. $fontfamily = [System.Drawing.FontFamily]::GenericSansSerif
  39. $fontstyle = [System.Drawing.FontStyle]::Bold
  40. $font = [System.Drawing.Font]::new( $fontfamily, 8, $fontstyle )
  41. # Create graphic from bitmap
  42. $graphic = [System.Drawing.Graphics]::FromImage( $bitmap )
  43. # Create a circle in the graphic, fill this circle with color
  44. $blackbrush = [System.Drawing.Brushes]::Black
  45. $redbrush = [System.Drawing.Brushes]::Red
  46. $graphic.FillEllipse( $redbrush, 0, 0, 16, 16 )
  47. # Center text in circle
  48. $textsize = $graphic.MeasureString( $text, $font )
  49. # Resize font if text is too large to fit in icon
  50. if ( $textsize.Width -gt 14 ) {
  51. 	$font = [System.Drawing.Font]::new( $fontfamily, $font.Size * 0.8, $fontstyle )
  52. 	$textsize = $graphic.MeasureString( $text, $font )
  53. }
  54. $x = [Math]::Max( 0, [Math]::Floor( ( $bitmap.Width - $textsize.Width ) / 2 ) )
  55. $y = [Math]::Max( 0, [Math]::Ceiling( ( $bitmap.Height - $textsize.Height ) / 2 ) )
  56. $stringformat = [System.Drawing.StringFormat]::GenericDefault
  57. $graphic.DrawString( $text, $font, $blackbrush, $x, $y, $stringformat )
  58. # Create icon from graphic
  59. $icon = [System.Drawing.Icon]::FromHandle( $bitmap.GetHicon( ) )
  60. # Cleanup
  61. $graphic.Dispose( )
  62. $bitmap.Dispose( )
  63.  
  64. # Create new System Tray icon
  65. [void] [System.Reflection.Assembly]::LoadWithPartialName( "System.Windows.Forms" )
  66. $notifyIcon = New-Object System.Windows.Forms.NotifyIcon
  67. $notifyIcon.Icon = $icon
  68.  
  69. # Explain how to break free from the endless loop
  70. Write-Host "`nPress any key to stop monitoring CapsLock . . .  " -NoNewline
  71.  
  72. # Monitor CapsLock until a key is pressed in this console
  73. while ( !$Host.UI.RawUI.KeyAvailable ) {
  74. 	Start-Sleep -Seconds 1
  75. 	# Show System Tray icon only if CapsLock is ON
  76. 	$notifyIcon.Visible = [System.Windows.Forms.Control]::IsKeyLocked( [System.Windows.Forms.Keys]::CapsLock )
  77. }
  78.  
  79. # Remove the key from the keyboard buffer . . .
  80. [void] $Host.UI.RawUI.ReadKey( )
  81. # . . .  and wipe it from the screen
  82. Write-Host "`b `n"
  83.  
  84. # Cleanup
  85. $notifyIcon.Visible = $False
  86. # Only if variable "$icon" is of type "Icon" it should be disposed of
  87. if ( $icon.GetType( ) -eq [System.Drawing.Icon] ) {
  88. 	$icon.Dispose( )
  89. }
  90. $notifyIcon.Dispose( )
  91.  

page last modified: 2024-02-26; loaded in 0.0248 seconds