Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

winforms

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 Application.Run() without passing in a form, but how can I hook the created form into the message loop?

Is there another way to go about this?


1 Answers

In the process of re-writing a VC++ TaskTray App, in C# .NET, I found the following method truly workable to achieve the following.

  1. No initial form dislayed at startup
  2. Running Message Loop that can be used with Invoke/BeginInvoke as needed as IsWindowHandle is true

The steps I followed:

  1. Used an ApplicationContext in Application.Run() Instead of a form. See http://www.codeproject.com/Articles/18683/Creating-a-Tasktray-Application for the example I used.
  2. Set the Form's ShowInTaskbar property to true within the GUI Designer. (This seems counter productive but it works)
  3. Override the OnLoad() method in your Form Class setting Visible and ShowInTaskbar to false as shown below.
protected override void OnLoad(EventArgs e)
    {
        Visible = false; 
        ShowInTaskbar = false; 
        base.OnLoad(e);
    }
like image 72
Saxar Avatar answered Sep 14 '25 03:09

Saxar