Transparent background

Hi,

Is it at all possible to give a label or another control a transparent background I want to display text, and in most cases the ability to click on it, without spoiling the background image of my panel or form.

Regards,

  Guido


Answer this question

Transparent background

  • Sarit Kommineni

    Aha! Idea

    If I select the backcolor in the property window, the forecolor is alright.
    Before, I did so in the form constructor. A small bug




  • cjtaylor

    Hi Guido

    Some controls do support transparent background color including Label, LinkLabel, Panel, RadioButton, CheckBox.

    For those controls just set the BackColor property to Color.Transparent. This will have the effect you want.

    Regards,

    Michael

  • yourself_20052006

    Wouldn't that cost a lot of time
    I'm talking render-loop here.

    I can of course use MSPaint to create a small background image for a control so that it coincides with the panel's background image.

    Labels don't seem to allow background images though.

    Edit: oh well, I managed with a button.

    Regards,

      Guido

  • rangalo

    Thanks, it appears that that's possible. The existence of a transparency property had me believe that I shouldn't check the colors ...

    Unfortunately, when the backcolor is set to transparent, the forecolor is made transparent automatically as well.

    I can still go this way in cases where I don't display text, however, i.e. basically let the user click on an area of the form's background. Very handy. Smile


  • Andy W

    Hi Guido

    This should not affect the foreground color at all.
    How are your displaying text Can you post some code for this.

    Just some more backrgound information how this transpareny feature works.
    The controls OnPaintBackground will simulate transparency by asking its parent control to paint the background.

    Here is a working sample. It's a a simple Form containing a "transparent" Panel. The panel itself contains a Label control showing some text.



    public class Form1 :
    Form
    {
       public Form1 ()
       {
          InitializeComponent ();
       }
       
       private void panel1_Paint (object sender, PaintEventArgs e)
       {
          using (SolidBrush brush = new SolidBrush (this.ForeColor))
          {
             // draw string using the forms fore color
             e.Graphics.DrawString ("Hello World", this.Font, brush, this.ClientRectangle);
          }
       }

    #region
    Windows Form Designer generated code
    private
    void InitializeComponent ()
    {
       this.panel1 = new System.Windows.Forms.Panel ();
       this.label1 = new System.Windows.Forms.Label ();
       this.panel1.SuspendLayout ();
       this.SuspendLayout ();
       
       this.panel1.BackColor = System.Drawing.Color.Transparent;
       this.panel1.Controls.Add (this.label1);
       this.panel1.Location = new System.Drawing.Point (32, 12);
       this.panel1.Name = "panel1";
       this.panel1.Size = new System.Drawing.Size (200, 126);
       this.panel1.TabIndex = 1;
       this.panel1.Paint += new System.Windows.Forms.PaintEventHandler (this.panel1_Paint);

       this.label1.AutoSize = true;
       this.label1.Location = new System.Drawing.Point (139, 99);
       this.label1.Name = "label1";
       this.label1.Size = new System.Drawing.Size (58, 13);
       this.label1.TabIndex = 0;
       this.label1.Text = "Some Text";

       //
       // Form1
       // 
       this.AutoScaleDimensions = new System.Drawing.SizeF (6F, 13F);
       this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
       this.ClientSize = new System.Drawing.Size (292, 266);
       this.Controls.Add (this.panel1);
       this.DoubleBuffered = true;
       this.Name = "Form1";
       this.Text = "Form1";

       this.panel1.ResumeLayout (false);
       this.panel1.PerformLayout ();
       this.ResumeLayout (false);
    }
    #endregion

       private System.Windows.Forms.Panel panel1;
       private System.Windows.Forms.Label label1;
    }

     



    Michael



  • Brian Welcker

    When you have a transparent background, the mouse events fall through to the control behind.

    However, it should be possible to dynamically generate the background for your control as a bitmap, based on the container's background.  You would have to get the parent's background as an Image and crop that image based on the bounds of your control and set the cropped image as the background of your control.  That'll give the effect of (static) transparency with mouse events enabled.

  • Abstract Labs

    Yeah, the transparency property can create a really cool effect. Big Smile

    My text is created by assigning it to the text property of the label in the editor.
    If the backcolor is, say, orange, the text is displayed in my chosen color, but as soon as I set the backcolor to transparent the text does no longer show. The label is there though since the onmouseclick event works.

  • Transparent background