• 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.
  • Posts

    • You are like that narcissistic moron and his name is Paul Thurrott.
    • WhatsApp document scanning gets closer to launch as more beta testers get access by Paul Hill Meta looks as though it’s getting closer to releasing WhatsApp’s native document scanner on Android because more people are now reporting they have access in the beta. This will give users a privacy-friendly way to snap photos of documents and automatically convert them to PDF ready for sharing. According to WABetaInfo, the feature comes with two modes: manual and automatic. The manual capture gives users more control over the process allowing them to adjust positioning and lighting to get the best quality scan. Automatic mode on the other hand automatically detects the edge of your document on its own and snaps the image without too much user input, making it faster and easier. Once the picture is taken, WhatsApp automatically converts it to a PDF file so that it can be shared and opened easily by the recipient. The file can be sent within private chats or group conversations. The entire scanning and conversion process takes place locally on your device, using Android’s built-in API for snapping documents. The document itself is also only stored locally until you decide to send it. Just like other WhatsApp messages, these scans are secured with end-to-end encryption when you send them to recipients. The only privacy risk is the user sending the PDF to the wrong person. The new feature was first noticed for Android a few weeks ago and with the latest beta it’s rolling out to more people. Once Meta has tested it across a wide array of devices, we will likely see it land for people on the stable version of WhatsApp. In recent weeks, we have also seen that Meta is testing new chat color themes in the beta releases. That feature doesn’t seem to have filtered down to the stable version yet, so be on the lookout for that too if that’s of interest to you.
  • Recent Achievements

    • Collaborator
      Mighty Pen went up a rank
      Collaborator
    • Week One Done
      emptyother earned a badge
      Week One Done
    • Week One Done
      DarkWun earned a badge
      Week One Done
    • Very Popular
      valkyr09 earned a badge
      Very Popular
    • Week One Done
      suprememobiles earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      568
    2. 2
      +FloatingFatMan
      188
    3. 3
      ATLien_0
      177
    4. 4
      Skyfrog
      111
    5. 5
      Xenon
      110
  • Tell a friend

    Love Neowin? Tell a friend!