【问题标题】:Prevent Win32 from drawing classic title bar防止Win32绘制经典标题栏
【发布时间】:2017-10-16 18:02:28
【问题描述】:

我想为我的无边界表单添加一个漂亮的阴影,而我发现以最小的性能损失做到这一点的最佳方法是使用DwmExtendFrameIntoClientArea。然而,这似乎导致 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,我什至尝试在窗口接收WM_NCPAINT而不调用DefWndProc时返回0,但没有任何区别。

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

【问题讨论】:

标签: c# windows winforms aero dwm


【解决方案1】:

非常感谢@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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多