问题描述
我想在我的无边界表单中添加漂亮的阴影,而我发现以最小的性能损失来做到这一点的最佳方法是使用
这是我正在使用的代码:
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(){
leftWidth = 0,
topHeight = 0,
rightWidth = 0,
bottomHeight = 1
};
NativeApi.DwmExtendFrameIntoClientArea(Handle,ref margins);
我尝试将 ALLOW_NCPAINT
设置为1,我什至尝试在窗口收到 WM_NCPAINT
而不返回 DefWndProc
时返回0,但这没什么区别。
有没有办法解决这个奇怪的问题,同时仍然使用 DwmExtendFrameIntoClientArea
?
非常感谢@Erik Philips,我终于通过遵循的建议。问题出在 CreateParams
:
///< summary>
///获取定义初始窗口样式的参数。
///< / summary>
受保护的覆盖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
中删除:
受保护的覆盖CreateParams CreateParams {
get {
CreateParams cp = base.CreateParams;
if(!DesignMode){
cp.ClassStyle | =(int)ClassStyle.DoubleClicks;
cp.Style =未选中((int)(WindowStyle.Popup | WindowStyle.SystemMenu | WindowStyle.ClipChildren | WindowStyle.ClipSiblings));
cp.ExStyle | =(int)ExtendedWindowStyle.Layered;
}
return cp;
}
}
此问题已解决,因为WinForms显然添加了 WS_BORDER
默认为类样式,即使以后将 FormBorderStyle
设置为 None
。
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
?
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
.
这篇关于防止Win32绘制经典标题栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!