0

描画部分のあるWindows.Formがあります。これは次のとおりです。

this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);

System.Windows.Forms.Control.Invalidate()メソッドがあります。このメソッドは、50ミリ秒ごとに呼び出され、描画されたものをすべて消去し、要求されたものをすべて描画します。

System.Drawing.Graphics.DrawRectangle()

しかし、必要なのは、以前に描画されたものをすべて消去するのではなく、新しい長方形を追加するためだけに必要なことです。この機能を利用するには、どの方法を置き換える必要がありますか?

4

1 に答える 1

0

Invalidate means that your paint area is no longer valid.. ie, if the user resized the window and all your drawing proportions should react to that event or if something should be deleted from your drawing while it is over something else and etc.. So it is logical to assume that if you want to react to "Invalidate" you need to delete the area and everything should be re-drawn.

So - that been explained but what you really need is some array that holds the elements you need to draw and use invalidate to draw them.. ie: (I'll write it in pseudo code)

List<Shape> Shapes = new List<Shape>();

ctor:
Shapes.Add(rect1);
Shapes.Add(rect2);
...
and so on..

void some_user_incoming_event(){
    shapes.Add(rect200);
    Invalidate();
}

public void Invalidate()
{
    foreach(var shape in shapes) {code to draw specific shape}  
}

If you would use that tactic you will get to the result you want but as mentioned in the notes above this answer - you need to stop calling invalidate every x msecs.. or you will see flickering. Instead of that - you need to call Invalidate manually each time something has changed.

This tactic is also known as MVP (model-view-presenter) where the List is your model, your graphics area is your view, and your Invalidate() method is your presenter.

I hope this helps.

于 2013-02-03T20:05:33.060 に答える