473,785 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8998
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 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.

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.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.

Nov 17 '05 #4

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

Similar topics

13
2090
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 overflow. All I know is that the program is either stopped due to an exception at the end of Main or has exited after the Stack Overflow exception. (Both cases are Debug builds running in the debugger.) After a lot of trial and error (mostly...
5
15129
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 fast but is not good enough when you have to repaint very frequently. For example, when you scroll the control or select blocks of text quickly. I have it paint its graphics to an offscreen Graphics instance, which I then transfer to the...
11
6200
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 OnPaint . The recangle draws correct when i press the mouse, but when i release the mouse the background is not restored What should i do in the Onpaint to make sure the background (the form) is restored correctly ? This problem already keeps...
0
1907
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 messages in the form which has drawn the control. Evidently, the control is consuming all of these messages. How can I pass them back/on? I have a reference to the owner form but calling Refresh() via this reference isn't helping. Help! Do I need to...
9
7383
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
2574
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 "painting the control". It also has no effect to explicitly call txtTest.Invalidate or txtTest.Refresh. Am I missing something simple here? Any push in the right direction is appreciated.
7
2025
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 control graphics object in the paint event. Any help would be appreciated. Here is my code: public class as mycontrol Private Declare Auto Function BitBlt Lib "GDI32.DLL" (ByVal hdcDest As IntPtr, ByVal nxDest As Integer, ByVal nyDest As...
15
1853
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
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
10356
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10161
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9958
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7506
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5523
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4058
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.