Become a fan of Slashdot on Facebook

 



Forgot your password?
typodupeerror
×
User Journal

Journal SurturZ's Journal: Transparent controls in VB.NET

Translated from http://www.bobpowell.net/transcontrols.htm

Create a new subclass of Panel which supports transparency:

Public Class PanelTransparent
        Inherits Panel

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

        Protected Sub InvalidateEx()
                If Parent Is Nothing Then Exit Sub
                Dim rc As New Rectangle(Me.Location, Me.Size)
                Parent.Invalidate(rc, True)
        End Sub

        Protected Overrides Sub OnPaintBackground(ByVal e As System.Windows.Forms.PaintEventArgs)
                'do not allow the background to be painted
        End Sub

        Private mimg As Image
        Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
                e.Graphics.DrawImage(mimg, 0, 0)
        End Sub
        Public Sub New(ByVal img As Image)
                mimg = img
        End Sub
End Class

And in the form...

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim img As Image = Bitmap.FromFile(My.Computer.FileSystem.SpecialDirectories.Desktop & "\pitransgif.gif")
        Dim pnlTransparent As New PanelTransparent(img)
        Me.Controls.Add(pnlTransparent)
        pnlTransparent.BringToFront()
End Sub

This discussion has been archived. No new comments can be posted.

Transparent controls in VB.NET

Comments Filter:

This restaurant was advertising breakfast any time. So I ordered french toast in the renaissance. - Steven Wright, comedian

Working...