473,324 Members | 2,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Force control to paint onto a Graphics object.

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.

Nov 17 '05 #1
3 8945
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

"pa*****@gmail.com" wrote:
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.

Nov 17 '05 #2
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.

Nov 17 '05 #3
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
"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 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.

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Will Pittenger | last post by:
I have a Control derived class. When the parent of the control changes the control's Location property, the stack overflows. I have not found a way to find out what was on the stack when it does...
5
by: Alien | last post by:
I have a hex editor-type class that extends UserControl and paints its data to a PictureBox. Basically the problem is that repainting it takes usually between 60 and 80ms, which may seem pretty...
11
by: Sagaert Johan | last post by:
I have made a custom control that draws a rectangle when the mouse is down, and does nothing when the mouse is up. I set/reset a flag in MouseDown/Mouse up and use this to do the drawing in the...
0
by: Duncan Mole | last post by:
Hi, I have created a control which draws a title bar and provides a drop down menu for a Smart Device Application. It seemed to work fine until I came to add an event handler to act on Paint...
9
by: Kevin | last post by:
I would like to print the panel control. The panel controls contains images and lables. If i can capture as image i can use it for other purposes like Zooming etcc.. Any body have any idea...
20
by: BB | last post by:
Hello all, I am trying to override OnPaint in a custom textbox control (so I can drawstring a caption, etc.). In the code below, I get the "painting the form" message as expected, but not the...
7
by: Dennis | last post by:
I am trying to implement drawing on a bitmap and using bitblt to transfer it to the control graphics object in the paint event. It seems to draw on the bitmap ok but doesn't get transferred to the...
15
by: Hamed | last post by:
Have I posted the message to wrong newsgroup? Or Does the question is so much strage? Would someone please kindly direct me to a true newsgroup or resource? Best Regards Hamed
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shllpp 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.