当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