0

I have a C# Windows Form application lauching an instance of a form upon execution using the default code:

[STAThread]
static void Main () {
  Application.EnableVisualStyles();
  Application.SetCompatibleTextRenderingDefault(false);
  Application.Run(new Form1());
}

If I use the default value "Normal" for WindowState property on Form1, I'm able to create a shortcut for the application and set the Run property to Normal, Minimized or Maximized and it's gonna be respected.

However, if the application is NOT run through a shortcut, I want it to be maximized, by default. But if I set WindowState property to Maximized, the shortcut's Run property is no longer respected at all.

Is there a way to handle both cases?

Thanks.

STremblay
  • 125
  • 1
  • 7
  • possible duplicate of http://stackoverflow.com/questions/27257808/how-does-windows-force-minimized-state-from-a-desktop-shortcut – Hamid Pourjam Jan 06 '15 at 16:41
  • @dotctor Thanks but after review, it's not a duplicate of that question. I also need to have a "default" windowstate to Maximized instead of Normal when the application is NOT started through a shurtcut, like dbl-clicking the executable. – STremblay Jan 06 '15 at 16:58

1 Answers1

0

I found a partial way of detecting the shortcut setting. By partial I mean this method does not seem to work for the Normal Window option in the shortcut if you set the WindowState to something else then Normal in the OnLoad event. It works however for Minimized & Maximized shortcut options. Maybe someone else will be able to explain this.

Form-class:

bool windowStateSetByShortcut = false;

protected override void WndProc(ref Message m)
{
    /*WM_SIZE*/
    if (m.Msg == 0x0005)
    {
        // This will be set to true if the shortcut uses the Maximized or Minimized
        // options because then it runs before OnLoad.
        windowStateSetByShortcut = true;     
    }
    base.WndProc(ref m);
}

protected override void OnLoad(EventArgs e)
{    
    if (!windowStateSetByShortcut)
    {
        WindowState = FormWindowState.Normal;
    }
    base.OnLoad(e);
}
t3chb0t
  • 16,340
  • 13
  • 78
  • 118