C# Can';t在Windows窗体C中完全隐藏顶部栏#

C# Can';t在Windows窗体C中完全隐藏顶部栏#,c#,winforms,controlbox,C#,Winforms,Controlbox,我在C#工作。我知道这个问题很常见,只是我仍然无法完全隐藏顶部的栏。这是我将表单文本字符串设置为“”并且controlbox=false时得到的结果 仍然需要阴影效果: 正如你所看到的,边上的边框消失了(很好!),还有通常的阴影(很好!),但是上面的边框有一条奇怪的白线,我似乎无法删除 我不想将form border属性设置为“none”,因为我喜欢集成的可缩放控件和form shadow,所以这不是一个选项。还有其他建议吗 提前谢谢 (我应该指定右上角的按钮由我生成,并显示我可编辑表单的边缘

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

仍然需要阴影效果:

正如你所看到的,边上的边框消失了(很好!),还有通常的阴影(很好!),但是上面的边框有一条奇怪的白线,我似乎无法删除

我不想将form border属性设置为“none”,因为我喜欢集成的可缩放控件和form shadow,所以这不是一个选项。还有其他建议吗

提前谢谢


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

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

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

当我运行我的项目时,我在表单顶部得到了完全相同的白线,尽管我已经正确设置了所有适当的表单属性

问题在于,对于表单继承,所有派生表单都有自己的私有
InitializeComponent
方法。我不知道VS IDE在派生表单类中将FormBorderStyle属性设置为
sizeable
,因为我只关注自定义基类


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

如果您单击表单,转到属性,这里有一节FormBorderSyle并将其放入False

如果您没有将阴影的FormBorderStyle设置为“无”,我已经在这里回答了如何轻松创建阴影:

以下是我的答案:

请尝试以下步骤,并返回任何错误:

将以下代码添加到名为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
    }
}

你有没有试过:@RezaAghaei是的,我在发布这篇文章之前读过。form.FormBorderStyle=FormBorderStyle.SizableToolWindow;我知道我把什么作为我的边界,但它看起来还是一样。嗨。这次运气好吗?我也面临着同样的问题,尝试了很多解决方案却没有成功…你能帮我拿个样品吗?我听不懂你的意思。
(new Core.DropShadow()).ApplyShadows(this);