C# 无边界形阴影

C# 无边界形阴影,c#,winforms,C#,Winforms,因此,我有以下代码: #region Dropshadow [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")] private static extern IntPtr CreateRoundRectRgn ( int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, int nWidthEllipse, int n

因此,我有以下代码:

#region Dropshadow
[DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
private static extern IntPtr CreateRoundRectRgn
(
    int nLeftRect,
    int nTopRect,
    int nRightRect,
    int nBottomRect,
    int nWidthEllipse,
    int nHeightEllipse
);
[DllImport("dwmapi.dll")]
public static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMarInset);
[DllImport("dwmapi.dll")]
public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);
[DllImport("dwmapi.dll")]
public static extern int DwmIsCompositionEnabled(ref int pfEnabled);
private bool m_aeroEnabled;
public struct MARGINS
{
    public int leftWidth;
    public int rightWidth;
    public int topHeight;
    public int bottomHeight;
}
protected override CreateParams CreateParams {
    get {
        m_aeroEnabled = CheckAeroEnabled();
        CreateParams cp = base.CreateParams;
        if (!m_aeroEnabled) {
            cp.ClassStyle |= 0x00020000;
        }

        return cp;
    }
}
private bool CheckAeroEnabled()
{
    if (Environment.OSVersion.Version.Major >= 6) {
        int enabled = 0;
        DwmIsCompositionEnabled(ref enabled);
        return (enabled == 1) ? true : false;
    }
    return false;
}
protected override void WndProc(ref Message m)
{
    switch (m.Msg) {
        case 0x0085:
            if (m_aeroEnabled) {
                int v = 2;
                DwmSetWindowAttribute(Handle, 2, ref v, 4);
                MARGINS margins = new MARGINS() {
                    bottomHeight = 1,
                    leftWidth = 0,
                    rightWidth = 0,
                    topHeight = 0
                };
                DwmExtendFrameIntoClientArea(Handle, ref margins);
            }
            break;
        default:
            break;
    }
    base.WndProc(ref m);
}
#endregion
这将使用GDI生成Dropshadow。 然而,唯一的问题是,我必须让它在顶部保持一个1像素高的边框,它可以是任何边缘,只是在我的应用程序上最难注意到顶部

这使得我的应用程序在顶部的一行基本上降低了观看体验

有没有可能在没有边界的情况下做到这一点

bottomHeight=1代码就是它的全部内容。如果我将其设置为0,将topHeight设置为1,则该行将位于底部。将它们全部设置为0,则完全不显示dropshadow

事实证明,这与我的填充有关,我需要在至少1条边上留出1个像素线空白,Dropshadow才能工作。我选择使用填充来生成1像素的线条,并将顶部填充设置为1。这将在顶部设置线。底部高度=1根本不重要。它就在那里,因为它要求其中至少有一个非0

如果我删除了填充和顶行等,并且在CreateParams overide中,如果我删除了启用aero的复选框,它将显示类似以下内容的dropshadow:

这是一个表单类,它使用DWM渲染其边框/阴影

如上所述,您需要注册一个属性以及相关的策略设置,将其值设置为Enabled。 然后使用设置属性和所需效果,依此类推

所有需要的声明都在这里

这是一个名为Borderless的表单类,它激发了创造力。 我试图让它看起来像你已经发布的一样,以尽量减少影响

该表单是FormBorderStyle=None的标准WinForms表单

这些是所有需要的声明,以及其他可能有用的声明。 请注意,我只使用内部属性表单Win32 API,它们是使用helpr方法调用的

它是一个分部类,因为Winapi类是一个扩展类库。你可以把它改成你习惯的任何东西

我建议保留[SuppressUnmanagedCodeSecurity属性] Win32 API声明的属性

我将bottomHeight设置为3,发现边框高度包含在表单大小中,表单大小没有改变。
所以我给这个表单设置了一个背景图像,图像中隐藏了边框。

我真的不知道。但是你试过负边际-1吗?这应该会触发玻璃片效果。@nigelwhatting在这里产生一条透明线,你可以看到聚焦和非聚焦的不透明度变化/顺便说一句,我必须把填充1放在顶部,这样它才能工作。如果我的CEFSharp浏览器UI覆盖了整个表单,则它无法工作。您需要注册窗口设置,使用表单中启用的DWMNCRENDERINGPOLICY值调用DwmSetWindowAttribute。Load事件,您还必须第一次调用DwmExtendFrameIntoClientArea,指定页边距。然后,你应该处理,而不是WM_绘画。您只需将边距设置为0,1,0,1=>Right,将Bottom设置为1即可启用阴影。或全部设置为-1以启用特殊的玻璃板功能。DWMNCRENDERINGPOLICY为已设置的int 2。我不认为它需要设置为Form.Load。它被称为WndProc。DwmExtendFrameIntoClientArea我已经尝试将all设置为-1。它不工作,因为我的控件覆盖了整个表单,它需要一个边为空,因为它出于某种原因做了一个dropshadow。
public partial class Borderless : Form
{
    public Borderless() => InitializeComponent();

