Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / August 2005

Tip: Looking for answers? Try searching our database.

Paint DC of Hidden Control using Managed Code?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
BYY - 30 Jul 2005 00:47 GMT
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.
Lloyd Dupont - 30 Jul 2005 13:32 GMT
never tried it, but I recently discovered this method:
class Control
{
protected void InvokePaint(Control c, PaintEventArgs e);
}

Signature

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

> How do I paint the DC of a hidden control using only managed code?
>
[quoted text clipped - 8 lines]
>
> Thanks.
Lloyd Dupont - 30 Jul 2005 14:19 GMT
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);
       }
   }
}

Signature

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

> never tried it, but I recently discovered this method:
> class Control
[quoted text clipped - 14 lines]
>>
>> Thanks.
Lloyd Dupont - 30 Jul 2005 15:03 GMT
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);
   }
}
Lloyd Dupont - 30 Jul 2005 15:26 GMT
Oops..
works well with CheckBox, doesn';t work with TextBox.... :-/

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

Signature

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);
     }
 }
BYY - 01 Aug 2005 18:15 GMT
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.

> Oops..
> works well with CheckBox, doesn';t work with TextBox.... :-/
[quoted text clipped - 37 lines]
>       }
>   }
BYY - 01 Aug 2005 18:32 GMT
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?

> 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
[quoted text clipped - 41 lines]
> >       }
> >   }

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.