How to make a UserControls BackColor transparent in C#?

后端 未结 3 1540
死守一世寂寞
死守一世寂寞 2020-12-11 11:39

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 use

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-11 12:01

    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:

    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..

提交回复
热议问题