C# Completely hide form border ONLY when maximized

284 views Asked by At

I have a form that is a child of an MDI form. When this form isn't maximized, it fits inside of the MDI form, below several menu panels and controls. It has a border, icon, and controlbox. When the form is maximized, the border shows directly underneath the main MDI form border. When it is maximized, the controlbox is disabled and we don't need the border for any reason- it just looks sloppy. The form is resized programmatically so there is never a need for the border/controls when it is maximized.

Is there a way to set the FormBorderStyle = None, ONLY when the form is maximized, and have FormBorderStyle = sizeable when it is any size other than Max?

See screenshots below, The red line is on the border that I want hidden- the area marked toolstrip controls is above the form, not actually on it. The white space marked picture box is the actual form that has the border. When not maximized, it the border will show directly above the picture box area and below the toolstrip area, and can be resized by the user. When maximized, the user cannot resize it, so I want to hide that border as it looks sloppy below the main form border

When Not maximized: [1]: https://i.stack.imgur.com/TFDjk.png

When Maximized: https://i.stack.imgur.com/EK4cY.png

2

There are 2 answers

1
SmartDeveloper On

It is a child form hence it cannot maximize the parent form. Can you please share more details to further assist you? Maybe share a screenshot?

0
McNets On

I've wrote this code according to others posts I've found in stackoverflow and googling.

I've tested it, and it works.

[DllImport("user32.dll")]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);

[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

[DllImport("user32.dll", ExactSpelling = true)]
private static extern int SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

const int GWL_EXSTYLE = -20;
const int WS_EX_CLIENTEDGE = 0x200;
const uint SWP_NOSIZE = 0x0001;
const uint SWP_NOMOVE = 0x0002;
const uint SWP_NOZORDER = 0x0004;
const uint SWP_NOACTIVATE = 0x0010;
const uint SWP_FRAMECHANGED = 0x0020;
const uint SWP_NOOWNERZORDER = 0x0200;

private void MdiEdgeBorderOnOff(bool off)
{
    foreach(Control ctl in this.Controls)
    {
        if (ctl.GetType() != typeof(MdiClient)) continue;

        int wnd = GetWindowLong(ctl.Handle, GWL_EXSTYLE);
        if (off)
            wnd &= ~WS_EX_CLIENTEDGE;
        else
            wnd |= WS_EX_CLIENTEDGE;

        SetWindowLong(ctl.Handle, GWL_EXSTYLE, wnd);

        SetWindowPos(ctl.Handle, IntPtr.Zero, 0, 0, 0, 0,
            SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
            SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
    }
}