Several panels with transparent background in a form

1

I'm doing a layout of setting up a restaurant room, with its tables, stools, bar, etc. I have it almost finished, but I have a design problem that I can not solve. You see, I can add objects to the room (these objects have a transparent background) and they are inserted, and they move well, you see, let's say, the floor behind, without problems.

The problem comes when I want to place or move one of those objects on top of another existing one. In the following image you can see what I'm trying to explain.

The application is made in C # and in the code of the form are the events that make the objects, are placed and move, nothing great. The objects really are panels whose background image is that of the object, and it has a label for the shtick above it. Should I use another type of container?

    
asked by Mario Rodríguez 11.01.2017 в 16:59
source

1 answer

1

WinForms controls do not handle transparencies natively correctly. I suppose that in your panels what you do is use a transparent color in your panel, which uses a "trick" that basically consists of having the background drawn by the parent form (that's why you see that if the background appears, but not the control that is below.

At first I thought that the solution to this was not simple, but according to the solution exposed in this question in StackOverflow it seems that creating a Panel custom control seems possible. I copy the code for that control here:

public class TransparentPanel : Panel
{
    protected override CreateParams CreateParams 
    {            
        get {
            CreateParams cp =  base.CreateParams;
            cp.ExStyle |= 0x00000020; // WS_EX_TRANSPARENT
            return cp;
            }
    }
    protected override void OnPaintBackground(PaintEventArgs e) 
    {
        //base.OnPaintBackground(e);
    }
}
    
answered by 11.01.2017 в 18:07