Click here to Skip to main content
15,867,308 members
Articles / Desktop Programming / Windows Forms

Start a WinForm Hidden

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
2 Apr 2011CPOL2 min read 18.5K   5   2
How to start a WinForm Hidden

This is a short one, there may be better ways but this was fast and simple. Works for me!

The case here is I have a WinForm application and want the Main window to start hidden. From the designer, you can’t set Visible = false. Also, setting this.Visible=false in the constructor or Load event has no effect. Sure you can set it later (like in Paint) but the last thing you want is your window to flash and disappear every time your application starts.

So, here is all you have to do:

  1. In the designer, set your window Opacity to 0%.
  2. In the constructor, pass in a boolean to indicate if the window is displayed.
  3. In the function body, add this line:<>
    JavaScript
    Opacity = bVisible ? 100 : 0;

Simple, fast, done… moving on to other things in life.

Well okay… I’m not really one for leaving well enough alone. The fact is, there is a better way but you’re not going to like it! I imagine your main form is doing something like sitting in the notification tray and/or monitoring something like a windows service. That functionality, whatever it is your application does, should be abstracted out of the form altogether. You’re really not going to like what’s next. This object, the meat of your program, can then exist in a worker thread and provide the relevant events to your UI so it may update itself if the main form is shown.

It’s not as bad as it sounds. Go on, you know separating the logic from the UI layer is the right design! We’ll abstract all our real functionality into a class called MyFunctionality. We have BackgroundWorker in our .NET toolbox MyFunctionality will inherit from. Then override OnDoWork where we’ll put the code we originally had in the main form’s constructor or Load event. We will handle the case of exiting the thread in case the user logs off. Last, we just need optional events for the UI to receive an update from our functionality object. An example of this is below, with our DisplayChanged event and DoDisplayChanged function. Use a custom event to pass more information.

JavaScript
class MyFunctionality : System.ComponentModel.BackgroundWorker
{
public event EventHandler DisplayChanged = null;

protected override void OnDoWork(DoWorkEventArgs e)
{
//The meat of my program…

Microsoft.Win32.SystemEvents.SessionEnded += 
   new Microsoft.Win32.SessionEndedEventHandler(SystemEvents_SessionEnded);

base.OnDoWork(e);
}

void SystemEvents_SessionEnded(object sender, Microsoft.Win32.SessionEndedEventArgs e)
{
this.CancelAsync();
}

void DoDisplayChanged()
{
if (DisplayChanged != null)
DisplayChanged.Invoke(this, EventArgs.Empty);
}
}

Your form will create the thread and add the relevant events, in this case DisplayChanged.

JavaScript
private void Main_Load(object sender, EventArgs e)
{
MyFunctionality func = new MyFunctionality();
func.DisplayChanged += new EventHandler(Func_DisplayChanged);
func.RunWorkerAsync();
}

Lastly, we just need to make a small change in Program.cs to either (A) instantiate our main form, which in turn launches the thread hosting our functionality, or (B) directly instantiate the thread without ever using our main form.

JavaScript
if (bDisplay) Application.Run(new Main());
else
{
MyFunctionality func = new MyFunctionality ();
func.RunWorkerAsync();
Application.Run();
}

See, I told you that you weren’t going to like it!

Reference: http://www.interact-sw.co.uk/iangblog/2004/11/30/nomainform

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Avaya Inc.
Ireland Ireland
Formerly a C++ client developer, nowadays I'm all about C# and ASP.NET. Over the years I have mastered some and played with many aspects of .NET.

Follow my blog as I catalogue the more arcane problems I encounter and their solutions at CodingLifestyle.com

Comments and Discussions

 
GeneralI like your article, but it would be nice to say which versions of Windows this work for. Pin
MicroImaging6-Apr-11 6:10
MicroImaging6-Apr-11 6:10 
GeneralRe: I like your article, but it would be nice to say which versions of Windows this work for. Pin
Chris_Green6-Apr-11 6:21
Chris_Green6-Apr-11 6:21 
Hi, these articles are imported from my blog and as such might not conform to the standard articles you see on CodeProject.

This has nothing to do with Aero or Windows 7. If you are doing WinForm programming on any Windows OS this should work for you.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.