gpt4 book ai didi

c# - 无边框winform窗体阴影

转载 作者:行者123 更新时间:2023-12-02 02:56:53 29 4
gpt4 key购买 nike

我有一个无边框窗口窗体,我使用下面的代码在它后面创建了一个阴影。
但是,当我单击父窗体时,阴影消失了。

任何人都可以帮我解决如何在单击花药表单/父表单时保持阴影吗?
阴影在不同的窗口(例如 Chrome)上可见,但在其父窗体上不可见

(我尝试谷歌但找不到任何东西)

更新
我确实注意到,如果我最小化窗口并再次最大化它,阴影就会回来

我的代码

    private const int CS_DROPSHADOW = 0x00020000;
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ClassStyle |= CS_DROPSHADOW;
return cp;
}
}

提前致谢

最佳答案

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

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

关于c# - 无边框winform窗体阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60913399/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com