Skip to content

Instantly share code, notes, and snippets.

@SQL-MisterMagoo
Last active December 24, 2020 01:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SQL-MisterMagoo/1b2ac83f3d8e248091cf39d751212a1e to your computer and use it in GitHub Desktop.
Save SQL-MisterMagoo/1b2ac83f3d8e248091cf39d751212a1e to your computer and use it in GitHub Desktop.
Show system tray icon from poweshell
Add-Type -AssemblyName PresentationFramework, System.Drawing, System.Windows.Forms
$icon = New-Object System.Windows.Forms.NotifyIcon
$icon.icon = New-Object System.Drawing.Icon("someicon.ico")
$icon.Visible = $true
function Update-ViewCount {
# THANKS VEXX!
$viewcount = Get-Random -Min 10 -Max 90
[System.Drawing.Bitmap]$image = [System.Drawing.Bitmap]::New(16, 16)
$null = $image.SetResolution(96, 96)
[System.Drawing.Graphics]$surface = [System.Drawing.Graphics]::FromImage($image)
$color = [System.Drawing.Color]::White
$brush = [System.Drawing.SolidBrush]::New($color)
if ([int]$viewcount -gt 99) {
$weight = "Regular"
$fontsize = 8.5
} else {
$fontsize = 12
$weight = "Bold"
}
$font = [System.Drawing.Font]::New("Segoe UI", $fontsize, $weight, "Pixel")
$surface.DrawString($viewcount, $font, $brush, 0, 0)
$surface.Flush()
[System.Drawing.Icon]::FromHandle($image.GetHicon())
}
Add-Type -AssemblyName PresentationFramework, System.Drawing, System.Windows.Forms
$icon = New-Object System.Windows.Forms.NotifyIcon
$icon.icon = Update-ViewCount
$icon.Visible = $true
$icon.add_Click( {
if ($_.Button -eq [Windows.Forms.MouseButtons]::Right) {
$icon.Dispose()
}
}
)
$count = 1
do {
$icon.icon = Update-ViewCount
$count++
Start-Sleep -Seconds 3
} While ($count -le 5 -and $icon.icon)
$icon.Dispose()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment