Get the graphics of a hidden control

T

tomko

Hi,

I've found a way to copy the visible presentation of a control on to a
picturebox (using Graphics and GDI). But this works only if the
control is visible. How can I have the control hidden and still
retreive the graphics of the control (have the control draw itself for
me)? And what makes a child control hide itself when its parent is
hidden?

Br,
Tom
 
M

Marc Gravell

How can I have the control hidden and still retreive the graphics of the control?
Move it off-screen, perhaps on a separate form?
And what makes a child control hide itself when its parent is hidden?
Because that is the normaly behavior... if you hide a panel, you
expect the contents of the panel to hide... so where would it paint?

Marc
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

I do not think it can be done, using GDI you can only access the screen,
there is no way for you to get the visible representation of a hidden
control cause, well it's hidden and therefore it has no visible
representation
 
T

tomko

I do not think it can be done, using GDI you can only access the screen,
there is no way for you to get the visible representation of a hidden
control cause, well it's hidden and therefore it has no visible
representation

Could it be possible to write a custom control that would parent
another one, and even if it was hidden, it wouldn't let its child
control know this?

How does a control know if its parent is hidden, disabled etc? Are
there some standard events?

Br,
Tom
 
P

Peter Duniho

tomko said:
I've found a way to copy the visible presentation of a control on to a
picturebox (using Graphics and GDI). But this works only if the
control is visible. How can I have the control hidden and still
retreive the graphics of the control (have the control draw itself for
me)? And what makes a child control hide itself when its parent is
hidden?

It probably depends on the control.

For a control that is entirely managed code, the first thing I'd try is
the DrawToBitmap() method. But it's possible that if you really want to
do this, you might have to subclass the control, create your own Bitmap
for it to draw into, get a Graphics instance from the Bitmap, and then
call OnPaint with a PaintEventArgs with the Graphics property set to
your Graphics instance.

For a control that is substantially unmanaged code (ie, pretty much any
of the built-in controls), it's likely a lot more difficult or
impossible. If it's possible at all, you probably will have to use the
WM_PRINTCLIENT window message, sending it directly to the underlying
window handle for the control. You need to create the Bitmap and
Graphics as above, but then get the HDC from the Graphics and use that
as the wParam when sending the WM_PRINTCLIENT message.

This of course requires that the control actually handles the
WM_PRINTCLIENT message. According to MSDN all of the common controls
do, but you could run into exceptions anyway.

Trying to hack the normal to-screen drawing process of a control,
especially for controls that are essentially unmanaged, is likely to get
you nothing but headaches. :) Hopefully the above ideas give you
something more likely to work regardless of the on-screen state of the
control.

Pete
 

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

Top