Paint DC of Hidden Control using Managed Code?

G

Guest

How do I paint the DC of a hidden control using only managed code?

There was a post under microsoft.public.dotnet.framework.windowsforms called
"Capturing image of a hidden control" and the person responding suggested
using an interop call to SendMessage in User32.dll to accomplish the task.

Is there a way to avoid doing the interop and use only .NET calls (i.e. only
managed code)?

Thanks.
 
L

Lloyd Dupont

never tried it, but I recently discovered this method:
class Control
{
protected void InvokePaint(Control c, PaintEventArgs e);
}
 
L

Lloyd Dupont

yeah, it really seems to work!
try that:
class CapturePanel : Control
{
public void Snapshot(Control c, Bitmap b)
{
using (Graphics g = Graphics.FromImage(b))
{
Rectangle rect = new Rectangle(new Point(), c.Size);
PaintEventArgs e = new PaintEventArgs(g, rect);
this.InvokePaint(c, e);
}
}
}
 
L

Lloyd Dupont

Terminator is coming!...
hmm.. sorry...
here you go (it just happened I upgrade my code to use that as well ;-):

public class Snapshot
{
static readonly SnapshotControl snapshotMaker = new SnapshotControl();
class SnapshotControl : Control
{
public void Snapshot(Control c, Image b, bool background, Rectangle r)
{
using (Graphics g = Graphics.FromImage(b))
{
Rectangle rect = new Rectangle(new Point(), c.Size);
PaintEventArgs e = new PaintEventArgs(g, rect);
if (background)
this.InvokePaintBackground(c, e);
this.InvokePaint(c, e);
}
}
}
public static Image GetSnapshot(Control c)
{
return GetSnapshot(c, true);
}
public static Image GetSnapshot(Control c, bool withBackground)
{
Bitmap image = new Bitmap(c.Width, c.Height);
GetSnapshot(image, c, withBackground, new Rectangle(0, 0, c.Width, c.Height));
return image;
}
public static void GetSnapshot(Image dst, Control c, bool withBackground, Rectangle rect)
{
snapshotMaker.Snapshot(c, dst, withBackground, rect);
}
}
 
L

Lloyd Dupont

Oops..
works well with CheckBox, doesn';t work with TextBox.... :-/

well I have to keep my old win32 code :-/

--
There are 10 kinds of people in this world. Those who understand binary and those who don't.
Terminator is coming!...
hmm.. sorry...
here you go (it just happened I upgrade my code to use that as well ;-):

public class Snapshot
{
static readonly SnapshotControl snapshotMaker = new SnapshotControl();
class SnapshotControl : Control
{
public void Snapshot(Control c, Image b, bool background, Rectangle r)
{
using (Graphics g = Graphics.FromImage(b))
{
Rectangle rect = new Rectangle(new Point(), c.Size);
PaintEventArgs e = new PaintEventArgs(g, rect);
if (background)
this.InvokePaintBackground(c, e);
this.InvokePaint(c, e);
}
}
}
public static Image GetSnapshot(Control c)
{
return GetSnapshot(c, true);
}
public static Image GetSnapshot(Control c, bool withBackground)
{
Bitmap image = new Bitmap(c.Width, c.Height);
GetSnapshot(image, c, withBackground, new Rectangle(0, 0, c.Width, c.Height));
return image;
}
public static void GetSnapshot(Image dst, Control c, bool withBackground, Rectangle rect)
{
snapshotMaker.Snapshot(c, dst, withBackground, rect);
}
}
 
G

Guest

Getting all child controls to paint is just a matter of recursively calling
InvokePaint on the Controls collection.

The unsolved problem is figuring out how to get the right
System.Windows.Forms.Control.Wm<Message> to controls like TextBox,
TrackSlider, ComboBox etc. (I think)

Basically controls that are drawing themselves correctly have some paint
methods within there defintion. The controls that do not paint correctly
listen for Windows Messages and have the WndProc(ref Message m) method
overriden within their definition.

FYI - calling InvokePaint on a TextBox or ComboBox only draws a white
(background of the textbox) rectangle. The text, borders and button
(combobox) are not drawn. So, the question is how to get all of these other
parts of these controls to draw?
 

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