|
-
Feb 24th, 2007, 08:29 PM
#1
Minimise Form to System Tray on Close (.NET 2.0)
C# version here.
In previous versions minimising to the system tray instead of closing a form was a hassle. You had to mess about checking whether the system was shutting down, etc. or else your app would prevent Windows from ever closing. In .NET 2.0 that process is simplified consideralbly:
VB Code:
Private Sub Form1_FormClosing(ByVal sender As Object, _ ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing If e.CloseReason = CloseReason.UserClosing Then 'The user has requested the form be closed so mimimise to the system tray instead. e.Cancel = True Me.Visible = False Me.NotifyIcon1.Visible = True End If End Sub
This will minimise the form to the system tray if the user requests the close by clicking the Close button on the title bar, selecting Close from the system menu, pressing Alt+F4 or performing some action that causes your code to call the form's Close method. All other reasons for closure, including calling Application.Exit or Windows shutting down, will cause the form to actually close.
I only just discovered the CloseReason property myself, so I thought I'd share it as others may not be aware of it either.
Last edited by jmcilhinney; Oct 30th, 2008 at 08:25 PM.
-
Feb 24th, 2007, 10:20 PM
#2
Re: Minimise Form to System Tray on Close (.NET 2.0)
Here's a more thorough look at the different reasons a form could be closing:
VB Code:
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Select Case e.CloseReason
Case CloseReason.ApplicationExitCall
'The user perfromed an action that caused your code to call Application.Exit.
'You could go either way here but I'd suggest that you shouldn't be calling Application.Exit
'if you don't actually want the application to exit. In that case close the main form instead.
e.Cancel = False
Case CloseReason.FormOwnerClosing
'This form is a modeless dialogue, which you shouldn't be minimising to the system tray anyway.
e.Cancel = False
Case CloseReason.MdiFormClosing
'This form is an MDI child form, which you shouldn't be minimising to the system tray anyway.
e.Cancel = False
Case CloseReason.None
'If the reason can't be determined then something funky is going on so I'd suggest you let the form close.
e.Cancel = False
Case CloseReason.TaskManagerClosing
'The user pressed the End Task button on the Applications tab (NOT the Processes tab) of the Task Manager.
'You could go either way here too. It really depends on your app and if you don't want the user to be able to exit
'this way. I'd suggest letting the form close but there would definitely be legitimate reasons for preventing it.
e.Cancel = False
Case CloseReason.UserClosing
'The user clicked the Close button on the title bar, pressed Alt+F4, selected Close from the
'system menu or performed some action that caused your code to call the form's Close method.
'Don't let the form close.
e.Cancel = True
Me.Visible = False
Me.NotifyIcon1.Visible = True
Case CloseReason.WindowsShutDown
'Windows is shutting down.
'Definitely let the form close or you'll prevent Windows shutting down normally.
e.Cancel = False
End Select
End Sub
Last edited by jmcilhinney; Feb 24th, 2007 at 10:33 PM.
-
Mar 18th, 2007, 01:27 PM
#3
New Member
Re: Minimise Form to System Tray on Close (.NET 2.0)
Hey a quick question. On the closereason.taskmanagerclose, how could you make it to where you dont get a End Now Window?
When I tried it out it would work, and would not close, but then the End Program Now came up, and you have to clikc cancel for it to continue. I would like to bypass this if possible.
Great code though, I have been looking for something like this for a month or so. Thanks
-
Mar 18th, 2007, 04:22 PM
#4
Re: Minimise Form to System Tray on Close (.NET 2.0)
 Originally Posted by KillerTomato
Hey a quick question. On the closereason.taskmanagerclose, how could you make it to where you dont get a End Now Window?
When I tried it out it would work, and would not close, but then the End Program Now came up, and you have to clikc cancel for it to continue. I would like to bypass this if possible.
Great code though, I have been looking for something like this for a month or so. Thanks
It's not possible because that's the way the task manager works. In .NET terms it sends a CloseMainWindow command to your process first, then checks to see if it was obeyed. If it wasn't it asks the user whether they still want to close the app by force and, if they do, it sends a Kill command to your process, which cannot be ignored. The only way I can see to get around that would be, once you receive the CloseMainWindow command and refuse it, to use API functions to continually look for the End Task dialogue that the Task Manager displays and remotely "click" its Cancel button.
-
Jul 15th, 2007, 06:18 AM
#5
Hyperactive Member
Re: Minimise Form to System Tray on Close (.NET 2.0)
Me.NotifyIcon1.Visible = True
is that won't work in vb2005? it said that notifyIcon is not a member in my program when debug
and...
If e.CloseReason = CloseReason.TaskManagerClosing Then
e.Cancel = True
End If
I find that e cannot being cancel
Last edited by newpat; Jul 15th, 2007 at 06:24 AM.
-
Jul 15th, 2007, 08:34 AM
#6
Re: Minimise Form to System Tray on Close (.NET 2.0)
 Originally Posted by newpat
Me.NotifyIcon1.Visible = True
is that won't work in vb2005? it said that notifyIcon is not a member in my program when debug
and...
If e.CloseReason = CloseReason.TaskManagerClosing Then
e.Cancel = True
End If
I find that e cannot being cancel
NotifyIcon1 is only a member of your form if you've added NotifyIcon1. If you haven't then it isn't. The original point of this thread was to minimise a form to the system tray instead of closing, which is what the NotifyIcon is for. If that's not what you're doing then ignore that part.
The whole point of the FormClosing event is so that you CAN cancel the close if you want to. If you weren't able to set e.Cancel then the event would be completely useless.
-
Jul 15th, 2007, 10:02 AM
#7
Hyperactive Member
Re: Minimise Form to System Tray on Close (.NET 2.0)
 Originally Posted by jmcilhinney
NotifyIcon1 is only a member of your form if you've added NotifyIcon1. If you haven't then it isn't. The original point of this thread was to minimise a form to the system tray instead of closing, which is what the NotifyIcon is for. If that's not what you're doing then ignore that part.
The whole point of the FormClosing event is so that you CAN cancel the close if you want to. If you weren't able to set e.Cancel then the event would be completely useless.
haha, actually, I planned to ask something like your topic idea later...
I find the msdn with NotifyIcon1, it told me also to use NotifyIcon1, but it did not tell me how to dim it
-
Mar 6th, 2008, 07:10 AM
#8
Hyperactive Member
Re: Minimise Form to System Tray on Close (.NET 2.0)
This just using in closing. how about clicking minimise button to?
How to declare the type of NotifyIcon1?
-
Mar 6th, 2008, 09:01 AM
#9
Re: Minimise Form to System Tray on Close (.NET 2.0)
 Originally Posted by newpat
This just using in closing. how about clicking minimise button to?
Handle the SizeChanged event and test the WindowState property to decide whether to show the form or the tray icon.
 Originally Posted by newpat
How to declare the type of NotifyIcon1?
You add a NotifyIcon to your form from the Toolbox.
-
Mar 6th, 2008, 11:15 AM
#10
Hyperactive Member
Re: Minimise Form to System Tray on Close (.NET 2.0)
It is very successful to system tray. But It cannot be re-open from the system tray.
vb Code:
Private Sub Main_FormMinize(ByVal sender As Object, _
ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.MinimumSizeChanged
Its not work
-
Mar 6th, 2008, 12:10 PM
#11
Re: Minimise Form to System Tray on Close (.NET 2.0)
 Originally Posted by newpat
It is very successful to system tray. But It cannot be re-open from the system tray.
vb Code:
Private Sub Main_FormMinize(ByVal sender As Object, _
ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.MinimumSizeChanged
Its not work 
Handle the Click or DoubleClick event of the notify icon to show your form.
Another way to do it is to make a context menu and attach it to your notify icon, then you can have them open various different windows (or just the main window)
-
Oct 8th, 2008, 04:37 AM
#12
Re: Minimise Form to System Tray on Close (.NET 2.0)
It's a nice code John. I didn't knew that such thing is also introduced with .NET 2.0. I remember that when I was developing a xmpp messenger client for my company with .NET 1.1, I trapped the windows messages for this. When user were trying to Shutdown the Windows while running my messenger client in system tray, they were not able to shutdown the system. After some time I realized that When user was clicking on the X button of main window, I was canceling the form closing and hiding the window to system tray. It happened because I trapped the wrong windows message. It was a pain really. Now life is easy. Thanks for sharing this.
-
Oct 22nd, 2009, 09:31 PM
#13
New Member
Re: Minimise Form to System Tray on Close (.NET 2.0)
This is what I was looking for! 
Thanks.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|