开发者

How to make the User's Control's BackGround transparent?

I want my UserControl's BackGround to look like it's not there. 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.BackgroundIm开发者_如何转开发age = (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. 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. 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. I want it to look just like there were no UserControl's background.


Make usercontrol backcolor as Transparent this.BackColor = Color.Transparent;


You can use , 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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