User control with transparent background

I have a simple custom control that is actually just a panel, when I click on that panel, a child user control will be added. this child is another custom control where i set width = 150px

and height = 100px

and the background color is transparent. it also has a text box in the center that is 100 x 100 pixels.

this basic structure will be in the future a Node based interface where every box will have communication bindings and btn logic or something like that.

my problem is that if I click multiple times in the panel and the added checkbox is overlaid with another one, the transparency won't take effect.

here is a screenshot

enter image description here

how can i fix this problem? is there allowTransparency

or something like that?

there is also a problem with the drawing order, the newly added blocks are always behind the other.

if you want to see the code for this, let me know, but I don't think there is anything important for this.

also if you know a better way to implement a Node graph please feel free to tell me.

EDIT

The following code was the first one I tried before I even thought about posting the question to StackOverFlow.

SetStyle(ControlStyles.SupportsTransparentBackColor, true);
MakeTransparentControls(this);

      

so please don't take this as a duplicate question or post this code as an answer

+3


source to share


4 answers


This is, generally speaking, a very fundamental limitation in Windows. A window overlaps another window and obstructs what is behind it. Everyone's favorite answer, use ControlStyles.SupportsTransparentBackColor, not a workaround, it's already included for the UserControl so setting it up again doesn't matter.

The style flag simulates the transparency of the window. It does this by changing the view of the window. He first asks the Parent to dip into the window to provide a background, then he draws himself on top of it. Therefore, by setting the BackColor property to Color.Transparent, you can expect the pixels of the parent window to appear as the background.

And you will do it. The parent of your custom control is a form. He dutifully draws himself first and creates gray edges on the left and right. What you were hoping for, you should also see pixels of other custom controls. But that doesn't work, it only asks the parent to draw, not any other control that also overlaps. It is definitely not impossible, it is just ugly and slow. This KB article shows this approach. Rather emphasizing the "ugly" nickname.



It is clear that you can improve your UserControl, there at first glance it makes no sense to have these transparent edges to the left and right of the panel. So set the panel property to (0, 0) and the Dock property to fill. Now every custom control is nothing more than a "node", and you do not need or need to see fragments of any overlapping other "nodes". To draw any lines between nodes, you need to implement the Shape's Paint event.

If you really want this kind of transparency, you'll have to do it differently. You should give up the idea of ​​using a window for each node. One way to do this is simply to draw them. If you do it in the correct order, then you have no problem simulating true transparency created with layered paint, and just don't paint where you need to see the "background". It will also be much faster road controls. However, you will give up the usability of the controls; instead, you will have to write code. Things like the mouse hit test get more complicated. And the textbox will surely hover, unless it really should be a shortcut (surely it is?) Or use a GUI class library.which refused to use the window as a control. Just like WPF. And don't forget that there are many libraries that already do this for you, linked nodes are a very common paradigm for user interface. Visio, for example.

+7


source


try this in the constructor of your UserControl:



this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.BackColor = Color.Transparent;

      

+5


source


In one of my projects, I had to set a transparent background to almost every child of the form, so I created this method:

private void MakeTransparentControls(Control parent)
{
    if (parent.Controls != null && parent.Controls.Count > 0)
    {
        foreach (Control control in parent.Controls)
        {
            if ((control is PictureBox) || (control is Label) || (control is GroupBox) || (control is CheckBox))
                control.BackColor = Color.Transparent;

            if (control.Controls != null && control.Controls.Count > 0)
                MakeTransparentControls(control);
        }
    }
}

      

And in the constructor of the form, I added these lines:

SetStyle(ControlStyles.SupportsTransparentBackColor, true);
MakeTransparentControls(this);

      

You can try something like this in your own context.

0


source


You can also try this:

     public partial class UCTransparent : UserControl
     {

         public UCTransparent()
         {
                InitializeComponent(); 
         }
         protected override CreateParams CreateParams
         {
                get
                {
                       CreateParams cp = base.CreateParams;
                       cp.ExStyle |= 0x20;
                       return cp;
                }
         }

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

      

0


source







All Articles