• 0

[C#] Start application minimized


Question

Hello,

I've made a small applicaiton and added a system trya icon. Now I'm trying to make my applicaiton start minimized, if I specify a command line argument. I've got this code at the moment:

static void Main(string[] args)
{
	Application.EnableVisualStyles();
	Application.SetCompatibleTextRenderingDefault(false);
	Form1 form1 = new Form1();
	if (args.Length == 1 && (args[0] == "/minimized" || args[0] == "-minimized"))
		form1.Visible = false;
	Application.Run(form1);
}

However, it doesn't work. But if I change "form1.Visible = false;" in that code to something different like "form1.WindowState = FormWindowState.Minimized;", then it starts the applicaiton minimized, but not minimized to the system tray.

Why doesn't this work?

Any help is appreciated greatly. Thanks.

Link to comment
https://www.neowin.net/forum/topic/428833-c-start-application-minimized/
Share on other sites

3 answers to this question

Recommended Posts

  • 0
  BigCheese said:

Hello,

I've made a small applicaiton and added a system trya icon. Now I'm trying to make my applicaiton start minimized, if I specify a command line argument. I've got this code at the moment:

static void Main(string[] args)
{
	Application.EnableVisualStyles();
	Application.SetCompatibleTextRenderingDefault(false);
	Form1 form1 = new Form1();
	if (args.Length == 1 && (args[0] == "/minimized" || args[0] == "-minimized"))
		form1.Visible = false;
	Application.Run(form1);
}

However, it doesn't work. But if I change "form1.Visible = false;" in that code to something different like "form1.WindowState = FormWindowState.Minimized;", then it starts the applicaiton minimized, but not minimized to the system tray.

Why doesn't this work?

Any help is appreciated greatly. Thanks.

Visible won't work because the form's handle hasn't yet been created. It is only created when Show() is called, which is called when the Run method invokes it. So, what you need to do is set the WindowState to Minimized and handle the VisibleChanged event. Test if the window state is minimize in your handler, then you set the visibility of the form to false.

  • 0

If you have a Tray Icon and you don't want to see the app in the taskbar, use form1.Hide() and then form1.Show() to show it again whenever you need to. I have found it difficult sometimes to get it to start in the tray. I'm sure there is a better way, but in the past I have set a timer to 1 millisecond after onload, and then called this.Hide(). It's a while since I did this though, so I may have found a better way at some point, but I don't remember it atm.

  • 0
  weenur said:

Visible won't work because the form's handle hasn't yet been created. It is only created when Show() is called, which is called when the Run method invokes it. So, what you need to do is set the WindowState to Minimized and handle the VisibleChanged event. Test if the window state is minimize in your handler, then you set the visibility of the form to false.

Thanks, that works fine.

I can see the app in the taskbar for a split-second before it goes to the system tray, but I suppose it doesn't really matter.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.