Search code examples
c#user-interfacebackground-application

C# - Background application with GUI


My problem is that I want to create a background application but with a user interface that could be restored and minimized to the system tray and it starts with windows. I tried searching how to start but I only found threads about Windows Service without UI or creating form and hiding it. So my question is how should I start ? A Windows Form ? A Service and add an interface somehow ?

Thank you!


Solution

  • Here's a way to create an app that isn't tied to a Form. Use a Standard WinForms Project, but pass your custom implementation of ApplicationContext to Application.Run():

    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MyContext());
        }
    
    }
    
    public class MyContext : ApplicationContext
    {
    
        public MyContext()
        {
            // this is the new "entry point" for your application
    
            // use Application.Exit() to shut down the application
    
            // you can still create and display a NotifyIcon control via code for display in the tray
            // (the icon for the NotifyIcon can be an embedded resource)
            // you can also display forms as usual when necessary
        }
    
    }