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

halfer
  • 19,824
  • 17
  • 99
  • 186
Hossein
  • 24,202
  • 35
  • 119
  • 224

3 Answers3

10

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);
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Awesome!It was Fantastic (^_^).Thankyou So much Sir:) – Hossein Jan 13 '13 at 18:00
  • By the way, would you look at the similar problem here please: http://stackoverflow.com/questions/14373292/how-can-i-convert-an-image-to-a-drawing-in-c – Hossein Jan 17 '13 at 16:51
  • 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. – Hans Passant Jun 05 '13 at 09:28
  • @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." – BillW Jun 05 '13 at 09:36
  • 2
    @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. – RSB Feb 23 '15 at 08:10
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.

Community
  • 1
  • 1
Algirdas
  • 677
  • 1
  • 6
  • 15
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..

aleksandaril
  • 170
  • 4
  • 14
  • yeah, pretty late but I appreciate the answer;) – Hossein Dec 22 '15 at 05:27
  • 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 :P – aleksandaril Dec 23 '15 at 07:35