0
Public Class HighlightKey
    Inherits Control
    Private m_fillColor As Color = Color.White
    Private m_opacity As Integer = 100
    Private alpha As Integer
    Private m_image As Image

    Public Sub New()

        SetStyle(ControlStyles.SupportsTransparentBackColor, True)
        SetStyle(ControlStyles.Opaque, True)
        Me.BackColor = Color.Transparent
        Console.WriteLine("new Highlight key ")
    End Sub

    Public Property Image() As Image
        Get
            Return m_image
        End Get
        Set(ByVal value As Image)
            m_image = value
        End Set
    End Property

    Protected Overloads Overrides ReadOnly Property CreateParams() As CreateParams
        Get
            Dim cp As CreateParams = MyBase.CreateParams
            cp.ExStyle = cp.ExStyle Or &H20
            Return cp
        End Get
    End Property

    Protected Overloads Overrides Sub OnBackColorChanged(ByVal e As EventArgs)
        If Me.Parent IsNot Nothing Then
            Parent.Invalidate(Me.Bounds, True)
        End If
        MyBase.OnBackColorChanged(e)
    End Sub

    Protected Overloads Overrides Sub OnParentBackColorChanged(ByVal e As EventArgs)
        Me.Invalidate()
        MyBase.OnParentBackColorChanged(e)
    End Sub


    Protected Overrides Sub OnPaintBackground(ByVal pevent As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaintBackground(pevent)
    End Sub

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)

        Dim g As Graphics = e.Graphics
        Dim brush As New SolidBrush(Me.ForeColor)
        Dim StringSize As New SizeF

        alpha = (m_opacity * 255) / 100

        If m_image IsNot Nothing Then
            StringSize = g.MeasureString(Me.Text, Me.Font)
            Dim x As Integer = (CInt(Me.m_image.Width) - CInt(StringSize.Width)) / 2
            g.DrawImage(m_image, 0, 0, m_image.Width, m_image.Height)
            g.DrawString(Me.Text, Me.Font, brush, x, 20)
        End If

        brush.Dispose()
        g.Dispose()
        MyBase.OnPaint(e)
    End Sub

    Public Function SetImgOpacity(ByVal imgPic As Image, ByVal imgOpac As Single) As Image


        Dim bmpPic As New Bitmap(imgPic.Width, imgPic.Height)

        Dim gfxPic As Graphics = Graphics.FromImage(bmpPic)

        Dim cmxPic As New ColorMatrix()
        cmxPic.Matrix33 = imgOpac

        Dim iaPic As New ImageAttributes()
        iaPic.SetColorMatrix(cmxPic, ColorMatrixFlag.[Default], ColorAdjustType.Bitmap)

        gfxPic.DrawImage(imgPic, New Rectangle(0, 0, bmpPic.Width, bmpPic.Height), 0, 0, imgPic.Width, imgPic.Height, _
         GraphicsUnit.Pixel, iaPic)

        gfxPic.Dispose()
        Return bmpPic
    End Function

    Private Sub HighlightKey_LocationChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LocationChanged
        Me.Refresh()
    End Sub

    Private Sub HighlightKey_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.TextChanged
        Me.Refresh()
    End Sub
End Class

我已经创建了一个如上的控件,这个控件允许我导入一个带有 alpha 通道和随机形状的 png。然后它可以显示在任何其他控件之上,即文本框、图片框等。并且背景应始终显示其下方的内容,而不是仅显示其父控件。

如果它是静态模式,它就可以工作,即在表格中静止。但是当我尝试拖动/移动它时,控件无法正确呈现自身,并且还位于其他控件下方。

4

1 回答 1

1

移动时背景不再正确。控件不知道它,你必须通过调用它的 Invalidate() 方法来告诉它。

它在控件下方下降是一个 Z 顺序问题。如果表单包含任何嵌套的容器控件(如 Panels、UserControls 或 GroupBoxes),这可能很难解决。你不能让它显示在这些之上。但只要所有内容都将表单作为其父项,那么在控件上调用 BringToFront() 将确保它始终位于顶部。

更通用的解决方案是覆盖原始表单并将其 TransparencyKey 属性设置为其 BackColor 以使其完全透明。您在该表单上放置的任何控件都将始终位于顶部。我在这个线程中的代码演示了覆盖的想法。

如果您正在创建自己的设计师,您会想阅读这篇文章

于 2010-03-25T18:34:25.607 回答