2012-10-09 106 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)的參數