RE: drawing custom control

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance




"cronusf@xxxxxxxxx" wrote:

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.

--
Happy Coding!
Morten Wennevik [C# MVP]

.



Relevant Pages

  • Re: Owner draw repaint
    ... And consider drawing on a dialog to be a Fundamentally Bad Idea. ... Where are you "painting" these items? ... CStatic and do it in the OnPaint of your CStatic-derived subclass. ...
    (microsoft.public.vc.mfc)
  • Re: CListCtrl owner draw paint issue
    ... If you are painting the background of the ... OnPaint that I wrote is called by problem and everything works fine except ... that the first item in the list has paint problem. ... Is there anyway to invalidate and forcefully paint the complete area of ...
    (microsoft.public.vc.mfc)
  • Re: Odd GDI bug - Full and complete example enclosed!
    ... then the controls will flicker in any case. ... still seeing flicker and CE doesn't support double-buffering itself, you might need to implement it yourself at the form level. ... It's _possible_ that you could _only_ implement it at the form level, and the child controls within the form would get double-buffered "for free". ... override OnPaint(), replace the PaintEventArgs with your own before calling base.OnPaint, then of course do whatever rendering your custom form code does. ...
    (microsoft.public.dotnet.languages.csharp)
  • drawing custom control
    ... I override OnPaint and do my custom drawing code. ... Can someone explain how the drawing of child controls ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: Text over progressbar
    ... I did the OnPaint,, just a small test, ... Graphics the wrong one to use? ... >Rene Sørensen wrote: ... >override the OnPaint method, call the base, then do your own painting ...
    (microsoft.public.dotnet.languages.csharp)