In retrospect... my use of the term overloading was a poor choice of words.
But yes, your first example is what I was talking about.
The only way you could do a protected override void OnPaint is if you were
to derive your own control from the base control you are using, that
internally calls your painting code.
You are correct that by the time b_Paint() in your example is called, the
control would already have been drawn by the internal drawing methods.
From what you are saying, you want to tell the control “Do not draw
yourself, I’ll draw you”... and you’ve got two ways of doing that... you
could derive your own control, or simply respond to the Paint event.
If you derive and override OnPaint(), you can do all of your custom work
there and not call the base method.
The other option is to simply do everything in b_Paint(). As you’ve found,
for the most part (more on this later) by the time b_Paint() is called, the
control has already been drawn. You can wipe out what has been drawn with a
call to e.Graphics.Clea r() and then run your drawing code.
Some controls support the option of not being drawn themselves and instead
leaving it up to you to handle the task, this is often called being Owner
Drawn, items such as StatusBar Panels support this by specifying their style.
The advantage to the Panels method is that when you set it to be OwnerDrawn,
it never tries to draw itself and instead calls any attached EventHandlers
saving you a bit of time rather than using b_Paint().
If you are not using a control that permits this automatic behavior and do
not want to derive your own control, listening to the Paint event with
b_Paint() would be your next best bet.
Brendan
"pacemkr" wrote:
Hmm...
Overloading the Paint event looks like a good idea.
However, I suppose by overloading you mean this:
<code>
//in form constructor
Button b = new Button();
b.Paint += new PaintEventHandl er(b_Paint);
private void b_Paint(object sender, PaintEventArgs e)
{
//my paint code
}
</code>
Or is there an actual way of having "protected override void OnPaint
.... etc" for the control?
That aside, inside the override I have access to e.Graphics.
Now that graphics object had already been painted using the data from
the DC of the form (form's DC results in bad data) and then passed on
to my paint event handler , or not?
Now lets asume that it is possible to have an actual override method.
Then its possible to prevent the controls OnPaint method from being
called. But thats not what I'm trying to do. I want the control to draw
itself, but on the DC that has the correct data ( which would be the
one I prepare before hand).
In other words I want to supply the dc and make the control draw
itself.
Thank you for taking the time to answer.
Nick.