【问题标题】:How to fully hide the top bar in Windows Form using C#无法在 Windows 窗体 C# 中完全隐藏顶部栏
【发布时间】:2016-09-19 00:05:13
【问题描述】:

我正在使用 C#。我知道这个问题很常见,只是我仍然无法完全隐藏顶部栏。这是我将表单文本字符串设置为 "" 和 controlbox = false 时得到的结果。

还是想要阴影效果:

所以你可以看到侧面的边框消失了(太棒了!)并且有通常的阴影(太棒了!)但是顶部边框有一条奇怪的白线,我似乎无法删除。

我不想将表单边框属性设置为“无”,因为我喜欢集成的可缩放控件和表单阴影,所以这不是一个选项。对此还有其他建议吗?

提前致谢!

(我应该指定右上角的按钮由我生成并显示我的可编辑表单的边缘。上面的空白是我要删除的。)

【问题讨论】:

标签: c# winforms controlbox


【解决方案1】:

我知道这篇文章很旧,但我偶然发现了这个问题并最终解决了它,所以我将它发布在这里以供任何可以帮助的人使用。

所以,我用自己的控制栏和按钮制作了一个自定义的可调整大小的无边框表单类,类似于 OP。想法是让该表单以及所有相关的WndProc 代码充当项目中所有其他对话框表单的基类。

当我运行我的项目时,我的表单顶部出现了完全相同的白线,即使我已经正确设置了所有适当的表单属性。

问题最终在于,通过 Form 继承,所有派生的 Forms 也有自己的私有 InitializeComponent 方法。我不知道 VS IDE 在派生的 Form 类中将 FormBorderStyle 属性设置为 Sizable,因为我只关注自定义基类。

如果您为Form 使用自定义基类,则在派生 类中正确设置FormBorderStyle 可以解决问题。

【讨论】:

  • 你能帮我做个样品吗?我无法理解你的想法。
【解决方案2】:

如果你点击Form,进入属性,有FormBorderSyle部分,把它放在False

【讨论】:

    【解决方案3】:

    如果您只是不将 FormBorderStyle 设置为 None 来生成阴影,我已经回答了如何在这里轻松制作阴影: Drop shadow on Borderless Winform-No flicker or disappearance

    这是我的答案:

    请尝试以下步骤并恢复任何错误:

    将以下代码添加到名为 DropShadow.cs 的新代码文件中;

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace Core
    {
        public class DropShadow
        {
            #region Shadowing
    
            #region Fields
    
            private bool _isAeroEnabled = false;
            private bool _isDraggingEnabled = false;
            private const int WM_NCHITTEST = 0x84;
            private const int WS_MINIMIZEBOX = 0x20000;
            private const int HTCLIENT = 0x1;
            private const int HTCAPTION = 0x2;
            private const int CS_DBLCLKS = 0x8;
            private const int CS_DROPSHADOW = 0x00020000;
            private const int WM_NCPAINT = 0x0085;
            private const int WM_ACTIVATEAPP = 0x001C;
    
            #endregion
    
            #region Structures
    
            [EditorBrowsable(EditorBrowsableState.Never)]
            public struct MARGINS
            {
                public int leftWidth;
                public int rightWidth;
                public int topHeight;
                public int bottomHeight;
            }
    
            #endregion
    
            #region Methods
    
            #region Public
    
            [DllImport("dwmapi.dll")]
            [EditorBrowsable(EditorBrowsableState.Never)]
            public static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMarInset);
    
            [DllImport("dwmapi.dll")]
            [EditorBrowsable(EditorBrowsableState.Never)]
            public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);
    
            [DllImport("dwmapi.dll")]
            [EditorBrowsable(EditorBrowsableState.Never)]
            public static extern int DwmIsCompositionEnabled(ref int pfEnabled);
    
            [EditorBrowsable(EditorBrowsableState.Never)]
            public static bool IsCompositionEnabled()
            {
                if (Environment.OSVersion.Version.Major < 6) return false;
    
                bool enabled;
                DwmIsCompositionEnabled(out enabled);
    
                return enabled;
            }
    
            #endregion
    
            #region Private
    
            [DllImport("dwmapi.dll")]
            private static extern int DwmIsCompositionEnabled(out bool enabled);
    
            [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
            private static extern IntPtr CreateRoundRectRgn
            (
                int nLeftRect,
                int nTopRect,
                int nRightRect,
                int nBottomRect,
                int nWidthEllipse,
                int nHeightEllipse
             );
    
            private bool CheckIfAeroIsEnabled()
            {
                if (Environment.OSVersion.Version.Major >= 6)
                {
                    int enabled = 0;
                    DwmIsCompositionEnabled(ref enabled);
    
                    return (enabled == 1) ? true : false;
                }
                return false;
            }
    
            #endregion
    
            #region Overrides
    
            public void ApplyShadows(Form form)
            {
                var v = 2;
    
                DwmSetWindowAttribute(form.Handle, 2, ref v, 4);
    
                MARGINS margins = new MARGINS()
                {
                    bottomHeight = 1,
                    leftWidth = 0,
                    rightWidth = 0,
                    topHeight = 0
                };
    
                DwmExtendFrameIntoClientArea(form.Handle, ref margins);
            }
    
            #endregion
    
            #endregion
    
            #endregion
        }
    }
    

    在您的表单中,在 InitializeComponent(); 下方添加这一行

    (new Core.DropShadow()).ApplyShadows(this);
    

    【讨论】:

      猜你喜欢
      • 2011-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多