3

I have an application that is designed to minimize to the system tray. No issues there.

The problem I am having is that I cannot determine what Windows is doing to force the minimized state when I set up a desktop shortcut to that executable and launch it, such as:

enter image description here

I put some debug outputs in the form's constructor and launched via the shortcut. I get no command line arguments and a check of WindowState yields Normal. Yet the app starts minimized to the taskbar.

However, that's the rub: I want it to start minimized to the system tray, just as it would if the form were on-screen and the user minimized it. Not all the time, just when a "minimize" shortcut is used, or when the user clicks Minimize on the form, of course.

EDIT: for the curious...my initial testing was flawed because I checked in the constructor. Placing the test in the Load method produced a Minimized state, to which I could then react and call my code to perform the "minimize-to-tray".

DonBoitnott
  • 10,787
  • 6
  • 49
  • 68
  • 1
    I don't get the downvotes either. – siride Dec 02 '14 at 20:03
  • have you looked at any working examples that have been posted on the internet as well as `SO` also in your code are you setting the WindowState.. for example `1this.WindowState = FormWindowState.Minimized;` [App Minimize to Tray](http://stackoverflow.com/questions/1730731/how-to-start-winform-app-minimized-to-tray) I think that you need this one here I just re-read your question http://stackoverflow.com/questions/16140627/minimize-to-tray – MethodMan Dec 02 '14 at 20:03
  • @DJKRAZE Please read the entirety of the question. – DonBoitnott Dec 02 '14 at 20:03
  • http://stackoverflow.com/questions/1730731/how-to-start-winform-app-minimized-to-tray – siride Dec 02 '14 at 20:06
  • I figured it out. Just checking too soon. Apparently the `WindowState` isn't forced to `Minimized` until at least the `Load` event. Checking too soon. – DonBoitnott Dec 02 '14 at 20:06

1 Answers1

3

Windows is starting the process with parameters to minimize the main window.

In C#, you can do the same by setting WindowStyle (MSDN) at ProcessStartInfo for use in Process.Start().

In the native world, you would use the CreateProcess (MSDN) API and pass a STARTUPINFO, setting wShowWindow to SW_MINIMIZE.

To query the window state, use GetWindowInfo (MSDN), look at dwStyle and check if WS_MINIMIZEis set. In C#, this should be in Form.WindowState.

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222