How to draw on multiple controls in Windows Form

Problem: Draw on multiple controls in Windows Form? And this is what I want

Drawing on multiple controls in Windows Form

Hmm.. I met this problem when I did my project. I found a lot but there wasn’t anything meeting my requirement. But it’s not useless. There is the code to create a control as a transparent layer.

class Layer:Control
{
   public Layer()
   {
      SetStyle(ControlStyles.SupportsTransparentBackColor, true);
      SetStyle(ControlStyles.Opaque, true); this.BackColor = Color.Transparent;
   }

   protected override CreateParams CreateParams
   {
      get
      {
         CreateParams cp = base.CreateParams;
         cp.ExStyle |= 0x20; return cp;
      }
   }

   protected override void OnPaint(PaintEventArgs e)
   {
      base.OnPaint(e);
   }
}

That is, however, not the whole story. I bet if you have above code, you can’t still solve this problem. I did😀. The importance is how to use the code to resolve the problem. Again, I made lots of effort to find out the solution and finally paid off. And this is the solution… Normally, you will add controls to the form then draw the line by using Graphic object of the form. Let’s try it and see the result😀. . . . The line is not visible, right? Why? The form still draws the line but controls are on the top of the form and that’s reason why you can’t see the line. In this case, the key is that container of controls is not the form. Instead, it should be the transparent layer😀. Again, let’s try it😀.

Tags: , ,

About ninjapro

It is better to feel by yourself about me

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: