How to make a UserControls BackColor transparent in C#?
Asked Answered
B

3

2

I created a simple stick man in a Windows Form User-Control (consisting of a radio button and three labels and one progress bar).

I set the back-color of the new user-control to transparent so that when I drag it onto my form, it blends with other colors and drawings on the form. I am not getting what I'm trying to achieve.

Here is the picture:

enter image description here

Brach answered 13/1, 2013 at 17:12 Comment(2)
Metro? WinForms? WPF? Silverlight? Windows Phone? ASP.Net? MonoTouch?Enough
Windows Form,Visual Studio 2010Brach
M
11

UserControl already supports this, its ControlStyles.SupportsTransparentBackColor style flag is already turned on. All you have to do is set the BackColor property to Color.Transparent.

Next thing you have to keep in mind in that this transparency is simulated, it is done by asking the Parent of the control to draw itself to produce the background. So what is important is that you get the Parent set correctly. That's a bit tricky to do if the parent is not a container control. Like a PictureBox. The designer will make the Form the parent so you will see the form's background, not the picture box. You'll need to fix that in code, edit the form constructor and make it look similar to this:

var pos = this.PointToScreen(userControl11.Location);
userControl11.Parent = pictureBox1;
userControl11.Location = pictureBox1.PointToClient(pos);
Mulch answered 13/1, 2013 at 17:55 Comment(6)
Awesome!It was Fantastic (^_^).Thankyou So much Sir:)Brach
By the way, would you look at the similar problem here please: #14373792Brach
I'm not going to try to guess why setting a property to true that's already true would make a difference. This needs to be properly documented, click the Ask Question button.Mulch
@Hans VS 2012, WinForms: if you set a break-point in a UserControl constructor, and look at the value of SupportsTransparentBackColor Property in a Command window using GetStyle: it is 'true. If you explicitly set the Property to 'true in the UserControl constructor, after you invoke InitializeComponent(); elements of the UserControl will appear quite differently than if omit the call, or place it before InitializeComponents(); ... I seldom use transparency in WinForms/UserControls because of its limitations and "quirks."Detrital
@Hans Passant, I m adding usercontrol as, elementHost1.Parent = this; elementHost1.BackColor = Color.Transparent; elementHost1.Size = this.Size; elementHost1.Location = this.Location; But the usercontrol is not showing transparent, please suggest.Duggan
This enabled me to get a WPF ElementHost to behave as transparent when added to a WinForms Panel - thank you @HansPassant (only 12 years after you provided this reply!)Risorgimento
P
4

In constructor set style of control to support a transparent backcolor

SetStyle(ControlStyles.SupportsTransparentBackColor, true);

and then set Background to transperent color

this.BackColor = Color.Transparent;

From MSDN

A more complex approach (and possibly working one) is described here - with overrding of CreateParams and OnPaint.

Postaxial answered 13/1, 2013 at 17:18 Comment(6)
Thanks,But that seems to distort the control ! see : upload.ustmb.ir/uploads/13_1313580995401.jpgBrach
Got me on the right path. used style {opacity: 0.4;filter: alpha(opacity=40); }Millen
@Anthony Horne - that looks like CSS, not C#/WinFormsUncommunicative
@GeorgeBirbilis - It isn't, but what makes you say that? The var perhaps?Millen
@Anthony Horne I meant the filter: alpha(opacity=40)Uncommunicative
@GeorgeBirbilis Don't have that code any longer. Most likely it was a combination of both - did get me on the right path and solve my problem - hence the mark-up.Millen
B
2

Why all those things? UserControl class has property Region. Set this to what ever shape you like and no other adjustments are needed.

public partial class TranspBackground : UserControl
{
    public TranspBackground()
    {
        InitializeComponent();
    }

    GraphicsPath GrPath
    {
        get
        {
            GraphicsPath grPath = new GraphicsPath();
            grPath.AddEllipse(this.ClientRectangle);
            return grPath;
        }
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        // set the region property to the desired path like this
        this.Region = new System.Drawing.Region(GrPath);

        // other drawing goes here
        e.Graphics.FillEllipse(new SolidBrush(ForeColor), ClientRectangle);
    }

}

The result is as in the image below:

enter image description here No low level code, no tweaking, simple and clean. There is however one issue but in most cases it can go undetected, the edges are not smooth and anti-aliasing will not help either. But the workaround is fairly easy. In fact much easier than all those complex background handling..

Barbary answered 21/12, 2015 at 20:48 Comment(2)
yeah, pretty late but I appreciate the answer;)Brach
Unfortunately I wasn't very active on Stackoverflow. Hopefully it will change now :) And even though it is too late, I hope it was new different approach that you will learn from my answer :PBarbary

© 2022 - 2024 — McMap. All rights reserved.