Results 1 to 9 of 9

Thread: Formless Tray Application

  1. #1

    Thread Starter
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,957

    Formless Tray Application

    C# version here.

    Recently, I replied to a thread on another forum where the poster asked how to create an application that displayed an icon in the notification area (aka system tray) but didn't require a form to be created and hidden. My first thought was that a form was required but, upon reflection, I thought that there must be another way. I recalled having read something about the ApplicationContext class so I investigated that further and that is indeed the way to do it.

    Generally, whether you create your own Main method or you use the VB application framework (which hides the main method from you, but it's there) you call Application.Run and pass a form as an argument, which becomes the application's main form. You can also pass an object whose type inherits ApplicationContext as an argument to Application.Run. The application will then close when you call Application.Exit or Application.ExitThread.

    The example attached below meets the requirements of the poster of the aforementioned thread. They wanted a NotifyIcon and the ability to use an OpenFileDialog. For that, I created a component that provided a designer, which made it easy to configure the NotifyIcon and the ContextMenuStrip. My derived ApplicationContext then creates an instance of that component and duly disposes it when the ApplicationContext itself is disposed.

    Simply run the app and you will see the NotifyIcon. Right-click the icon to get a menu that provides items to display an OpenFileDialog and to exit the app.

    Finally, note that the attached application was written in VB 2010, so you will need VS 2010 or VB Express 2010 to open it.

    EDIT:

    I have added a version of the demo that uses the RegisterHotKey API.
    Attached Files Attached Files

  2. #2
    New Member Faizy999's Avatar
    Join Date
    May 2006
    Posts
    11

    Re: Formless Tray Application

    I was also looking for such features thanks.

  3. #3
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,348

    Re: Formless Tray Application

    can this be used in the same way as send to system tray apon startup and create a new instance of Form1 when needed?can this be used in the same way as send to system tray apon startup and create a new instance of Form1 when needed?

    Before i was using a single instance application, set to windowstate minimized. Event shown calling me.visible = false. Then when a new instance is called bring forth form1

  4. #4

    Thread Starter
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,957

    Re: Formless Tray Application

    Quote Originally Posted by ident View Post
    can this be used in the same way as send to system tray apon startup and create a new instance of Form1 when needed?can this be used in the same way as send to system tray apon startup and create a new instance of Form1 when needed?

    Before i was using a single instance application, set to windowstate minimized. Event shown calling me.visible = false. Then when a new instance is called bring forth form1
    The purpose of the code provided is to create an application that displays an icon in the system tray without a conventional main form. It's still a Windows Forms application though, so you can create a form and call Show or ShowDialog just as you can in any other WinForms app.

  5. #5
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,348

    Re: Formless Tray Application

    Which would you suggest the best way would be to detect if an instance was already open? since the frame work is disabled.

  6. #6

    Thread Starter
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,957

    Re: Formless Tray Application

    Quote Originally Posted by ident View Post
    Which would you suggest the best way would be to detect if an instance was already open? since the frame work is disabled.
    If you aren't passing commandline arguments then you can use the Process class to detect an existing instance. If you are passing commandline arguments then I'd stick with using the Application Framework and a hidden main form, as it will be much easier.

  7. #7
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,348

    Re: Formless Tray Application

    This is what worrys me. When to find the balance between a "hack" and the correct way. I call a "hack" by using opacity as nothing, me.visible = false and setting the form minimized at startup to make the form hidden.

    How ever i need to handle the unhandled exception event, singleinstance event and a few other frame work events.

    But then i ask my self if i do not want to display a form at startup then dont call a forms show event let alone call a form to be hidden


    would mean a lot more coding to disable frame work.

    gets me confused

  8. #8

    Thread Starter
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,957

    Re: Formless Tray Application

    Quote Originally Posted by ident View Post
    This is what worrys me. When to find the balance between a "hack" and the correct way. I call a "hack" by using opacity as nothing, me.visible = false and setting the form minimized at startup to make the form hidden.

    How ever i need to handle the unhandled exception event, singleinstance event and a few other frame work events.

    But then i ask my self if i do not want to display a form at startup then dont call a forms show event let alone call a form to be hidden


    would mean a lot more coding to disable frame work.

    gets me confused
    You don't really need the Application Framework for global exception handling. You can simply handle the AppDomain.UnhandledException and Application.ThreadException events individually. Other functionality from the Application Framework does take more effort to replicate, i.e. single-instance apps (particularly with commandline arguments), splash screens and quitting when the last form closes. If you need any of those, I'd say stick with the Application Framework. You don't need to mess with Opacity though. Set the form's WindowState to Minimized and ShowInTaskbar to False, then call Hide or set Visible to False in the Shown event handler.

  9. #9

    Thread Starter
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,957

    Re: Formless Tray Application

    I have added an updated version of the demo app that includes hotkey functionality based on the answer here. Ctrl+Shift+Alt+O is equivalent to clicking the Open menu item and Ctrl+Shift+Alt+X is for Exit.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width