开发者

hiding my window from the start?

开发者 https://www.devze.com 2023-03-25 21:44 出处:网络
I have a form (Form1, the main form) that I wish to hide at the very start. I want to do this becaus开发者_StackOverflowe I\'ve got a notifyIcon that, by clicking, sets visible true or false for the

I have a form (Form1, the main form) that I wish to hide at the very start.

I want to do this becaus开发者_StackOverflowe I've got a notifyIcon that, by clicking, sets visible true or false for the form, but I'm not able to set the visibility for the form at the very start.

Thanks


You don't have to display a Form on startup. You can simply use the Application.Run() overload that does not take a Form instance. That will start the Windows application message pump without displaying any UI.

Prior to doing Application.Run(), you can setup your NotifyIcon to be displayed. In effect, this means that the NotifyIcon will be visible after startup, and when the user interacts with the icon, you can open up the Form.

Here is a minimal working C# program that does that: Display a NotifyIcon at startup, and displaying a Form when the user clicks it:

using System;
using System.Drawing;
using System.Windows.Forms;

class Program
{
    static void Main()
    {
        NotifyIcon icon = new NotifyIcon();
        icon.Icon = new Icon("C:\\Windows\\System32\\PerfCenterCpl.ico");
        icon.Visible = true;
        icon.Click += (s,e) => new Form().Show();
        icon.DoubleClick += (s,e) => Application.Exit();
        Application.Run();
    }
}


I've got an application that does exactly that. To solve it, do this in your Form's OnLoad and it won't appear straight away or in the task bar:

protected override void OnLoad(EventArgs e)
{
    Visible = false;
    ShowInTaskbar = false;
    base.OnLoad(e);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消