设置用户控件的父防止它被透明 [英] Setting the parent of a usercontrol prevents it from being transparent

查看:110
本文介绍了设置用户控件的父防止它被透明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了手动的东西,如

I've created a simple user control which is manually created with something like

MyUserControl ctrl = new MyUserControl();



控制已被设计为具有的背景色= Color.Transparent 的那做工精细,直到我设置的的控制到那时它变成了形式的颜色形态。

The control have been designed to have BackColor = Color.Transparent and that works fine, until I set the Parent of the control to a form at which time it turns into the color of the form.

初听像它的透明,但它的作用是隐藏窗体上存在以及所有控件。我不是100%肯定它是获得一个坚实的背景或别的东西多数民众赞成在发生,当我把它挂起来,以防止其他控件从显示控制。

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.

基本上,如果你这样做


  • 创建一个表单

  • 挂断上一个按钮

  • 在为你做的按钮单击处理以下

示例

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



基本上我想要的usercontrol叠加整个表单,同时也展现形式在垫层控制(因此透明背景)。我不想将它添加到窗体控件集合,因为它并没有真正属于形式,它只是被显示ontop一切

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.

谢谢!

编辑:如果我重写在该用户的OnPaintBackground方法和防止油漆,然后它工作的背景,但是,使用的DrawImage一个PNG图像IM画控制的透明部分弄乱了,这是有道理的。

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.

推荐答案

Windows窗体真的不支持透明控制。

。您可以通过覆盖的CreateParams属性解决此限制控制和设置自定义样式(看它在谷歌)。

此外,您必须重写控件的绘制,不仅是你的控制,而且父控件重绘。其原因是,后台必须在控制油漆本身之前涂上。

最后,你应该重写OnPaintBackground方法,因为你所做的一切,以确保没有背景画。

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.

很笨拙,并不完美,但它应该工作。

Quite clumsy, and not perfect, but it should work.

这篇关于设置用户控件的父防止它被透明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