2012-10-09 56 views
9

我做了一个窗体,并在其中扩展了玻璃,如下图所示。但是,当我移动窗口,使其不能在屏幕上全部显示时,玻璃渲染在我将它移回后错误: enter image description here玻璃不是正确的

如何处理此问题,以便窗口呈现正确?

这是我的代码:

[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

如何知道如果它不可见? – Gabe

+1

不确定你的意思......你是说如果你移动窗口越过屏幕边缘再返回,那么玻璃已经在窗口与屏幕边缘相交的部分消失了? – series0ne

+1

P.S.我过去曾多次使用过玻璃。在Google中搜索Daniel Moth和Glass ...他似乎是一位具有玻璃效果的古茹! – series0ne

回答

11

我认为的createGraphics在这里给你造成一些悲痛。

尝试重写OnPaint方法和使用来自PaintEventArgs的图形对象,而不是:

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(); 
} 
+0

我也是这样做的,但我仍然遇到了一些调整大小的问题。尝试一下,看看? – Alan

4

以下调用必须在您的OnPaint me中thod

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

其余的只需要做一次。你不用调用CreateGraphics(),而是使用OnPaint(e.Graphics)的参数