-1

I have a startup screen in my application that I want to show with StartupPosition = CenterScreen and WindowState=Normal. All works fine, but if, for instance, the user creates a shortcut to my application and sets Run=Maximized, then the startup screen becomes maximized instead of normal and I want to disable this behavior. Is there any way to completely disable form from being maximized or override startup state given to application?

I've tried forcing StartupPosition and WindowState, but this doesn't work as the window just ends up on the top-left of the screen.

Doing some tests I found 2 ways to disable the window from being maximized due to Run=Maximized on startup: A) show a dummy form, close it, then load actual startup form B) add the following the the Form.Load:

if (WindowState != FormWindowState.Normal)
{
    WindowState = FormWindowState.Normal;
    Rectangle r = Screen.GetWorkingArea(this);
    Location = new Point(r.X + ((r.Width - Width) / 2), r.Y + ((r.Height - Height) / 2));
}

I know there should be an actual method of disabling form from starting maximized because if I show a message box before my startup form the message box shows up normally and then so does my startup form, but without showing the message box my startup screen wants to be maximized.

Adam
  • 562
  • 2
  • 15
  • 1
    Please show the code that you have written to _force_ the state and the position – Steve Feb 06 '19 at 17:20
  • In Form.Load, `add if (this.WindowState == FormWindowState.Maximized) { this.WindowState = FormWindowState.Normal; }`. Leave the `StartPosition` property set to `CenterScreen` in the designer. – Jimi Feb 06 '19 at 17:25
  • @Jimi Would you really need the if-block? :-) – LarsTech Feb 06 '19 at 17:26
  • @LarsTech Nope, but maybe the OP wants to test what happens in the event, so an `else { }` could be added to test a different behaviour. Now, do I need an `If` **and** an `else` block... :) – Jimi Feb 06 '19 at 17:29
  • In From.Load I added WindowState = FormWindowState.Normal; StartPosition = FormStartPosition.CenterScreen;. This forces the window to remain normal, but it does not center on screen if /StartMaximized argument was passed to application. I will note I already have MaximizeBox=false. – Adam Feb 06 '19 at 17:44
  • Why do you care about this at all? If the user wants your startup form to be opened as maximized, why do you want to prevent it? – Zohar Peled Feb 06 '19 at 19:14
  • The form was not designed to be maximized (and the user cannot restore the window since MaximizeBox=false and FormBorderStyle=FixedSingle). – Adam Feb 06 '19 at 19:30
  • *Leave the `StartPosition` property set to `CenterScreen` in the designer*. If your Form is not shown centered on screen, then you have left something somewhere that redefines its position. Keep just `this.WindowState = FormWindowState.Normal;` in Form.Load. – Jimi Feb 06 '19 at 20:03
  • @Jimi The windows short-cut system that has a Run = Maximized setting does seem to muck with the form. You can prevent the maximized part, and then fix the center position problem, but the form can visibly be seen moving from the corner. – LarsTech Feb 06 '19 at 20:09
  • With just `WindowState = FormWindowState.Normal`, the window appears on the top-left rather than center screen. I don't have anywhere in my code redefining the position of the form. This is only is set in the designer. – Adam Feb 06 '19 at 20:10
  • @LarsTech Exactly. If maximized, the user sees the window flash across the screen and if minimized they can see the restore animation. – Adam Feb 06 '19 at 20:13
  • Hans seemed to have solved this: [Application stuck in full screen?](https://stackoverflow.com/a/17003593/719186). Try his answer. – LarsTech Feb 06 '19 at 20:15
  • @LarsTech Ah, yes, I forgot about the latest Windows 10 updates. This doesn't happen in previous versions (in Windows 7 is not even a *thing*). Setting NOSIZE+NOMOVE as in Hans' answer can prevent this from happening at all (if you really need to). – Jimi Feb 06 '19 at 21:01

1 Answers1

1

Thank you @LarsTech! The answer was somewhere between Application stuck in full screen? and @Jimi's solution. The benefit is this disables Run = Maximized without disabling Run = Minimized.

using System.Runtime.InteropServices;
public partial class frmStartup : Form
{
    public frmStartup()
    {
        InitializeComponent();
    }

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == 0x46) // WM_WINDOWPOSCHANGING
        {
            var wpos = (WINDOWPOS)Marshal.PtrToStructure(m.LParam, typeof(WINDOWPOS));
            wpos.flags |= 0x0001; // Turn on SWP_NOSIZE
            Marshal.StructureToPtr(wpos, m.LParam, false);
        }
        base.WndProc(ref m);
    }
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    private struct WINDOWPOS
    {
        public IntPtr hwnd, hwndInsertAfter;
        public int x, y, cx, cy;
        public int flags;
    }

    private void frmStartup_Load(object sender, EventArgs e)
    {
        if (WindowState == FormWindowState.Maximized)
        {
            WindowState = FormWindowState.Normal;
        }
    }
}
Adam
  • 562
  • 2
  • 15