简体   繁体   English

如何使用户控件的背景透明?

[英]How to make the User's Control's BackGround transparent?

I want my UserControl's BackGround to look like it's not there.我希望我的 UserControl 的背景看起来不存在。 I tried to do it like that:我试着这样做:

if ((Parent != null) && (Parent.BackgroundImage != null))
{
    Bitmap backGroundImage = new Bitmap(Width, Height);
    Graphics.FromImage(backGroundImage).DrawImage(owner.BackgroundImage, 0, 0, new Rectangle(Location.X, Location.Y, Width, Height), GraphicsUnit.Pixel);
     this.BackgroundImage = (Image)backGroundImage;
}

and it works, but I also have to calculate BackGroundImage of the Form which is UserControl's Parent every time its size changes and set Form's BackGroundImageLayout to none.它可以工作,但我还必须在每次其大小更改时计算作为 UserControl 父级的 Form 的 BackGroundImage,并将 Form 的 BackGroundImageLayout 设置为无。 My UserControl could take Parent's BackGroundImage and stretch it every time it needs to redraw, and then just take a piece of it and set it as its BackGroundImage, but Form draws its stretched background outside its visible rectangle.我的 UserControl 可以获取 Parent 的 BackGroundImage 并在每次需要重绘时对其进行拉伸,然后只获取其中的一部分并将其设置为 BackGroundImage,但 Form 在其可见矩形之外绘制其拉伸的背景。 So I can see then the diffrence, it doesn't fit.所以我可以看到差异,它不适合。

I made it working and it fits the Parent's BackGroundImage, but it's a lot of code and slowing down my program when resizing.我让它工作并且它适合父母的背景图像,但是它有很多代码并且在调整大小时会减慢我的程序。

There must be a simplest way to do that.必须有一个最简单的方法来做到这一点。 I don't mean using ControlStyles.SupportsTransparentBackColor.我不是说使用 ControlStyles.SupportsTransparentBackColor。 I want it to look just like there were no UserControl's background.我希望它看起来就像没有 UserControl 的背景一样。

Make usercontrol backcolor as Transparent this.BackColor = Color.Transparent;使用户控件背景色为透明this.BackColor = Color.Transparent;

You can use, someUserControl.BackColor = Color.FromKnownColor(KnownColor.Transparent);您可以使用 someUserControl.BackColor = Color.FromKnownColor(KnownColor.Transparent); and invisible BackColor.和不可见的背景色。

if you need parent is transparent you can use this code:如果您需要父母是透明的,您可以使用以下代码:

 parent.BackColor = Color.Red;
 parent.TransparencyKey = parent.BackColor;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM