Any way to create a hidden main window in C#?

前端 未结 12 1009
粉色の甜心
粉色の甜心 2020-12-25 13:02

I just want a c# application with a hidden main window that will process and respond to window messages.

I can create a form without showing it, and can then call Ap

12条回答
  •  误落风尘
    2020-12-25 13:21

    I solved the problem like this:

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Main main = new Main();
        Application.Run();
        //Application.Run(new Main());
    }
    

    This code resides in the Program.cs file, and you can see the original Application.Run method call commented out. I just create a Main class object (my main form class is named Main) and start application message loop w/o any parameters. This starts the application, initializes any form components but doesn't show the form.

    Note: you have to have some method to get your window showing (like system tray icon, hotkey or timer or anything you might like).

提交回复
热议问题