    protected override void OnHandleCreated(EventArgs e) {
        base.OnHandleCreated(e);
        WinApi.Dwm.DWMNCRENDERINGPOLICY Policy = WinApi.Dwm.DWMNCRENDERINGPOLICY.Enabled;
        WinApi.Dwm.WindowSetAttribute(this.Handle, WinApi.Dwm.DWMWINDOWATTRIBUTE.NCRenderingPolicy, (int)Policy);
        if (DWNCompositionEnabled()) { WinApi.Dwm.WindowBorderlessDropShadow(this.Handle, 2); }
        //if (DWNCompositionEnabled()) { WinApi.Dwm.WindowEnableBlurBehind(this.Handle); }
        //if (DWNCompositionEnabled()) { WinApi.Dwm.WindowSheetOfGlass(this.Handle); }
    }

    private bool DWNCompositionEnabled() => (Environment.OSVersion.Version.Major >= 6)
                                         ? WinApi.Dwm.IsCompositionEnabled()
                                         : false;

    protected override void WndProc(ref Message m)
    {
        switch (m.Msg)
        {
            case (int)WinApi.WinMessage.WM_DWMCOMPOSITIONCHANGED:
                {
                    WinApi.Dwm.DWMNCRENDERINGPOLICY Policy = WinApi.Dwm.DWMNCRENDERINGPOLICY.Enabled;
                    WinApi.Dwm.WindowSetAttribute(this.Handle, WinApi.Dwm.DWMWINDOWATTRIBUTE.NCRenderingPolicy, (int)Policy);
                    WinApi.Dwm.WindowBorderlessDropShadow(this.Handle, 2);
                    m.Result = (IntPtr)0;
                }
                break;
            default:
                break;
        }
        base.WndProc(ref m);
    }
}
public partial class WinApi
{
    public enum WinMessage : int
    {
        WM_DWMCOMPOSITIONCHANGED = 0x031E,          //The system will send a window the WM_DWMCOMPOSITIONCHANGED message to indicate that the availability of desktop composition has changed.
        WM_DWMNCRENDERINGCHANGED = 0x031F,          //WM_DWMNCRENDERINGCHANGED is called when the non-client area rendering status of a window has changed. Only windows that have set the flag DWM_BLURBEHIND.fTransitionOnMaximized to true will get this message.
        WM_DWMCOLORIZATIONCOLORCHANGED = 0x0320,    //Sent to all top-level windows when the colorization color has changed.
        WM_DWMWINDOWMAXIMIZEDCHANGE = 0x0321        //WM_DWMWINDOWMAXIMIZEDCHANGE will let you know when a DWM composed window is maximized. You also have to register for this message as well. You'd have other windowd go opaque when this message is sent.
    }

    public class Dwm

        public enum DWMWINDOWATTRIBUTE : uint
        {
            NCRenderingEnabled = 1,     //Get only atttribute
            NCRenderingPolicy,          //Enable or disable non-client rendering
            TransitionsForceDisabled,
            AllowNCPaint,
            CaptionButtonBounds,
            NonClientRtlLayout,
            ForceIconicRepresentation,
            Flip3DPolicy,
            ExtendedFrameBounds,
            HasIconicBitmap,
            DisallowPeek,
            ExcludedFromPeek,
            Cloak,
            Cloaked,
            FreezeRepresentation
        }

        public enum DWMNCRENDERINGPOLICY : uint
        {
            UseWindowStyle, // Enable/disable non-client rendering based on window style
            Disabled,       // Disabled non-client rendering; window style is ignored
            Enabled,        // Enabled non-client rendering; window style is ignored
        };

        // Values designating how Flip3D treats a given window.
        enum DWMFLIP3DWINDOWPOLICY : uint
        {
            Default,        // Hide or include the window in Flip3D based on window style and visibility.
            ExcludeBelow,   // Display the window under Flip3D and disabled.
            ExcludeAbove,   // Display the window above Flip3D and enabled.
        };

        public struct MARGINS
        {
            public int leftWidth;
            public int rightWidth;
            public int topHeight;
            public int bottomHeight;

