无法使前控制在PictureBox上透明

时间:2014-09-26 02:12:10

标签: vb.net user-controls transparency picturebox

我试图在PictureBox控件的顶部放置一个自定义用户控件,但我似乎无法终身如何设置用户控件的透明度,因此它不会切断PictureBox图像。

我的用户控件包含一个RectangleShape,中间有文字,可在图像上创建“徽章”图标(参见下图)。 PictureBox和User Control都位于Panel控件中,我设置了PictureBox.SendToBack()属性和UserControl.BringToFront()属性。

我剩下的就是:

enter image description here enter image description here

我的代码如下:

Option Explicit On
Option Strict On

Imports Microsoft.VisualBasic.PowerPacks

Public Class BadgeIcon
    Inherits UserControl

    Private _value As Integer
    Private canvas As New ShapeContainer
    Private Badge_Icon As New RectangleShape
    Private rect As New Rectangle
    Private m_BorderColor As Color = Color.White
    Private m_FillColor As Color = Color.Red
    Private m_BorderThickness As Integer = 2
    Private m_BadgeFont As New Font("Segoe UI", 7, FontStyle.Bold)
    Private m_BadgeText As String
    Private m_TextColor As New SolidBrush(Color.White)
    Private m_TextSize As Size
    Private m_TextPadding As Integer = 5

    Public Property Value() As Integer
        Get
            Return _value
        End Get
        Set(value As Integer)
            _value = value
            m_BadgeText = CStr(_value)
            m_TextSize = TextRenderer.MeasureText(m_BadgeText, m_BadgeFont)
            rect.Width = m_TextSize.Width + m_TextPadding
            rect.Height = m_TextSize.Height + m_TextPadding
            Me.Refresh()
        End Set
    End Property

    Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams
        Get
            Dim cp As CreateParams = MyBase.CreateParams
            cp.ExStyle = &H20
            Return cp
        End Get
    End Property


    Sub New()
        ' This call is required by the designer.
        InitializeComponent()
        SetStyle(ControlStyles.SupportsTransparentBackColor, True)
        SetStyle(ControlStyles.Opaque, False)
        SetStyle(ControlStyles.DoubleBuffer, True)
        SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        SetStyle(ControlStyles.UserPaint, True)
        Me.BackColor = Color.FromArgb(0, 0, 0, 0)
        UpdateStyles()

        ' Add any initialization after the InitializeComponent() call.
        canvas.Parent = Me
        Badge_Icon.Parent = canvas
        canvas.BackColor = Color.FromArgb(0, 0, 0, 0)

        'Create Badge Icon
        With Badge_Icon
            .BackColor = Color.FromArgb(0, 0, 0, 0)
            .BorderColor = m_BorderColor
            .BorderWidth = m_BorderThickness
            .BorderStyle = Drawing2D.DashStyle.Solid
            .CornerRadius = 11
            .FillColor = m_FillColor
            .FillStyle = FillStyle.Solid
            .SelectionColor = Color.Transparent
        End With

        AddHandler Badge_Icon.Paint, AddressOf BadgeIcon_Paint
    End Sub

    Protected Overrides Sub OnPaint(e As PaintEventArgs)
        MyBase.OnPaint(e)
        DrawBadgeIcon(e)
    End Sub

    Public Sub DrawBadgeIcon(e As PaintEventArgs)
        Try
            'Alter the size of the icon to fix the text
            With Badge_Icon
                .Location = New Point(rect.Left + 1, rect.Top + 1)
                .Size = New Size(rect.Width, rect.Height - 1)
            End With

        Catch ex As Exception
            ErrorTrap(ex, "cls_NotificationBadgeIcon: DrawBadgeIcon()")
        End Try
    End Sub

    Private Sub BadgeIcon_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)
        Dim textRect As New Rectangle(2, 2, m_TextSize.Width + m_TextPadding - 1, m_TextSize.Height + m_TextPadding - 2)

        'Draw the Text
        Dim flags As New StringFormat
        flags.Alignment = StringAlignment.Center
        flags.LineAlignment = StringAlignment.Center
        e.Graphics.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit
        e.Graphics.DrawString(m_BadgeText, m_BadgeFont, m_TextColor, textRect, flags)
    End Sub

End Class

然后将所有内容添加到我的主表单中,我调用以下内容:

Dim pic As New PictureBox
        pic.Image = My.Resources.Notifications
        pic.SizeMode = PictureBoxSizeMode.StretchImage
        pic.Location = New Point(21, 221)
        pic.Size = New Size(42, 29)
        pnlLeftMenuBar.Controls.Add(pic)
        pic.SendToBack()

Dim Counter_Notify As New BadgeIcon
Counter_Notify.Location = New Point(50, 240)
        pnlLeftMenuBar.Controls.Add(Counter_Notify)
        Counter_Notify.BringToFront()

只需使用Counter_Notify.Value = 1更新计数器值。

如何删除切出背景图像的方形矩形?或者我应该以完全不同的方式设置它?我对用户控件有点新鲜。

任何帮助表示赞赏。感谢

1 个答案:

答案 0 :(得分:0)

使用绘画事件,您可以直接在图片框上绘制。

Private Sub pb__Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles pb.Paint
 Dim bgRect As New Rectangle({x,y,width,height})
 Dim textRect As New Rectangle(bgRect.X - {?}, bgRect.Y = {?}, width, height)
 e.Graphics.FillEllipse(New SolidBrush(Color.Red), bgRect)
 e.Graphics.DrawEllipse(New Pen(Color.White, 10), bgRect)
 Using sf As New StringFormat
  sf.LineAlignment = StringAlignment.Center
  sf.Alignment = StringAlignment.Center
 e.Graphics.DrawString("1", {your font}, {your brush}, textRect, sf)
 End Using
End Sub