(1 item) |
|
(1 item) |
|
(5 items) |
|
(1 item) |
|
(1 item) |
|
(2 items) |
|
(2 items) |
|
(4 items) |
|
(1 item) |
|
(6 items) |
|
(2 items) |
|
(4 items) |
|
(1 item) |
|
(4 items) |
|
(2 items) |
|
(1 item) |
|
(1 item) |
|
(1 item) |
|
(1 item) |
|
(1 item) |
|
(1 item) |
|
(1 item) |
|
(1 item) |
|
(2 items) |
|
(2 items) |
|
(5 items) |
|
(3 items) |
|
(1 item) |
|
(1 item) |
|
(1 item) |
|
(3 items) |
|
(1 item) |
|
(1 item) |
|
(2 items) |
|
(8 items) |
|
(2 items) |
|
(7 items) |
|
(2 items) |
|
(2 items) |
|
(1 item) |
|
(2 items) |
|
(1 item) |
|
(2 items) |
|
(4 items) |
|
(1 item) |
|
(5 items) |
|
(1 item) |
|
(3 items) |
|
(2 items) |
|
(2 items) |
|
(8 items) |
|
(7 items) |
|
(3 items) |
|
(7 items) |
|
(6 items) |
|
(1 item) |
|
(2 items) |
|
(5 items) |
|
(5 items) |
|
(7 items) |
|
(3 items) |
|
(7 items) |
|
(16 items) |
|
(10 items) |
|
(27 items) |
|
(15 items) |
|
(15 items) |
|
(13 items) |
|
(16 items) |
|
(15 items) |
A Windows Forms question that seems to crop up a lot on newsgroups and mailing lists is:
How do I hide my main form?
While it's a frequently asked question, on closer inspection it usually turns out to be the wrong question.
The context for the question is usually that someone wants the application to run as an icon in the notification area. (Also sometimes called, incorrectly, the system tray.) They don't actually want a window to appear when they run the application, or be able to continue running when all windows are closed.
So they try to make it invisible, or mess with its ShowInTaskBar
property, and go through all sorts of
hoops to make sure that the main form doesn't appear anywhere.
These people would have had a much easier time of it if only they had known this simple but useful fact:
You don't have to have a main form.
You get to choose whether or not to have a main form when you start the event loop in Windows Forms. The VS.NET wizards tend to give you something like this:
Application.Run(new Form1());
If you pass in a form to Application.Run
like this, that form will be designated as the 'main form', and the
application will run until that form closes. So how do you run without the main form? It's really rather simple:
Application.Run();
This means your application will run until you call Application.Exit()
, so you'll need to make sure you do that
at some point. The usual way to do that is that you'll create a NotifyIcon
, and give it a menu item with an
'Exit' item, and quit the application from there. Obviously you need to set up that NotifyIcon
before you call
Application.Run
.
The only moderately tricky aspect of this is handling shutdowns and logouts. If you have a main form, your application will
end up exiting when the user logs out or shuts down. But if your application doesn't have a main form, you need to detect when
this happens and exit. It's not hard to do this, you just need to remember to do it - if you don't you'll slow users down when they
log out - Windows will wait a few seconds for your application to exit, and eventually ask if the user wants to nuke it, claiming
that it seems to have crashed. You probably don't want your users to think your application crashes whenever Windows shuts
down... So you just need to handle the SessionEnded
event on the Microsoft.Win32.SystemEvents
class.