2

I have a windows form which contains a user control. This user control has the following code:

protected override void OnPaint(PaintEventArgs pe)
{
  base.OnPaint(pe);
  pe.Graphics.DrawRectangle(
       new Pen(Color.Red, 5 + laenge), 
       new Rectangle(
             new Point(50 + leerzeichen, hoehe), 
             new Size(laenge + 20, 20)));
}

and some more code, which is probably not important now. So when I start the programm it draws the red rectangle. All the variables (laenge, leerzeichen, hoehe) are set to 0 at the beginning of the program. Now, when I press a button the variables are changing, but OnPaint does not draw the new rectangle? What could be the problem? Do I have to call OnPaint in some way?

4

2 回答 2

5

更改变量后,您需要调用Invalidate()(它在OnPaint内部调用)

于 2013-01-11T08:46:29.350 回答
0

您不直接调用 OnPaint。

相反,由于继承自 Win32 (InvalidateRect()),控件的区域需要无效(例如通过调用 Invalidate())以使 Windows 在刷新期间调用控件的 OnPaint 方法。(见这个问题

请注意,操作系统只能在应用程序等待 Windows 消息队列时处理绘制/刷新请求(即完成处理用户请求或调用 Application.DoEvents())。

于 2013-01-11T08:52:43.793 回答