Click here to Skip to main content
15,122,845 members
Articles / Desktop Programming / Windows Forms
Technical Blog
Posted 31 Mar 2011


4 bookmarked

Start a WinForm Hidden

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
2 Apr 2011CPOL2 min read
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:<>
    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.

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);


void SystemEvents_SessionEnded(object sender, Microsoft.Win32.SessionEndedEventArgs e)

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.

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

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.

if (bDisplay) Application.Run(new Main());
MyFunctionality func = new MyFunctionality ();

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



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


About the Author

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

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 7:10
MemberMicroImaging6-Apr-11 7:10 
I assume this only works with the Aero interface that only is available on Windows 7.
Is this correct? May be you could actually state these requirements in the tip ?
GeneralRe: I like your article, but it would be nice to say which versions of Windows this work for. Pin
Chris_Green6-Apr-11 7:21
MemberChris_Green6-Apr-11 7:21 

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.