all groups > dotnet windows forms > july 2005 >
You're in the

dotnet windows forms

group:

Paint DC of Hidden Control using Managed Code?


Paint DC of Hidden Control using Managed Code? BYY
7/29/2005 4:47:01 PM
dotnet windows forms:
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.
Re: Paint DC of Hidden Control using Managed Code? Lloyd Dupont
7/30/2005 12:00:00 AM
never tried it, but I recently discovered this method:
class Control
{
protected void InvokePaint(Control c, PaintEventArgs e);
}

--
There are 10 kinds of people in this world. Those who understand binary and
those who don't.
[quoted text, click to view]

Re: Paint DC of Hidden Control using Managed Code? Lloyd Dupont
7/30/2005 12:00:00 AM
yeah, it really seems to work!
try that:
class CapturePanel : Control
{
public void Snapshot(Control c, Bitmap b)
{
using (Graphics g =3D Graphics.FromImage(b))
{
Rectangle rect =3D new Rectangle(new Point(), c.Size);
PaintEventArgs e =3D new PaintEventArgs(g, rect);
this.InvokePaint(c, e);
}
}
}




--=20
There are 10 kinds of people in this world. Those who understand binary =
and those who don't.

[quoted text, click to view]
Final solution Lloyd Dupont
7/30/2005 12:00:00 AM
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 =3D new =
SnapshotControl();
class SnapshotControl : Control
{
public void Snapshot(Control c, Image b, bool background, =
Rectangle r)
{
using (Graphics g =3D Graphics.FromImage(b))
{
Rectangle rect =3D new Rectangle(new Point(), c.Size);
PaintEventArgs e =3D 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 =3D 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);
}
Re: Final solution Lloyd Dupont
7/30/2005 12:00:00 AM
Oops..
works well with CheckBox, doesn';t work with TextBox.... :-/

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

--=20
There are 10 kinds of people in this world. Those who understand binary =
and those who don't.
[quoted text, click to view]
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 =3D new =
SnapshotControl();
class SnapshotControl : Control
{
public void Snapshot(Control c, Image b, bool background, =
Rectangle r)
{
using (Graphics g =3D Graphics.FromImage(b))
{
Rectangle rect =3D new Rectangle(new Point(), c.Size);
PaintEventArgs e =3D 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 =3D 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);
}
Re: Final solution BYY
8/1/2005 10:15:40 AM
OK I figured out that the problem is that the InvokePaint only invokes paint
on the control, but not any of its children. The pain message doesn't
propagate down through the drawing layers.

[quoted text, click to view]
Re: Final solution BYY
8/1/2005 10:32:04 AM
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?

[quoted text, click to view]
AddThis Social Bookmark Button