about handling drawing

T

Tony Johansson

Hi!

When you have a Form with name Form1 with several controls for example
Buttons TextBox and you move another window for example NotePad infront of
this Form1 window and then move this NotePad away so Form1 window is shown
again you don't have to worry about to redraw of any of these controls on
the Form1 window because windows take care of this for you.

I just wonder when I use graphics component like Rectangle and Ellipse from
the drawing namespace like this example below
why can't windows handle the redraw of the window in the same manner that
windows handle the redraw of normal controls like Button,TextBox and so on.
Graphics g = this.CreateGraphics();
Pen bluePen = new Pen(Color.Blue,3);
g.DrawRectangle(bluePen, 0, 0, 10, 10);
Pen redPen = new Pen(Color.Red,2);
g.DrawEllipse(redPen, 0, 0, 80, 60);

//Tony
 
V

vanderghast

One of the reason is that the controls are added to the controls collection
with code like:

this.Controls.Add( ...) ;

(you can spot those line in the automatically generated code when you add a
control to a form)
and when necessary, the form repaint itself and, can loop trough the
controls collection to order them to paint themselves too.


As you already know, using the Paint event of the form is the usual way to
add graphical elements which may evolve in time, dynamically, and there, you
don't need to use this.CreateGraphics, but rather use e.Graphics, with e the
PaintEventArgs argument of the Paint handler.


Vanderghast, Access MVP
 
P

Patrice

Rectangle and Ellipse are not components. They are just statements that are
forgotten as soon as they have been called.

In the first case the whole structure is known at all times as you have
defined a hierarchy of controls that you can inspect whenever you want. As
the OS keeps track of what should be rendered, it allows to perform quite
numerous things for you (hit testing, z-index ordering, redrawing only
obscured parts etc...) etc...

With the second approach you don't have this high level structure but just a
list of the graphic statements producing whatever output you want. So the
only thing Windows can do is to call your statements so that they all
execute.

Note that at some point those two aspects meets (i.e a control will be
ultimately rendered using painting procedures) so ultimately all is drawn
this way but in one case having just a high level view of those controls is
enough while in the second case, you need to deal explicitely with this low
level detail. Similarly you could ship this low level rendering sequence as
a control, making this consumable using the first approach by other
developers that won't have any idea about how your control renders itself at
a lower level...

Those two approaches are known as :

http://en.wikipedia.org/wiki/Retained_mode
http://en.wikipedia.org/wiki/Immediate_mode
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top