12

I am developing WinForms MDI app in VS2010 (.NET 4.0) and I just hate 3D border in MDI parent form.

So any ideas on how to remove it (make it flat or just no border it all) ?

EmirZ
  • 646
  • 4
  • 10
  • 21
  • 1
    No, you're pretty stuck with it. The MdiClient class has no BorderStyle property and there's no obvious way that I see to hook into the creation of its instance. – Hans Passant Oct 13 '11 at 12:46

3 Answers3

29

I know this is an old post but I have spent some time and pain working the 3D border stuff out (because I needed it too) from fragments across the internet including:

Elements from Jacob Slusser's page at codeproject.com (Accessed 1st Aug'12)

So here goes:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace MDITest
{
    public static class MDIClientSupport
    {
        [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);

        private const int GWL_EXSTYLE = -20;
        private const int WS_EX_CLIENTEDGE = 0x200;
        private const uint SWP_NOSIZE = 0x0001;
        private const uint SWP_NOMOVE = 0x0002;
        private const uint SWP_NOZORDER = 0x0004;
        private const uint SWP_NOREDRAW = 0x0008;
        private const uint SWP_NOACTIVATE = 0x0010;
        private const uint SWP_FRAMECHANGED = 0x0020;
        private const uint SWP_SHOWWINDOW = 0x0040;
        private const uint SWP_HIDEWINDOW = 0x0080;
        private const uint SWP_NOCOPYBITS = 0x0100;
        private const uint SWP_NOOWNERZORDER = 0x0200;
        private const uint SWP_NOSENDCHANGING = 0x0400;

        public static bool SetBevel(this Form form, bool show)
        {
            foreach (Control c in form.Controls)
            {
                MdiClient client = c as MdiClient;
                if (client != null)
                {
                    int windowLong = GetWindowLong(c.Handle, GWL_EXSTYLE);

                    if (show)
                    {
                        windowLong |= WS_EX_CLIENTEDGE;
                    }
                    else
                    {
                        windowLong &= ~WS_EX_CLIENTEDGE;
                    }

                    SetWindowLong(c.Handle, GWL_EXSTYLE, windowLong);

                    // Update the non-client area.
                    SetWindowPos(client.Handle, IntPtr.Zero, 0, 0, 0, 0,
                        SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
                        SWP_NOOWNERZORDER | SWP_FRAMECHANGED);

                    return true;
                }
            }
            return false;
        }

    }
}

In the form load event call:

form.SetBevel(false);

Don't forget to change the namespace and remember this is an extension method but it could be changed to be just a method call in another class or in you MDI parent form.

James
  • 2,812
  • 3
  • 22
  • 33
  • It does not work with .NET Framework 4 (and probably with higher versions) on Windows 10 anymore. Maybe it's about the Aero theme, i don't know. – Roni Tovi Mar 01 '20 at 11:50
  • I have just tested it and it is working just fine on .NET 4.8. However, I have some updates pending on my system so I will check it again after that. – James Mar 02 '20 at 18:20
  • @Roni Tovi just to confirm what I said earlier the code is working on .NET 4.0 and 4.8 on Windows 10. – James Mar 02 '20 at 18:38
  • No - it doesn't work fully. It works only for left-right and bottom borders. I still see the top border. – Roni Tovi Mar 18 '20 at 06:39
  • Worked for me on: Windows 10 V1903; .net 4.6.1. – stigzler Mar 19 '20 at 12:19
  • Worked on Windows 10 1909, .NET Framework 4.6.1. Thank you. – Artfaith Oct 25 '20 at 00:11
8

If you would prefer not to import external libraries there is also following cheat which repositions/resizes the mdi container control.

    protected override void OnLoad(EventArgs e)
    {
        var mdiclient = this.Controls.OfType<MdiClient>().Single();
        this.SuspendLayout();
        mdiclient.SuspendLayout();
        var hdiff = mdiclient.Size.Width - mdiclient.ClientSize.Width;
        var vdiff = mdiclient.Size.Height - mdiclient.ClientSize.Height;
        var size = new Size(mdiclient.Width + hdiff, mdiclient.Height + vdiff);
        var location = new Point(mdiclient.Left - (hdiff / 2), mdiclient.Top - (vdiff / 2));
        mdiclient.Dock = DockStyle.None;
        mdiclient.Size = size;
        mdiclient.Location = location;
        mdiclient.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom;
        mdiclient.ResumeLayout(true);
        this.ResumeLayout(true);
        base.OnLoad(e);
    }
Paul
  • 81
  • 1
  • 1
-1

Try changing the FormBorderStyle property to FixedSingle

Prashant
  • 966
  • 9
  • 26
  • 1
    It is the first thing I tried... main form loses "sizability" and 3D border is still there! – EmirZ Oct 13 '11 at 10:40