New to Telerik UI for WinFormsStart a free 30-day trial

How to achieve transparency effect in Winforms cotrols

Updated over 6 months ago

Environment

Product VersionProductAuthor
2020.2.512RadPanel for WinFormsNadya Karaivanova

Description

A common requirement is to achieve transperancy effect in Winforms control.

Windows Forms controls do not support true transparency. The background of a transparent Windows Forms control is painted by its parent. More information is available here.

This is why if you want to achieve some transparency effect on the control you should simulate it.

Solution

If your requirement is to achieve a transparent RadForm it is suitable to use TransparancyKey property. It gets or sets the color that will represent transparent areas of the form.

C#
public RadForm1()
{
    InitializeComponent();
    this.BackColor = Color.Red;
    this.TransparencyKey = this.BackColor;
}

transparency001.png

If your requirement is to achieve transparency effect on contols there are two approaches that you can use. The first approach is to specify the alpha component of the Color. One of the overloads of the Color.FromArgb method reates a Color structure from the four ARGB component that includes alpha, red, green, and blue values. The alpha component has valid values from 0 to 255 that indicates the opacity/transparency effect.

The following example demonstrated three different transparency effects in RadPanel:

C#
public RadForm1()
{
    InitializeComponent();

    // transparent
    this.radPanel1.BackColor = Color.FromArgb(0, 127, 124, 127);

    //semi transparent
    this.radPanel2.BackColor = Color.FromArgb(120, 127, 127, 127);

    // not transparent
    this.radPanel3.BackColor = Color.FromArgb(255, 127, 127, 127);
}
   

transparency002.png

The second approach is to create associate panel and assign a custom image to the BackgroundImage property. Then, add the associate panel to the Controls collection by using the SetChildIndex and BringToFront methods:

C#
public RadForm1()
{
    InitializeComponent();
    this.radPanel1.BackColor = Color.FromArgb(120, 127, 127, 127);
}

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    this.AddAssociateCoverPanel();
}
public void AddAssociateCoverPanel()
{
    RadPanel associatedPanel = new RadPanel();
    associatedPanel.Size = this.ClientSize;
    associatedPanel.Location = new Point(0, 0);
    Bitmap controlImage = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);
    this.DrawToBitmap(controlImage, new Rectangle(Point.Empty, this.ClientSize));
    SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(128, 255, 255, 255));
    Graphics g = Graphics.FromImage(controlImage);
    g.FillRectangle(semiTransBrush, new Rectangle(Point.Empty, controlImage.Size));
    associatedPanel.BackgroundImage = controlImage;
    RadButton button = new RadButton();
    button.ThemeName = this.ThemeName;
    button.Text = "Button on panel";
    button.Location = new Point(290, 120);
    associatedPanel.Controls.Add(button);
    this.Controls.Add(associatedPanel);
    this.Controls.SetChildIndex(associatedPanel, 0);
    associatedPanel.BringToFront();
}
   

transparency003.png

In this article
EnvironmentDescriptionSolution
Not finding the help you need?
Contact Support