            public MARGINS(int LeftWidth, int RightWidth, int TopHeight, int BottomHeight)
            {
                leftWidth = LeftWidth;
                rightWidth = RightWidth;
                topHeight = TopHeight;
                bottomHeight = BottomHeight;
            }

            public void NoMargins()
            {
                leftWidth = 0;
                rightWidth = 0;
                topHeight = 0;
                bottomHeight = 0;
            }

            public void SheetOfGlass()
            {
                leftWidth = -1;
                rightWidth = -1;
                topHeight = -1;
                bottomHeight = -1;
            }
        }


        [SuppressUnmanagedCodeSecurityAttribute]
        internal static class SafeNativeMethods
        {
            //https://msdn.microsoft.com/en-us/library/windows/desktop/aa969508(v=vs.85).aspx
            [DllImport("dwmapi.dll")]
            internal static extern int DwmEnableBlurBehindWindow(IntPtr hwnd, ref DWM_BLURBEHIND blurBehind);

            //https://msdn.microsoft.com/it-it/library/windows/desktop/aa969512(v=vs.85).aspx
            [DllImport("dwmapi.dll")]
            internal static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMarInset);

            //https://msdn.microsoft.com/en-us/library/windows/desktop/aa969515(v=vs.85).aspx
            [DllImport("dwmapi.dll")]
            internal static extern int DwmGetWindowAttribute(IntPtr hwnd, DWMWINDOWATTRIBUTE attr, ref int attrValue, int attrSize);

            //https://msdn.microsoft.com/en-us/library/windows/desktop/aa969524(v=vs.85).aspx
            [DllImport("dwmapi.dll")]
            internal static extern int DwmSetWindowAttribute(IntPtr hwnd, DWMWINDOWATTRIBUTE attr, ref int attrValue, int attrSize);

            [DllImport("dwmapi.dll")]
            internal static extern int DwmIsCompositionEnabled(ref int pfEnabled);
        }

        public static bool IsCompositionEnabled()
        {
            int pfEnabled = 0;
            int result = SafeNativeMethods.DwmIsCompositionEnabled(ref pfEnabled);
            return (pfEnabled == 1) ? true : false;
        }

        public static bool IsNonClientRenderingEnabled(IntPtr hWnd)
        {
            int gwaEnabled = 0;
            int result = SafeNativeMethods.DwmGetWindowAttribute(hWnd, DWMWINDOWATTRIBUTE.NCRenderingEnabled, ref gwaEnabled, sizeof(int));
            return (gwaEnabled == 1) ? true : false;
        }

        public static bool WindowSetAttribute(IntPtr hWnd, DWMWINDOWATTRIBUTE Attribute, int AttributeValue)
        {
            int result = SafeNativeMethods.DwmSetWindowAttribute(hWnd, Attribute, ref AttributeValue, sizeof(int));
            return (result == 0);
        }


        public static bool WindowEnableBlurBehind(IntPtr hWnd)
        {
            //Create and populate the Blur Behind structure
            DWM_BLURBEHIND Dwm_BB = new DWM_BLURBEHIND(true);

            int result = SafeNativeMethods.DwmEnableBlurBehindWindow(hWnd, ref Dwm_BB);
            return (result == 0);
        }

        public static bool WindowExtendIntoClientArea(IntPtr hWnd, WinApi.Dwm.MARGINS Margins)
        {
            // Extend frame on the bottom of client area
            int result = SafeNativeMethods.DwmExtendFrameIntoClientArea(hWnd, ref Margins);
            return (result == 0);
        }

        public static bool WindowBorderlessDropShadow(IntPtr hWnd, int ShadowSize)
        {
            MARGINS Margins = new MARGINS(0, ShadowSize, 0, ShadowSize);
            int result = SafeNativeMethods.DwmExtendFrameIntoClientArea(hWnd, ref Margins);
            return (result == 0);
        }

        public static bool WindowSheetOfGlass(IntPtr hWnd)
        {
            MARGINS Margins = new MARGINS();
            Margins.SheetOfGlass();

            //Margins set to All:-1 - Sheet Of Glass effect
            int result = SafeNativeMethods.DwmExtendFrameIntoClientArea(hWnd, ref Margins);
            return (result == 0);
        }

        public static bool WindowDisableRendering(IntPtr hWnd)
        {
            DWMNCRENDERINGPOLICY NCRP = DWMNCRENDERINGPOLICY.Disabled;
            int ncrp = (int)NCRP;
            // Disable non-client area rendering on the window.
            int result = SafeNativeMethods.DwmSetWindowAttribute(hWnd, DWMWINDOWATTRIBUTE.NCRenderingPolicy, ref ncrp, sizeof(int));
            return (result == 0);
        }
    }
}