9

我制作了一个表格,并在其中扩展了玻璃,如下图所示。但是当我移动窗口以使其并非全部在屏幕上可见时,将其移回后玻璃渲染是错误的: 在此处输入图像描述

我该如何处理才能正确呈现窗口?

这是我的代码:

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

2 回答 2

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();
}
于 2012-10-16T19:01:10.077 回答
4

以下调用必须在您的 OnPaint 方法中

FillRectangle( new SolidBrush( Color.Black ), new Rectangle( 0, this.ClientSize.Height - 32, this.ClientSize.Width, 32 ) );

其余的只需完成一次。而不是调用 CreateGraphics() 使用 OnPaint (e.Graphics) 的参数

于 2012-10-16T19:03:22.263 回答