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

前端 未结 12 937
粉色の甜心
粉色の甜心 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:30
    Form1 f1=new Form1();
    f1.WindowState = FormWindowState.Minimized;
    f1.ShowInTaskbar = false;
    
    0 讨论(0)
  • 2020-12-25 13:31

    The best way is to use the following 1-2 lines in the constuctor:

    this.WindowState = FormWindowState.Minimized;
    this.ShowInTaskbar = false; // This is optional
    

    You can even set the Minimized property in the VS Property window.

    0 讨论(0)
  • 2020-12-25 13:35

    I know this is old question, but it ranks well in google, so I will provide my solution anyway.

    I do two things:

    private void Form_Load(object sender, EventArgs e)
    {
        Opacity = 0;
    }
    
    private void Form_Shown(object sender, EventArgs e)
    {
        Visible = false;
        Opacity = 100;
    }
    
    0 讨论(0)
  • 2020-12-25 13:38

    You can create a class that inherits from System.Windows.Forms.NativeWindow (which provides basic message loop capability) and reference the Handle property in its constructor to create its handle and hook it into the message loop. Once you call Application.Run, you will be able to process messages from it.

    0 讨论(0)
  • 2020-12-25 13:40

    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);
        }
    
    0 讨论(0)
  • 2020-12-25 13:46

    Why can't you just pass the form when you call Application.Run? Given that it's clearly a blocking call, on what event do you want to show the form? Just calling form.Show() should be enough.

    0 讨论(0)
提交回复
热议问题