问题描述
我在 Windows 窗体用户控件(由一个单选按钮、三个标签和一个进度条组成)中创建了一个简单的火柴人.
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.
图片如下:
推荐答案
UserControl 已经支持这个,它的 ControlStyles.SupportsTransparentBackColor 样式标志已经打开.您所要做的就是将 BackColor 属性设置为 Color.Transparent.
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.
接下来要记住的是,这种透明度是模拟的,它是通过要求控件的父级绘制自身以产生背景来完成的.因此,重要的是您正确设置了 Parent .如果父级不是容器控件,这有点棘手.就像一个图片框.设计器将使表单成为父级,因此您将看到表单的背景,而不是图片框.您需要在代码中修复它,编辑表单构造函数并使其看起来类似于:
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);
这篇关于如何在 C# 中使 UserControls BackColor 透明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!