如何从MDI父窗体中的MDIClient组件中移除3d边框(凹陷)

本文关键字:3d 边框 凹陷 组件 MDIClient MDI 窗体 | 更新日期: 2023-09-27 18:11:13

我在VS2010中开发WinForms MDI应用程序。. NET 4.0),我只是讨厌MDI父窗体中的3D边界。

那么关于如何删除它(使其平坦或只是没有边框)有什么想法吗?

如何从MDI父窗体中的MDIClient组件中移除3d边框(凹陷)

我知道这是一个旧的帖子,但我花了一些时间和痛苦的工作3D边界的东西(因为我也需要它)从碎片在互联网上,包括:

来自Jacob Slusser在codeproject.com的页面元素(访问日期:2012年8月1日)

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;
        }
    }
}

在form load事件中调用:

form.SetBevel(false);

不要忘记更改命名空间,并记住这是一个扩展方法,但它可以更改为另一个类或MDI父窗体中的方法调用。

如果您不希望导入外部库,也有以下作弊,重新定位/调整大小的mdi容器控件。

    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);
    }

尝试将FormBorderStyle属性更改为FixedSingle