【问题标题】:OnPaint method not drawing the rectangle properlyOnPaint 方法未正确绘制矩形
【发布时间】:2012-06-28 13:54:26
【问题描述】:

我有以下绘制矩形的绘制事件(表单):

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);
}

矩形显示片刻,然后立即消失。矩形只会在用户调整表单大小时再次显示。

我该如何解决这个问题?

【问题讨论】:

  • 您需要在使用 Pen 后进行 Dispose()。或者使用普通笔。

标签: c# .net winforms paint


【解决方案1】:

不要使用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);
}

【讨论】:

  • 工作出色。谢谢。
猜你喜欢
  • 1970-01-01
  • 2019-06-15
  • 2015-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多