Prevent Win32 from drawing classic title bar

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-07 03:13:16

问题


I wanted to add a nice shadow to my borderless form, and the best way I found to do it with minimal performance loss is to use DwmExtendFrameIntoClientArea. However, this seems to be causing Windows to draw a classic title bar over the window, but it is non-functional (ie. the glitch is merely graphical).

This is the code I am using:

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);

I have tried setting ALLOW_NCPAINT to 1, and I even tried returning 0 when the window receives WM_NCPAINT without calling DefWndProc, but it made no difference.

Is there a way to resolve this weird issue while still using DwmExtendFrameIntoClientArea?


回答1:


Big thanks to @Erik Philips, I have finally resolved the issue by following this answer's advice. The issue was in 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 | had to be removed from 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;
    }
}

This solved the issue, as apparently WinForms adds WS_BORDER by default to the class style, even if FormBorderStyle is later set to None.



来源:https://stackoverflow.com/questions/46776425/prevent-win32-from-drawing-classic-title-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!