2010-05-13 41 views
0

我正嘗試在C#.Net中爲Windows窗體應用程序構建自己的自定義控件。目前我使用繪畫事件繪製一些矩形和其他圖形元素。 當我現在調整應用程序窗體大小以適合桌面大小時,所有元素都會重新繪製(這正是我需要的行爲),但舊的顯示在背景中。c# - 調整大小時清除表面

這裏是我現在在做什麼:

Pen penDefaultBorder = new Pen(Color.Wheat, 1); 
int margin = 5; 
private void CustomControl_Paint(object sender, PaintEventArgs e) { 
    CustomControl calendar = (CustomControl)sender; 
    Graphics graphics = e.Graphics; 
    graphics.Clear(Color.WhiteSmoke); 

    graphics.DrawRectangle(penDefaultBorder, margin, margin, calendar.Width - margin * 2, calendar.Height - margin * 2); 
    //... 
} 

無論是graphics.Clear,也不加入graphics.FillRectangle(...)將隱藏在表面的舊矩形。

想法?謝謝你們。

回答

1

您是否試過.Invalidate()導致窗體重繪?

+0

這將工作;這是一個霰彈槍的方法,但它會工作。我仍然喜歡只在需要更改時渲染緩衝區,並複製OnPaint中請求的區域。 – overslacked 2010-05-13 15:36:52

+0

嗯 - 這很簡單;-)感謝您的幫助,現在調整大小按預期工作。 – dhh 2010-05-13 15:38:23

2

畫面事件通常不會請求整個畫布的更新,只是PaintEventArgs中指定的區域。我猜測發生了什麼事情是隻有畫布的新暴露區域正在PaintEventArgs中傳遞。

這是您不應該在Paint事件中進行任何渲染的原因之一。您應該渲染到離屏位圖 - 緩衝區 - 並從該緩衝區複製到Paint事件中的控件畫布。

在這裏或谷歌搜索「雙緩衝」會給你很多這種技術的例子。

+0

感謝您的提示 - 我發現了一些有關雙緩衝的有趣文章。我會看看那些。 – dhh 2010-05-13 15:54:38