Force control to paint onto a Graphics object.

  • Thread starter Thread starter pacemkr
  • Start date Start date
P

pacemkr

Is it possible to force a control to paint to a Graphics object (or
Device Context, or a bitmap, anywhere aside from the form) that I
provide.

I am writing a windows form class that supports full alpha blending.
Basically I'm trying to make a layered window that supports controls.
In order to do that I need to redirect painting of the controls to a
memory DC (which is then sent to the layered window). All this asuming
that I figured out how to capture events in such a window.

Thanks in advance,
Nick.
 
Yes... however you may not like the result.

If you overloaded the Paint event of the control, you would have your
regular painting code that uses a reference to your own Graphics instance and
not the one passed in by the Paint event. The drawback of this type of method
is that your control would not get drawn by the Paint event, you would have
to do so later yourself.

Brendan
 
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 PaintEventHandler(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.
 
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.Clear() 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
 

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

Back
Top