c# - 防止Win32绘制经典标题栏

标签 c# windows winforms aero dwm

我想为我的无边界表单添加一个漂亮的阴影,我发现以最小的性能损失来做到这一点的最佳方法是使用 DwmExtendFrameIntoClientArea .然而,这似乎导致 Windows 在窗口上绘制一个经典的标题栏,但它不起作用(即故障仅仅是图形上的)。

Windows Title Bar

这是我使用的代码:

int v = (int) DWMNCRENDERINGPOLICY.DWMNCRP_ENABLED;
NativeApi.DwmSetWindowAttribute(Handle, DwmWindowAttribute.NCRENDERING_POLICY, ref v, sizeof(int));
int enable = 0;
NativeApi.DwmSetWindowAttribute(Handle, DwmWindowAttribute.ALLOW_NCPAINT, ref enable, sizeof(int));
MARGINS margins = new MARGINS() {
    leftWidth = 0,
    topHeight = 0,
    rightWidth = 0,
    bottomHeight = 1
};
NativeApi.DwmExtendFrameIntoClientArea(Handle, ref margins);

我曾尝试将 ALLOW_NCPAINT 设置为 1,我什至尝试在窗口接收到 WM_NCPAINT 时返回 0 而未调用 DefWndProc,但它使没有区别。

有没有办法在仍然使用 DwmExtendFrameIntoClientArea 的同时解决这个奇怪的问题?

最佳答案

非常感谢@Erik Philips,我终于通过关注 this answer 解决了这个问题。的建议。问题出在 CreateParams 中:

/// <summary>
/// Gets the parameters that define the initial window style.
/// </summary>
protected override CreateParams CreateParams {
    get {
        CreateParams cp = base.CreateParams;
        if (!DesignMode) {
            cp.ClassStyle |= (int) ClassStyle.DoubleClicks;
            cp.Style |= unchecked((int) (WindowStyle.Popup | WindowStyle.SystemMenu | WindowStyle.ClipChildren | WindowStyle.ClipSiblings));
            cp.ExStyle |= (int) ExtendedWindowStyle.Layered;
        }
        return cp;
    }
}

| 必须从 cp.Style 中删除:

protected override CreateParams CreateParams {
    get {
        CreateParams cp = base.CreateParams;
        if (!DesignMode) {
            cp.ClassStyle |= (int) ClassStyle.DoubleClicks;
            cp.Style = unchecked((int) (WindowStyle.Popup | WindowStyle.SystemMenu | WindowStyle.ClipChildren | WindowStyle.ClipSiblings));
            cp.ExStyle |= (int) ExtendedWindowStyle.Layered;
        }
        return cp;
    }
}

这解决了这个问题,显然 WinForms 默认将 WS_BORDER 添加到类样式,即使 FormBorderStyle 后来设置为 None

关于c# - 防止Win32绘制经典标题栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46776425/

相关文章:

c# - 注册多个 DbContext 时,如何解析 ConfigureServices 中正确的 DbContext 类型

windows - schtasks 命令在从 CMD 运行时有效,但在 VBScript 中无效

windows - 相同的代码,在 windows/ubuntu (Keras/Tensorflow) 上的准确性非常不同

javascript - 从文件系统在 webbrowser 控件中加载 jQuery

c# - Entity Framework Core 词汇比较

c# - LINQ to Entities in linq join with tuples 仅支持无参数构造函数和初始值设定项

c# - 如何让 File.OpenText 返回不可为空的 StreamReader

windows - "rm -rf"等同于 Windows PowerShell?

c# - 一次将datagridview的所有数据插入数据库

javascript - WebBrowser 控件不适用于某些 javascript 结构