C# PicBox 背景透明

本文介绍了一段使用C#编程语言的代码片段,该代码展示了如何在绘图事件中遍历控件集合,判断控件类型,并根据不同类型的控件执行相应的操作,如隐藏控件或在背景图片上绘制文本和图片。代码适用于Windows应用程序的界面设计。

private void picBackground_Paint(object sender, PaintEventArgs e)
        {
            foreach (Control C in this.Controls)
            {
                if (C is Label)
                {

                    Label L = (Label)C;

                    L.Visible = false;

                    //设置绘制文字的格式  
                    StringFormat strFmt = new System.Drawing.StringFormat();
                    strFmt.Alignment = StringAlignment.Center; //文本垂直居中  
                    strFmt.LineAlignment = StringAlignment.Center; //文本水平居中  
                    e.Graphics.DrawString(L.Text, this.Font, new SolidBrush(this.ForeColor), new RectangleF(L.Left - picBackground.Left, L.Top - picBackground.Top, L.Width, L.Height), strFmt); 

                }
                else if (C is PictureBox)
                {
                    PictureBox L = (PictureBox)C;
                    if (!L.Name.Equals("picBackground"))
                    {
                        L.Visible = false;
                        ImageAttributes attrib = new ImageAttributes();
                        //Bitmap img = new Bitmap(L.Image);
                        Color color = Color.Transparent;
                        attrib.SetColorKey(color, color);
                        e.Graphics.DrawImage(L.Image, new Rectangle(L.Left - picBackground.Left, L.Top - picBackground.Top, L.Width, L.Height), 0, 0, L.Image.Width, L.Image.Height, GraphicsUnit.Pixel, attrib);
                    }
                }
            }
        }

转载于:https://www.cnblogs.com/xsmhero/archive/2012/08/02/2620262.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值