2012-06-28 83 views
0

我有以下的油漆事件(形式)被绘制矩形:OnPaint方法不绘制矩形正确

void LogicSimulationViewerForm_Paint(object sender, PaintEventArgs e) { 
    Rectangle rect = new Rectangle(100, 100, 400, 100); 
    Graphics c = rtbLogicCode.CreateGraphics(); 
    c.DrawRectangle(new Pen(Color.Black, 3), rect); 
} 

的矩形显示了一瞬间,然后立即消失。矩形只会在用户调整窗体大小时再次显示。

我该如何解决这个问题?

+0

你需要用它之后的Dispose()钢笔。或使用股票钢笔。 –

回答

6

不要使用Control.CreateGraphics()方法,使用PaintEventArgs.Graphics属性:

void LogicSimulationViewerForm_Paint(object sender, PaintEventArgs e) { 
    Rectangle rect = new Rectangle(100, 100, 400, 100); 
    e.Graphics.DrawRectangle(Pens.Black, rect); 
} 
+0

奇怪地工作。谢谢。 – l46kok