How to make a UserControls BackColor transparent in C#?

后端 未结 3 1533
死守一世寂寞
死守一世寂寞 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
    2020-12-11 11:56

    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);
    

提交回复
热议问题