drawing custom control

  • Thread starter Thread starter cronusf
  • Start date Start date
C

cronusf

In my control's constructor I set:

SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);

My reference book, "Programming Windows Forms" by Charles Petzold says
this means that all painting logic is performed in the OnPaint
method.

I override OnPaint and do my custom drawing code. However, I noticed
that if I add child controls to my UserControl, those are painted
automatically. Can someone explain how the drawing of child controls
works?

This behavior is fine--I'd rather them automatically be drawn then
have to do it myself, I just wasn't expecting it since I figured I had
to do all the drawing for my custom control.
 
In my control's constructor I set:

SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);

My reference book, "Programming Windows Forms" by Charles Petzold says
this means that all painting logic is performed in the OnPaint
method.

I override OnPaint and do my custom drawing code. However, I noticed
that if I add child controls to my UserControl, those are painted
automatically. Can someone explain how the drawing of child controls
works?

This behavior is fine--I'd rather them automatically be drawn then
have to do it myself, I just wasn't expecting it since I figured I had
to do all the drawing for my custom control.

Hi,

Controls paint themselves, and only themselves. Any child control you drop
onto it is reponsible for its own painting.

You can add painting to a child control by subscribing to their Paint event
though.

You usually don't have to use SetStyle to create your own drawing.
Overriding OnPaint is sufficient in most cases.
 
Back
Top