winform中Label背景透明的解决方案

当Label控件在PictureBox控件上面时,我们想设置PictureBox控件上的Label控件透明,尝试设置Label的各种属性都无济于事。

现将本人的解决方案提供如下:

1) 新建一个PictureBox控件,名称是pbxImage
2) 新建一些Label控件,放在PictureBox上
3) 添加PictureBox的Paint事件
4) 添加如下代码:

private void pbxImage_Paint(object sender, PaintEventArgs e)
    {
      foreach (Control C in this.Controls)
      {
        if (C is Label)
        {
          Label L = (Label)C;
          L.Visible = false; //其实就是将原来的Label隐藏,在根据Label上的内容画出原内容
          e.Graphics.DrawString(L.Text, L.Font, new
SolidBrush(L.ForeColor), L.Location.X, L.Location.Y);
        }
      }
    }

 对应VB代码如下:

Private Sub pbxImage_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles pbxImage.Paint
        For Each C As Control In Me.Controls
            If TypeOf C Is Label Then
                Dim L As Label = DirectCast(C, Label)
                L.Visible = False
                e.Graphics.DrawString(L.Text, L.Font, New SolidBrush(L.ForeColor), L.Location.X, L.Location.Y)
            End If
        Next
    End Sub

 

转载于:https://www.cnblogs.com/Derek-He/archive/2013/05/02/3055493.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值