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 ?

svick
  • 236,525
  • 50
  • 385
  • 514
Danpe
  • 18,668
  • 21
  • 96
  • 131
  • Perhaps your Compass_Rose is missing some transparency? – Tokk Oct 24 '11 at 11:00
  • Compass_Rose is a PNG with full transparent background :) – Danpe Oct 24 '11 at 11:13
  • 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 it – Tokk Oct 24 '11 at 11:43
  • @Tokk 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) – Danpe Oct 24 '11 at 12:12

1 Answers1

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

Renatas M.
  • 11,694
  • 1
  • 43
  • 62
  • 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) – Renatas M. Oct 24 '11 at 16:15
  • I used to get flickering but then i created the TransTablePanel class and now no flickering :) – Danpe Oct 24 '11 at 21:03