Transparent Control on Transparent control?
Asked Answered
C

1

0

I created a TransparentTableLayoutPanel:

class TransTablePanel : TableLayoutPanel
{
    public TransTablePanel()
    {

    }

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams createParams = base.CreateParams;
            createParams.ExStyle |= 0x00000020; // WS_EX_TRANSPARENT
            return createParams;
        }
    }

    protected override void OnPaintBackground(PaintEventArgs e)
    {
        // Do not paint background.
    }
}

And a transparent PictureBox (I tried just using BackColor = Transparent and it didn't work)

class TransPicBox : PictureBox
{
    public TransPicBox()
    {

    }

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams createParams = base.CreateParams;
            createParams.ExStyle |= 0x00000020; // WS_EX_TRANSPARENT
            return createParams;
        }
    }

    protected override void OnPaintBackground(PaintEventArgs e)
    {
        // Do not paint background.
    }
}

Here is the result:

enter image description here

First cell is the PictureBox with this paint event:

private void picBoxCompass_Paint(object sender, PaintEventArgs e)
    {
        Bitmap b = Properties.Resources.Compass_Rose;
        float rot = PitControl.GetPitbullRotation();
        e.Graphics.DrawImage(rotateImage(b, rot), 0, 0, picBoxCompass.Width, picBoxCompass.Height);
        e.Graphics.DrawLine(LinePen, picBoxCompass.Width / 2, 0, picBoxCompass.Width / 2, picBoxCompass.Height / 2);
        e.Graphics.FillPie(Brushes.Green, picBoxCompass.Width / 2 - 10, picBoxCompass.Height / 2 - 10, 20, 20, 0, 360);
    }

And you can see that is not Transparent (Black Background) and second cell is transparent (you can see my form's background image).

How can i make the PictureBox Transparent ?

Conidiophore answered 24/10, 2011 at 10:16 Comment(4)
Perhaps your Compass_Rose is missing some transparency?Headgear
Compass_Rose is a PNG with full transparent background :)Conidiophore
I'm not sure, but I think you shoud not override the OnPaintBackground Methods, if you specify transparency as background, you will have to draw itHeadgear
@Headgear I tried using a normal PictureBox with Transparent Background as well without success, and the TranPanel works perfect. (Faster then using Normal Panel with Transparent color)Conidiophore
E
2

Ok Iam adding code that worked for me. Its not very nice(I would like to hear how it could be done better), but maybe it will help you

public class TransPicBox : Control
{
    public Image Image
    {
        get;
        set;
    }

    public TransPicBox()
    {
        SetStyle(ControlStyles.AllPaintingInWmPaint |
                 ControlStyles.SupportsTransparentBackColor, true);
        base.BackColor = Color.FromArgb(0, 0, 0, 0);//Added this because image wasnt redrawn when resizing form
    }

    protected override void OnPaintBackground(PaintEventArgs e)
    {

    }

    protected override void OnPaint(PaintEventArgs e)
    {
        if (Image != null)
        {
            e.Graphics.DrawImage(Image, 0, 0, Image.Width, Image.Height);
        }
    }

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

You will need to set Image property.

Edit: Note: Image should be with transparent background (tested with *.png format)

Result:

enter image description here

Episcopate answered 24/10, 2011 at 14:4 Comment(2)
No flickering in your project when resizing form? When i tested I've got flickering, probably because of hi resolution form background image(image layout style = strech)Episcopate
I used to get flickering but then i created the TransTablePanel class and now no flickering :)Conidiophore

© 2022 - 2024 — McMap. All rights reserved.