Hi,
I still use PS Studio 2015 4.2.99, so I don't have access to the new systray module.
Also I don't know if that would help.
So, I'm trying to get it working with my current version, but the issue I have is while systray works, there is always the form showing somewhere.
Either when using ALT-TAB or minimized on the desktop above the start menu.
I'd like to have a systray icon only, no context menu, with a timer every 5s and nothing else.
Here just the relevant parts, without the actual function.
What am I missing?
Many thanks, Roland
[void][System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")
$form1 = New-Object System.Windows.Forms.form
$NotifyIcon = New-Object System.Windows.Forms.NotifyIcon
$Timer = New-Object System.Windows.Forms.Timer
$form1.ShowInTaskbar = $false
$NotifyIcon.Icon = $iconCorp
$Timer.Interval = 5000
$Timer.add_Tick({ GetConnectionStatus })
$Timer.start()
$NotifyIcon.Visible = $true
<some functions>
GetConnectionStatus
[void][System.Windows.Forms.Application]::Run($form1)
exit
Systray script without visible form
Forum rules
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
- mxtrinidad
- Posts: 399
- Last visit: Tue May 16, 2023 6:52 am
Re: Systray script without visible form
Due to this is custom coding for adding a system tray, I would suggest look for information about it under C#.
It will make it easier to adapt to PowerShell scripting.
I found a YouTube video that may give you enough information to make it work.
https://www.youtube.com/watch?v=xfOM1sIMlGw
Give it a try!
It will make it easier to adapt to PowerShell scripting.
I found a YouTube video that may give you enough information to make it work.
https://www.youtube.com/watch?v=xfOM1sIMlGw
Give it a try!
Re: Systray script without visible form
Here is a simple example:
There is no need to create a form to show an icon.
Code: Select all
function Show-NotifyIcon{
<#
.SYNOPSIS
Displays a NotifyIcon's balloon tip message in the taskbar's notification area.
.DESCRIPTION
Displays a NotifyIcon's a balloon tip message in the taskbar's notification area.
.PARAMETER NotifyIcon
The NotifyIcon control you wish to display.
.PARAMETER BalloonTipText
Sets the text to display in the balloon tip.
.PARAMETER BalloonTipTitle
Sets the Title to display in the balloon tip.
.PARAMETER BalloonTipIcon
The icon to display in the ballon tip.
.PARAMETER Timeout
The time the ToolTip Balloon will remain visible in milliseconds. Default: 0
#>
param(
[Parameter(Mandatory = $true, Position = 0)]
[ValidateNotNull()]
[System.Windows.Forms.NotifyIcon]$NotifyIcon,
[Parameter(Mandatory = $true, Position = 1)]
[ValidateNotNullOrEmpty()]
[String]$BalloonTipText,
[Parameter(Position = 2)]
[String]$BalloonTipTitle = '',
[Parameter(Position = 3)]
[System.Windows.Forms.ToolTipIcon]$BalloonTipIcon = 'None',
[Parameter(Position = 4)]
[int]$Timeout = 0
)
if($NotifyIcon.Icon -eq $null)
{
#Set a Default Icon otherwise the balloon will not show
$NotifyIcon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon([System.Windows.Forms.Application]::ExecutablePath)
}
$NotifyIcon.ShowBalloonTip($Timeout, $BalloonTipTitle, $BalloonTipText, $BalloonTipIcon)
}
Add-Type -AssemblyName System.Windows.Forms
$notifyicon1 = New-Object System.Windows.Forms.NotifyIcon
$notifyicon1.Visible = $true
Show-NotifyIcon -NotifyIcon $notifyicon1 -BalloonTipText 'ballon text' -BalloonTipTitle 'My title'
- mxtrinidad
- Posts: 399
- Last visit: Tue May 16, 2023 6:52 am
Re: Systray script without visible form
Events on NotifyIcon can be received using Register-ObjectEvent. The script will have to sit in a loop or be doing other work so it doesn't exit.
C# is much better at doing all of this. PS has some issues as the complexity of the problem and limitations of a single thread make management of tray icons much harder.
C# is much better at doing all of this. PS has some issues as the complexity of the problem and limitations of a single thread make management of tray icons much harder.
Re: Systray script without visible form
ok, I removed all the form content.
I don't think I need events, the timer is sufficient and actually the best choice I think to update the systray.
So, what's the best choice for just having it in a loop, doing nothing?
It's not supposed to be closed by the user, logoff or shutdown would take care.
Unless there is a better way to deal with closing while it's just doing nothing (besides the timer).
-Roland
I don't think I need events, the timer is sufficient and actually the best choice I think to update the systray.
So, what's the best choice for just having it in a loop, doing nothing?
It's not supposed to be closed by the user, logoff or shutdown would take care.
Unless there is a better way to deal with closing while it's just doing nothing (besides the timer).
-Roland