Systray script without visible form

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
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.
This topic is 5 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
RolandJ
Posts: 3
Last visit: Mon Sep 10, 2018 6:06 am

Systray script without visible form

Post by RolandJ »

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
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: Systray script without visible form

Post by mxtrinidad »

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!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Systray script without visible form

Post by jvierra »

Here is a simple example:

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'
There is no need to create a form to show an icon.
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: Systray script without visible form

Post by mxtrinidad »

Awesome! Thanks JVierra
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Systray script without visible form

Post by jvierra »

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.
RolandJ
Posts: 3
Last visit: Mon Sep 10, 2018 6:06 am

Re: Systray script without visible form

Post by RolandJ »

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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Systray script without visible form

Post by jvierra »

Just wait.

while(1){sleep 5}
No timer needed.
RolandJ
Posts: 3
Last visit: Mon Sep 10, 2018 6:06 am

Re: Systray script without visible form

Post by RolandJ »

jvierra wrote: Thu Aug 30, 2018 7:52 am Just wait.

while(1){sleep 5}
No timer needed.
thanks!
This topic is 5 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked