locked
Transparent user control RRS feed

  • Question

  • hi,

    i need to create a transparent user control in which we can set the OPACITY of that control as we do in forms.

    im using c# 2005.

    thanks in advance for ur help..!

     

    Wednesday, October 31, 2007 12:18 PM

Answers

  • To make a UserControl transparent, we have to give it a WS_EX_TRANSPARENT style, override its OnPaintBackground method to draw the background with the opacity, and then invalidates its Parent to redraw the control whenever we need to update the graphics, I write the following sample for your information:

    Code Block

    public partial class UserControl1 : UserControl

        {

            public UserControl1()

            {

                InitializeComponent();

            }

     

            protected override CreateParams CreateParams

            {

                get

                {

                    CreateParams cp = base.CreateParams;

                    cp.ExStyle |= 0x00000020;//WS_EX_TRANSPARENT

                    return cp;

                }

            }

     

            private int opacity;

     

            public int Opacity

            {

                get { return opacity; }

                set {

                    opacity = value;

                    this.InvalidateEx();

                }

            }

     

            protected override void OnPaintBackground(PaintEventArgs e)

            {

                Color bk = Color.FromArgb(Opacity, this.BackColor);

                e.Graphics.FillRectangle(new SolidBrush(bk), e.ClipRectangle);

            }

     

            protected void InvalidateEx()

            {

                if (Parent == null)

                    return;

                Rectangle rc = new Rectangle(this.Location, this.Size);

                Parent.Invalidate(rc, true);

            }

        }

     

     



    And codes for testing:


    Code Block

    public partial class Form5 : Form

        {

            public Form5()

            {

                InitializeComponent();

            }

     

            private void Form5_Load(object sender, EventArgs e)

            {

                this.BackgroundImage = Image.FromFile(@"c:\images\test.jpg");

                this.trackBar1.Minimum = 0;

                this.trackBar1.Maximum = 100;

                this.trackBar1.ValueChanged += new EventHandler(trackBar1_ValueChanged);

            }

     

            void trackBar1_ValueChanged(object sender, EventArgs e)

            {

                int opacity = (int)((this.trackBar1.Value * 255) / 100);

                this.userControl11.Opacity = opacity;

            }

        }

     

     

    Sunday, November 4, 2007 4:29 PM

All replies

  • To make a UserControl transparent, we have to give it a WS_EX_TRANSPARENT style, override its OnPaintBackground method to draw the background with the opacity, and then invalidates its Parent to redraw the control whenever we need to update the graphics, I write the following sample for your information:

    Code Block

    public partial class UserControl1 : UserControl

        {

            public UserControl1()

            {

                InitializeComponent();

            }

     

            protected override CreateParams CreateParams

            {

                get

                {

                    CreateParams cp = base.CreateParams;

                    cp.ExStyle |= 0x00000020;//WS_EX_TRANSPARENT

                    return cp;

                }

            }

     

            private int opacity;

     

            public int Opacity

            {

                get { return opacity; }

                set {

                    opacity = value;

                    this.InvalidateEx();

                }

            }

     

            protected override void OnPaintBackground(PaintEventArgs e)

            {

                Color bk = Color.FromArgb(Opacity, this.BackColor);

                e.Graphics.FillRectangle(new SolidBrush(bk), e.ClipRectangle);

            }

     

            protected void InvalidateEx()

            {

                if (Parent == null)

                    return;

                Rectangle rc = new Rectangle(this.Location, this.Size);

                Parent.Invalidate(rc, true);

            }

        }

     

     



    And codes for testing:


    Code Block

    public partial class Form5 : Form

        {

            public Form5()

            {

                InitializeComponent();

            }

     

            private void Form5_Load(object sender, EventArgs e)

            {

                this.BackgroundImage = Image.FromFile(@"c:\images\test.jpg");

                this.trackBar1.Minimum = 0;

                this.trackBar1.Maximum = 100;

                this.trackBar1.ValueChanged += new EventHandler(trackBar1_ValueChanged);

            }

     

            void trackBar1_ValueChanged(object sender, EventArgs e)

            {

                int opacity = (int)((this.trackBar1.Value * 255) / 100);

                this.userControl11.Opacity = opacity;

            }

        }

     

     

    Sunday, November 4, 2007 4:29 PM
  • Unfortunately the controls on the UserControl surface are not transparent-ified when I try this technique.

    Would I need to perform a similar operation on those controls as well? Would that even work?

    Currently I've only got an image and a label on the UserControl in question, so if you have any suggestions I'd appreciate it.

    Thanks!


    [edit]

    After thinking about this a bit... (think, think, think...) would it be possible to just draw the image and the text on the UserControl surface, and if so, would those become transparent automatically?

    Just trying to think of a relatively simple workaround...

    [/edit]
    Tuesday, May 20, 2008 12:47 AM
  • Thank you I have been look for this solution for very long time.
    Friday, June 26, 2009 10:05 PM
  • Hello, first of all thank you for the tip !

     

    In order to have every contained control with cleaner background, please modify the OnPaintBackground function like that :

        protected override void OnPaintBackground(PaintEventArgs e)
        {
          Color l_colBackground = Color.FromArgb(Opacity, this.BackColor);
          e.Graphics.FillRectangle(new SolidBrush(l_colBackground), e.ClipRectangle);
    
          Brush l_brFilled = new SolidBrush(Color.FromArgb(255, this.BackColor));
    
          foreach(Control l_ctrlCurrent in this.Controls)
            if(l_ctrlCurrent.Visible) 
              e.Graphics.FillRectangle(l_brFilled, l_ctrlCurrent.Bounds);
        }
    



    Thursday, October 27, 2011 4:23 PM
  • private int opacity = 100;
    public Color brushColor = Color.DeepSkyBlue;//any color you want
    
    
            protected override CreateParams CreateParams
            {
                get
                {
                    CreateParams cp = base.CreateParams;
                    cp.ExStyle |= 0x20;
                    return cp;
                }
            }
    
            protected override void OnPaintBackground(PaintEventArgs e)
            {
                 
            }
    
            protected override void OnMove(EventArgs e)
            {
                base.RecreateHandle();
               
            }
    
            public Color BrushColor
            {
                get
                {
                    return this.brushColor;
                }
                set
                {
                    this.brushColor = value;
                    base.RecreateHandle();
                }
            }
    
            public int Opacity
            {
                get
                {
                    if (this.opacity > 100)
                    {
                        this.opacity = 100;
                    }
                    else if (this.opacity < 0)
                    {
                        this.opacity = 0;
                    }
                    return this.opacity;
                }
                set
                {
                    this.opacity = value;
                    base.RecreateHandle();
                }
            }
    
    
            protected override void OnPaint(PaintEventArgs e)
            {
                int num;
                Graphics graphics = e.Graphics;
                Rectangle rect = new Rectangle(-1, -1, base.Width + 1, base.Height + 1);
                Color brushColor = this.brushColor;
                if (brushColor == Color.Transparent)
                {
                    num = 0;
                }
                else
                {
                    num = (this.opacity * 0xff) / 100;
                }
                Pen pen = new Pen(Color.FromArgb(num, brushColor));
                SolidBrush brush = new SolidBrush(Color.FromArgb(num, brushColor));
                
                //Draw Rectangle
                graphics.FillRectangle(brush, rect);  
                graphics.DrawRectangle(pen, rect);
    
                //Draw Ellipse
                //graphics.FillEllipse(brush, rect);
                //graphics.DrawEllipse(pen, rect);
                pen.Dispose();
                brush.Dispose();
                graphics.Dispose();
            }
    
    
    

    Friday, April 25, 2014 11:45 PM