C# 设置用户控件的父控件可防止其透明
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/373913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting the parent of a usercontrol prevents it from being transparent
提问by TheCodeJunkie
I've created a simple user control which is manually created with something like
我创建了一个简单的用户控件,它是用类似的东西手动创建的
MyUserControl ctrl = new MyUserControl();
The control have been designed to have BackColor = Color.Transparentand that works fine, until I set the Parentof the control to a form at which time it turns into the color of the form.
该控件被设计为具有BackColor = Color.Transparent并且工作正常,直到我将控件的父级设置为一个表单,此时它会变成表单的颜色。
Might sound like its transparent but what it does is hide all the controls that exist on the form as well. I'm not 100% sure its the control that gets a solid background or something else thats happening when i hook it up, which prevents other controls from showing.
听起来可能是透明的,但它的作用是隐藏表单上存在的所有控件。我不是 100% 确定它的控件会获得纯色背景,或者当我连接它时发生的其他事情,这会阻止其他控件显示。
Basically if you do this
基本上如果你这样做
- Create a form
- Drop a button on it
- In the click handler for the button you do the following
- 创建表格
- 在上面放一个按钮
- 在按钮的单击处理程序中,您执行以下操作
Example
例子
MyUserControl ctrl = new MyUserControl();
ctrl.Parent = this;
ctrl.BackColor = Color.Transparent;
ctrl.Size = this.Parent.ClientRectangle.Size;
ctrl.Location = this.Parent.ClientRectangle.Location;
ctrl.BringToFront();
ctrl.Show();
Basically I want the usercontrol to overlay the entire form, while showing the underlaying controls on the form (hence the transparent background). I do not want to add it to the forms control collection because it doesn't really belong to the form, its just being shown ontop of everything else
基本上我希望用户控件覆盖整个表单,同时在表单上显示底层控件(因此是透明背景)。我不想将它添加到表单控件集合中,因为它并不真正属于表单,它只是显示在其他所有内容之上
I tried doing the same, but without setting the parent, but then the control didnt show at all.
我尝试做同样的事情,但没有设置父级,但是控件根本没有显示。
Thanks!
谢谢!
EDIT: If I override the OnPaintBackground method in the usercontrol and prevent the background from being painted then it works, however that messes up with the transparent parts of a PNG image im painting in the control using DrawImage, which makes sense.
编辑:如果我覆盖用户控件中的 OnPaintBackground 方法并防止绘制背景,那么它可以工作,但是这会与使用 DrawImage 在控件中绘制的 PNG 图像的透明部分混淆,这是有道理的。
采纳答案by Rune Grimstad
Windows Forms doesn't really support transparent controls.
You can work around this limitation by overriding the CreateParams property of the control and setting a custom style (look it up on google).
Further you have to override the painting of your control so that not only your control but also the parent control is redrawn. The reason is that the background must be painted before your control paints itself.
Finally you should override the OnPaintBackground method, as you have done, to make sure no background is painted.
Windows 窗体并不真正支持透明控件。
您可以通过覆盖控件的 CreateParams 属性并设置自定义样式来解决此限制(在 google 上查找)。
此外,您必须覆盖控件的绘制,以便不仅重绘您的控件,还重绘父控件。原因是背景必须在您的控件自行绘制之前绘制。
最后,您应该覆盖 OnPaintBackground 方法,就像您所做的那样,以确保没有绘制背景。
Quite clumsy, and not perfect, but it should work.
相当笨拙,并不完美,但它应该可以工作。