2021-02-08 08:43:03 +08:00
[ System.Reflection.Assembly ] :: LoadWithPartialName ( 'System.Windows.Forms' ) | Out-Null
2023-08-05 17:36:27 +08:00
$ICON_ON = [ System.Drawing.Icon ] :: FromHandle ( ( [ System.Drawing.Bitmap ] :: FromFile ( $ ( Join-Path $PSScriptRoot '\icon_on.png' ) ) ) . GetHicon ( ) ) # Convert my png (bitmap) to an icon
$ICON_OFF = [ System.Drawing.Icon ] :: FromHandle ( ( [ System.Drawing.Bitmap ] :: FromFile ( $ ( Join-Path $PSScriptRoot '\icon_off.png' ) ) ) . GetHicon ( ) )
2021-02-08 13:59:26 +08:00
# $ICON_ON = [System.Drawing.Icon]::ExtractAssociatedIcon() # Feel free to use a system icon
# $ICON_OFF = [System.Drawing.Icon]::ExtractAssociatedIcon()
2021-02-08 08:43:03 +08:00
function NewTrayIcon ( ) {
$TrayIcon = New-Object System . Windows . Forms . NotifyIcon
$TrayIcon . Text = " NumLock on "
2021-02-08 13:59:26 +08:00
$TrayIcon . Icon = If ( [ console ] :: numberlock ) { $ICON_ON } Else { $ICON_OFF }
$TrayIcon . Visible = $true
2021-02-08 08:43:03 +08:00
$TrayIcon . Add_Click ( { # In case our script stops working, allow user to kill icon so that their system tray isn't cluttered
$TrayIcon . Visible = $false
$TrayIcon . Dispose ( )
Stop-Process $pid
} )
return $TrayIcon
}
While ( $true ) {
While ( $TrayIcon . Text ) {
2021-02-08 13:59:26 +08:00
$TrayIcon . Icon = If ( [ console ] :: numberlock ) { $ICON_ON } Else { $ICON_OFF }
2021-02-08 08:43:03 +08:00
Start-Sleep -Seconds 1 # Set this to whatever polling rate you want.
}
$TrayIcon = NewTrayIcon
}