阻止Win32绘制经典标题栏

2024-01-27

我想为我的无边界窗体添加一个漂亮的阴影,我发现以最小的性能损失实现这一点的最佳方法是使用DwmExtendFrameIntoClientArea https://msdn.microsoft.com/en-us/library/windows/desktop/aa969512%28v=vs.85%29.aspx。然而,这似乎导致 Windows 在窗口上绘制一个经典的标题栏,但它不起作用(即,故障仅仅是图形上的)。

这是我正在使用的代码:

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,我什至尝试在窗口接收到时返回 0WM_NCPAINT不打电话DefWndProc,但这没有什么区别。

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


非常感谢@Erik Philips,我终于通过以下方式解决了这个问题这个答案 https://stackoverflow.com/a/7443081/5007383的建议。问题是在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;
    }
}

The |必须从中删除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.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

阻止Win32绘制经典标题栏 的相关文章

随机推荐