Windows Tray Application

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.
Post Reply
laned101
Posts: 25
Joined: Thu Aug 16, 2018 3:36 am
Answers: 1

Windows Tray Application

Post by laned101 »

HI I have been playing around with some GUI aspects of PowerShell.

I have created a simple Windows Tray Application and would like to launch a new Windows Form Dialog from a button in the app menu. I cant seem to work out how to launch it from a click.

Code: Select all

$Options = {
	Add-Type -AssemblyName System.Windows.Forms

	$TestForm = New-Object system.Windows.Forms.Form

	$TestForm.ClientSize = '500,300'
	$TestForm.text = "Simple Gui"
	$TestForm.BackColor = "#ffffff"
	
	[void]$TestForm.ShowDialog()
}

$ErrorActionPreference = 'silentlyContinue'

if ($SAPIENHost -ne $null)
{
	$Menu = $SAPIENHost.GetContextMenuStrip()
	
	$Separator = New-Object System.Windows.Forms.ToolStripSeparator
	
	$StartMenuItem = New-Object System.Windows.Forms.ToolStripMenuItem
	$StartMenuItem.Name = "Options"
	$StartMenuItem.Text = "&Options"
	$StartMenuItem.Add_Click($Options)
	
	$Menu.Items.Insert(0, $StartMenuItem)
	$Menu.Items.Insert(1, $Separator)
	
	
	$SAPIENHost.SetMenuHandler($StartMenuItem, $Options)
	
}
while ($true)
{
	
}
by laned101 » Thu Nov 05, 2020 4:05 am
I have worked this out now!

It seems I didnt need to add the Add_Click event.

This works as expected now

Code: Select all

$Options = {
	Add-Type -AssemblyName System.Windows.Forms
	
	$TestForm = New-Object system.Windows.Forms.Form
	
	$TestForm.ClientSize = '500,300'
	$TestForm.text = "Simple Gui"
	$TestForm.BackColor = "#ffffff"
	
	[void]$TestForm.ShowDialog()
}

$ErrorActionPreference = 'silentlyContinue'

if ($SAPIENHost -ne $null)
{
	$Menu = $SAPIENHost.GetContextMenuStrip()
	
	$Separator = New-Object System.Windows.Forms.ToolStripSeparator
	
	$StartMenuItem = New-Object System.Windows.Forms.ToolStripMenuItem
	$StartMenuItem.Name = "Options"
	$StartMenuItem.Text = "&Options"
	
	$Menu.Items.Insert(0, $StartMenuItem)
	$Menu.Items.Insert(1, $Separator)
	
	$SAPIENHost.SetMenuHandler($StartMenuItem, $Options)
	
}

While ($true)
{
}
Go to full post

jvierra
Posts: 14611
Joined: Tue May 22, 2007 9:57 am
Answers: 2
Has voted: 1 time
Been upvoted: 3 times
Contact:

Re: Windows Tray Application

Post by jvierra »

You are not creating any event here. Just typing in some lines that appear to work does not create an even.

I recommend reading the articles in the Information Center on how to use the tray control to create events.

https://info.sapien.com/

laned101
Posts: 25
Joined: Thu Aug 16, 2018 3:36 am
Answers: 1

Re: Windows Tray Application

Post by laned101 »

Thanks for the response, searches in the information center around those key words doesnt yield many results. And those results dont seem to be very fruitful. Could you be more specific please?

jvierra
Posts: 14611
Joined: Tue May 22, 2007 9:57 am
Answers: 2
Has voted: 1 time
Been upvoted: 3 times
Contact:

Re: Windows Tray Application

Post by jvierra »

Just read the article on the "NotifyIcon" control.

https://info.sapien.com/index.php/guis/ ... NvbidzIl0=

laned101
Posts: 25
Joined: Thu Aug 16, 2018 3:36 am
Answers: 1

Re: Windows Tray Application

Post by laned101 »

Thank you, I have taken a look and the only event I can see that remotely fits with what I am trying to achieve is the MouseClick event. However this is only for the Tray Icon, this doesnt seem to have any events for clicking icons on the the menu of the tray icon (once clicked). Here is a description of the event from the page.

"
MouseClick

This event occurs when a user clicks on the system tray icon."

You suggested my original post had no events and that me just typing things wasnt going to make one. The entries I have made are not just pulled from thin air, they are given as an example in some of Sapiens posts and I believed the Add_Click was a click event.

I will continue googling and searching, thanks for trying to help non the less.

laned101
Posts: 25
Joined: Thu Aug 16, 2018 3:36 am
Answers: 1

Re: Windows Tray Application

Post by laned101 »

I have worked this out now!

It seems I didnt need to add the Add_Click event.

This works as expected now

Code: Select all

$Options = {
	Add-Type -AssemblyName System.Windows.Forms
	
	$TestForm = New-Object system.Windows.Forms.Form
	
	$TestForm.ClientSize = '500,300'
	$TestForm.text = "Simple Gui"
	$TestForm.BackColor = "#ffffff"
	
	[void]$TestForm.ShowDialog()
}

$ErrorActionPreference = 'silentlyContinue'

if ($SAPIENHost -ne $null)
{
	$Menu = $SAPIENHost.GetContextMenuStrip()
	
	$Separator = New-Object System.Windows.Forms.ToolStripSeparator
	
	$StartMenuItem = New-Object System.Windows.Forms.ToolStripMenuItem
	$StartMenuItem.Name = "Options"
	$StartMenuItem.Text = "&Options"
	
	$Menu.Items.Insert(0, $StartMenuItem)
	$Menu.Items.Insert(1, $Separator)
	
	$SAPIENHost.SetMenuHandler($StartMenuItem, $Options)
	
}

While ($true)
{
}

User avatar
JackMayhoffer
Posts: 2
Joined: Fri Sep 05, 2014 11:19 am

Re: Windows Tray Application

Post by JackMayhoffer »

laned101 wrote:
Thu Nov 05, 2020 4:05 am
I have worked this out now!

It seems I didnt need to add the Add_Click event.

This works as expected now

Code: Select all

$Options = {
	Add-Type -AssemblyName System.Windows.Forms
	
	$TestForm = New-Object system.Windows.Forms.Form
	
	$TestForm.ClientSize = '500,300'
	$TestForm.text = "Simple Gui"
	$TestForm.BackColor = "#ffffff"
	
	[void]$TestForm.ShowDialog()
}

$ErrorActionPreference = 'silentlyContinue'

if ($SAPIENHost -ne $null)
{
	$Menu = $SAPIENHost.GetContextMenuStrip()
	
	$Separator = New-Object System.Windows.Forms.ToolStripSeparator
	
	$StartMenuItem = New-Object System.Windows.Forms.ToolStripMenuItem
	$StartMenuItem.Name = "Options"
	$StartMenuItem.Text = "&Options"
	
	$Menu.Items.Insert(0, $StartMenuItem)
	$Menu.Items.Insert(1, $Separator)
	
	$SAPIENHost.SetMenuHandler($StartMenuItem, $Options)
	
}

While ($true)
{
}
Thanks, this helped a lot.

laned101
Posts: 25
Joined: Thu Aug 16, 2018 3:36 am
Answers: 1

Re: Windows Tray Application

Post by laned101 »

JackMayhoffer wrote:
Fri Nov 13, 2020 1:43 pm
Thanks, this helped a lot.
You're welcome

Post Reply