我做了一个表格,然后像下面的图片一样将玻璃杯拉长了。但是,当我移动窗口以至于在屏幕上看不到所有窗口时,将玻璃移回后玻璃渲染是错误的:

我该如何处理才能正确显示窗口?

这是我的代码:

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

最佳答案

我认为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();
}

关于c# - 玻璃未正确渲染,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12803841/

10-12 02:54