我制作了一张表格并将玻璃杯延伸到下面,如下图所示。但是当我移动窗口所以并非所有窗口都在屏幕上可见时,我将其移回后玻璃渲染是错误的:
如何处理这个窗口才能正确呈现?
这是我的代码:
[DllImport( "dwmapi.dll" )]
private static extern void DwmExtendFrameIntoClientArea( IntPtr hWnd, ref Margins mg );
[DllImport( "dwmapi.dll" )]
private static extern void DwmIsCompositionEnabled( out bool enabled );
public struct Margins{
public int Left;
public int Right;
public int Top;
public int Bottom;
}
private void Form1_Shown( object sender, EventArgs e ) {
this.CreateGraphics().FillRectangle( new SolidBrush( Color.Black ), new Rectangle( 0, this.ClientSize.Height - 32, this.ClientSize.Width, 32 ) );
bool isGlassEnabled = false;
Margins margin;
margin.Top = 0;
margin.Left = 0;
margin.Bottom = 32;
margin.Right = 0;
DwmIsCompositionEnabled( out isGlassEnabled );
if (isGlassEnabled) {
DwmExtendFrameIntoClientArea( this.Handle, ref margin );
}
}
答案 0 :(得分:11)
我认为CreateGraphics让你感到悲伤。
尝试重写OnPaint方法并使用PaintEventArgs中的Graphic对象:
protected override void OnShown(EventArgs e) {
base.OnShown(e);
bool isGlassEnabled = false;
Margins margin;
margin.Top = 0;
margin.Left = 0;
margin.Bottom = 32;
margin.Right = 0;
DwmIsCompositionEnabled(out isGlassEnabled);
if (isGlassEnabled) {
DwmExtendFrameIntoClientArea(this.Handle, ref margin);
}
}
protected override void OnPaint(PaintEventArgs e) {
base.OnPaint(e);
e.Graphics.FillRectangle(Pens.Black,
new Rectangle(0, this.ClientSize.Height - 32, this.ClientSize.Width, 32));
}
如果要调整表单大小,请将其添加到构造函数中:
public Form1() {
InitializeComponent();
this.ResizeRedraw = true;
}
或覆盖Resize事件:
protected override void OnResize(EventArgs e) {
base.OnResize(e);
this.Invalidate();
}
答案 1 :(得分:4)
以下调用必须在您的OnPaint方法
中FillRectangle( new SolidBrush( Color.Black ), new Rectangle( 0, this.ClientSize.Height - 32, this.ClientSize.Width, 32 ) );
剩下的只需做一次。而不是调用CreateGraphics()使用OnPaint的参数(e.Graphics)