2012-07-17 82 views
3

我正在使用自定義邊框創建表單。 我想用onPaint覆蓋繪製我自己的邊框。 不幸的是,當我使用下面的代碼時,我得到了圖像的痕跡。OnPaint在調整大小時會留下痕跡

有誰知道如何解決這個問題? 是的,我已將DoubleBuffered設置爲true。

protected override void OnPaint(PaintEventArgs e) 
{ 
    Graphics g = e.Graphics; 

    // Background 
    g.FillRectangle(new SolidBrush(ColorTranslator.FromHtml("#EFEFF7")), new Rectangle(0, 0, this.Width - 1, this.Height - 1)); 

    // Outer Borders 
    g.DrawImage(Resources.Resource.OuterBorderTopLeft, new Rectangle(0, 0, 13, 13)); 
    g.DrawImage(Resources.Resource.OuterBorderTopLeft1, new Rectangle(13, 0, 221, 13)); 
    g.DrawImage(Resources.Resource.OuterBorderTopLeft2, new Rectangle(0, 13, 13, 221)); 
    g.DrawImage(Resources.Resource.OuterBorderTopRight, new Rectangle(this.Size.Width - 13, 0, 13, 13)); 
    g.DrawImage(Resources.Resource.OuterBorderTopRight1, new Rectangle(this.Size.Width - 13, 13, 13, 221)); 
    g.DrawImage(Resources.Resource.OuterBorderTopRight2, new Rectangle(this.Size.Width - 234, 0, 221, 13)); 
    g.DrawImage(Resources.Resource.OuterBorderBottomLeft, new Rectangle(0, this.Size.Height - 13, 13, 13)); 
    g.DrawImage(Resources.Resource.OuterBorderBottomLeft1, new Rectangle(0, this.Size.Height - 234, 13, 221)); 
    g.DrawImage(Resources.Resource.OuterBorderBottomLeft2, new Rectangle(13, this.Size.Height - 13, 221, 13)); 
    g.DrawImage(Resources.Resource.OuterBorderBottomRight, new Rectangle(this.Size.Width - 13, this.Size.Height - 13, 13, 13)); 
    g.DrawImage(Resources.Resource.OuterBorderBottomRight1, new Rectangle(this.Size.Width - 234, this.Size.Height - 13, 221, 13)); 
    g.DrawImage(Resources.Resource.OuterBorderBottomRight2, new Rectangle(this.Size.Width - 13, this.Size.Height - 234, 13, 221)); 
    base.OnPaint(e); 
} 

回答

9

嘗試重寫調整大小的方法,太:

protected override void OnResize(EventArgs e) { 
    base.OnResize(e); 
    this.Invalidate(); 
} 

或在構造函數中,你可以添加其他設置:

public Form1() { 
    InitializeComponent(); 
    this.DoubleBuffered = true; 
    this.ResizeRedraw = true; 
} 
+3

+1第二, 「更正確」 的答案;) – Tergiver 2012-07-17 23:38:25

+0

謝謝你的幫助提示! – 2012-07-18 05:11:56

+1

雖然可選,但我建議設置this.DoubleBuffered = true。否則,閃爍將是激烈的。 – RajV 2013-01-27 05:17:28

相關問題