以下是解决您的一般问题的示例。创建一个新的 Windows 窗体应用程序项目并将两个PictureBox 控件和两个Label 控件添加到窗体。在每个PictureBox 控件中设置一个Image。将以下代码添加到表单中:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
With Label1
.AutoSize = False
.Size = PictureBox1.Size
.Location = Point.Empty
.Parent = PictureBox1
.BackColor = Color.FromArgb(100, 100, 100)
End With
With Label2
.AutoSize = False
.Size = PictureBox2.Size
.Location = Point.Empty
.Parent = PictureBox2
.BackColor = Color.FromArgb(100, 100, 100, 100)
End With
End Sub
运行项目,您将看到两个PictureBox 控件与Label 控件重叠。第一个PictureBox 完全被相应的Label 遮住,因为Label 是不透明的,而第二个PictureBox 显示通过第二个Label,因为Label 的透明BackColor。
在你的情况下,因为Label 是PictureBox 的父级,你仍然可以像往常一样移动PictureBox,透明的Label 会随之移动。请注意,您不能在设计器中将子控件添加到 PictureBox,因此您必须像我在上面所做的那样在代码中完成。
编辑:
我不会在这里做,但值得注意的是,您实际上可以创建自己的自定义控件,该控件继承 PictureBox,然后在构造函数中创建自己的子 Label。然后,您可以通过控件的属性公开Label 的BackColor,然后您可以通过该属性设置该叠加层的颜色和透明度级别。您可以选择单独公开透明度级别,或者只使用 Color 属性的 Alpha 通道。
编辑:
我说我不会在这里做,但这里是我上面提到的自定义控件的实现:
Imports System.ComponentModel
''' <summary>
''' A picture box control with a transparent overlay.
''' </summary>
Public Class OverlaidPictureBox
Inherits PictureBox
'The label that provides the overlay.
Private WithEvents overlayLabel As Label
''' <summary>
''' Creates a new instance of the <see cref="OverlaidPictureBox"/> class.
''' </summary>
''' <remarks>
''' The overlay color is the default back color but fully transparent.
''' </remarks>
Public Sub New()
Me.New(Color.FromArgb(0, DefaultBackColor.R, DefaultBackColor.G, DefaultBackColor.B))
End Sub
''' <summary>
''' Creates a new instance of the <see cref="OverlaidPictureBox"/> class.
''' </summary>
''' <param name="overlayColor">
''' The overlay color, including the transparency level.
''' </param>
Public Sub New(overlayColor As Color)
overlayLabel = New Label With {.BackColor = overlayColor, .Dock = DockStyle.Fill}
Controls.Add(overlayLabel)
End Sub
''' <summary>
''' The overlay color, including the transparency level.
''' </summary>
''' <returns>
''' A <see cref="Color"/> value representing the overlay color.
''' </returns>
<Category("Appearance")>
Public Property OverlayColor As Color
Get
Return overlayLabel.BackColor
End Get
Set
overlayLabel.BackColor = Value
End Set
End Property
''' <summary>
''' Occurs when the value of the <see cref="OverlayColor"/> property changes.
''' </summary>
Public Event OverlayColorChanged As EventHandler
Private Sub overlayLabel_BackColorChanged(sender As Object, e As EventArgs) Handles overlayLabel.BackColorChanged
OnOverlayColorChanged(EventArgs.Empty)
End Sub
''' <summary>
''' Raises the <see cref="OverlayColorChanged"/> event.
''' </summary>
''' <param name="e">
''' The data fr the event.
''' </param>
Protected Overridable Sub OnOverlayColorChanged(e As EventArgs)
RaiseEvent OverlayColorChanged(Me, e)
End Sub
End Class
只需将该类添加到您的项目并构建,然后新控件将出现在 工具箱 中,您可以像使用任何其他控件一样使用它。默认情况下,它的行为与任何其他 PictureBox 相同,但您将在 Properties 窗口中看到一个新的 OverlayColor 属性。默认情况下应该是 (0, 240, 240, 240)。您可以将前导 0 更改为最高 255 的任何其他值,您会看到叠加层变得更加不透明。如果您希望覆盖不是默认的Control 颜色,您也可以更改其他值。