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.
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] "BYY" <BYY@discussions.microsoft.com> wrote in message news:4A4AAE31-0A8A-491B-826C-48D708D8EB93@microsoft.com... > 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. >
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] "Lloyd Dupont" <ld@NewsAccount.galador.net> wrote in message = news:etLFRLQlFHA.3288@TK2MSFTNGP09.phx.gbl... > never tried it, but I recently discovered this method: > class Control > { > protected void InvokePaint(Control c, PaintEventArgs e); > } >=20 > --=20 > There are 10 kinds of people in this world. Those who understand = binary and=20 > those who don't. > "BYY" <BYY@discussions.microsoft.com> wrote in message=20 > news:4A4AAE31-0A8A-491B-826C-48D708D8EB93@microsoft.com... >> 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.=20 >> only >> managed code)? >> >> Thanks. >>=20 >=20
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); }
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] "Lloyd Dupont" <ld@NewsAccount.galador.net> wrote in message = news:up6%23l9QlFHA.3032@TK2MSFTNGP10.phx.gbl...
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); }
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] "Lloyd Dupont" wrote: > 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. > "Lloyd Dupont" <ld@NewsAccount.galador.net> wrote in message news:up6%23l9QlFHA.3032@TK2MSFTNGP10.phx.gbl... > 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); > } > } >
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] "BYY" wrote: > 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. > > "Lloyd Dupont" wrote: > > > 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. > > "Lloyd Dupont" <ld@NewsAccount.galador.net> wrote in message news:up6%23l9QlFHA.3032@TK2MSFTNGP10.phx.gbl... > > 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); > > } > > } > >
Don't see what you're looking for? Try a search.
|