Problems hiding the main form and displaying only a tray icon

D

Danielb

I want my application to run most of the time as just an notify icon
visible in the system tray, with some dialogs windows that open if the user
selects an option from the context menu on the tray icon. I've had a look at
the example on code project that creates an application that runs in the
system tray:

http://www.codeproject.com/csharp/desktop_mail_checker.asp

So I copied how the above article sets up the main form: the form's
FormBorderStyle to none, set it to minizied, disable the titlebar controls
(maximize, minimize, close etc). It was all fine until I noticed that the
form was visible in the Alt-Tab menu and once you had alt-tabbed to it there
was no way of hiding it again :(

After a bit of googling I discover if I set the Form's FormBorderStyle to
FixedToolWindow then it was no longer visible in the Alt-Tab menu, result!
But today I have noticed that now I can no longer minimize or hide the form
it appears as a minizied tool window in the bottom left of my desktop when
the application is running and even worse if the user double clicks on the
title bar the window expands to its normal size, apart from the form is
empty and there's no way to minimize it again!

Anyone got any ideas as to how to completely hide form so its not visible as
a window on the taskbar or on the alt-tab menu? All I want is my
application's icon in the system tray while its running and a context menu
for the icon. I don't really need the main form at all!

Cheers,

DanielB
 
D

Danielb

Thats interesting, but I'm still confused. As all I want my app to do is
display a notify icon and a context menu for it, I already have options
dialogs that I create in response to the user selecting options on the
context menu. I don't want a main form at all but that link seems to imply
that I will need the unused main form?

The annoying thing is that making a form invisible is easy by setting
FormBorderStyle to None and setting the window to minizied. There just does
not seem an easy way to prevent it from being selectible on the Alt-Tab menu
(that I know of) without setting FormBorderStyle to FixedToolWindow which
would then seem to prevent it from being hidden when its 'minized'.
 
I

Ian Griffiths [C# MVP]

Why have a main form at all?

I've never really understood why so many people go to such lengths to have a
hidden main form and try to keep it invisible, rather than going for the
much simpler expedient of not using a main form at all.

Windows Forms doesn't require you to have a main form. If you just start
the app up like this:

Application.Run();

i.e. you don't pass in a form, then there is no main form. This means you
don't need to worry about it appearing in the Alt-Tab list - something
that's not there isn't going to appear!

So all you need to do is make sure you add the notify icon to the
notification area before you call Application.Run.

You'll also need to work out how you're going to shut down. If you don't
have a main form, the application will run until you call Application.Exit.
 
C

C# Learner

Ian Griffiths said:
Windows Forms doesn't require you to have a main form.

But doesn't a system tray icon require a window associated with it? It's
been a long time but I think I remember this being the case with Win32
windows, and if so, it's most likely the same situation for WinForms.
 
I

Ian Griffiths [C# MVP]

Nope.

It works just fine without a main form. Just tried it now.

Windows Forms creates hidden windows. So it it is necessary to have a
window in order to add a notify icon, maybe it's using one of these hidden
windows. You certainly don't need to create a Form object in order to have
your notify icon show up and respond to menu clicks.
 
C

Coder

The idea of not using a Form but rather managing the NotifyIcon
yourself is smart. BUT, if you dont use a Form, you cant use the
Designer to add resources (icons, wave files, etc.) to the project. How
do you get around this?

I want to use your non-form approach, but then how do I add an icon
resource into the project? I cant edit the resx file myself because you
need to serialise the icon to base64 and I dont know how to do that.
 
C

Coder

Oh and simply setting the resource to "embedded resource" isnt good
enough. That doesnt actually add the resource to the project so I can
get at it via the ResourceManager using some name. It doesnt embed it
at all if the main class is not a Form? What is wrong here?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top