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.
Locked
RolandJ
Posts: 3
Joined: Thu Aug 30, 2018 12:23 am

Systray script without visible form

Post by RolandJ » Thu Aug 30, 2018 4:27 am

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
Site Admin
Posts: 333
Joined: Sun Mar 03, 2013 12:42 pm

Re: Systray script without visible form

Post by mxtrinidad » Thu Aug 30, 2018 5:58 am

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!

User avatar
jvierra
Posts: 13736
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Systray script without visible form

Post by jvierra » Thu Aug 30, 2018 7:05 am

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
Site Admin
Posts: 333
Joined: Sun Mar 03, 2013 12:42 pm

Re: Systray script without visible form

Post by mxtrinidad » Thu Aug 30, 2018 7:30 am

Awesome! Thanks JVierra

User avatar
jvierra
Posts: 13736
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Systray script without visible form

Post by jvierra » Thu Aug 30, 2018 7:36 am

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
Joined: Thu Aug 30, 2018 12:23 am

Re: Systray script without visible form

Post by RolandJ » Thu Aug 30, 2018 7:43 am

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

User avatar
jvierra
Posts: 13736
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Systray script without visible form

Post by jvierra » Thu Aug 30, 2018 7:52 am

Just wait.

while(1){sleep 5}
No timer needed.

RolandJ
Posts: 3
Joined: Thu Aug 30, 2018 12:23 am

Re: Systray script without visible form

Post by RolandJ » Mon Sep 10, 2018 4:21 am

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

while(1){sleep 5}
No timer needed.
thanks!

Locked