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

上記のようにコントロールを作成しました。このコントロールを使用すると、アルファ チャネルとランダムな形状の png をインポートできます。そして、他のコントロール、つまりテキストボックス、ピクチャボックスなどの上に表示できます。背景には、親コントロールを表示するだけでなく、そのすぐ下にあるものを常に表示する必要があります。

静的モード、つまりフォームに静止している場合は機能しました。しかし、ドラッグ/移動しようとすると、コントロールが適切にレンダリングされず、他のコントロールの下にも移動します。

4

1 に答える 1

1

背景を移動すると、背景が正しくなくなります。コントロールはそれを認識していないため、その Invalidate() メソッドを呼び出して通知する必要があります。

コントロールの下に沈むのは、Z オーダーの問題です。Panels、UserControls、GroupBoxes などのネストされたコンテナ コントロールがフォームに含まれている場合、これを修正するのは困難です。それらの上に表示することはできません。しかし、すべてがその親としてフォームを持っている限り、コントロールで BringToFront() を呼び出すと、コントロールが常に上にあることが確認されます。

より一般的なソリューションは、元のフォームをオーバーレイし、TransparencyKey プロパティを BackColor に設定して完全に透明にするフォームです。そのフォームに配置したコントロールは常に一番上に表示されます。このスレッドの私のコードは、オーバーレイのアイデアを示しています。

独自のデザイナーを作成する場合は、この記事をお読みください。

于 2010-03-25T18:34:25.607 に答える