Imports System.ComponentModel
Public Class TransparentDGV
' original by Deumber of Stack Overflow Forum
' adapted by IanRyder of VBForums
' corrections, improvements and additional features by dunfiddlin of VBForums
Inherits DataGridView
Private _DGVHasTransparentBackground As Boolean
Private _DGVBackgroundImage As Image
<Category("Transparency"), _
Description("Select whether the control has a Transparent Background.")> Public Property DGVHasTransparentBackground As Boolean
Get
Return _DGVHasTransparentBackground
End Get
Set(value As Boolean)
_DGVHasTransparentBackground = value
If _DGVHasTransparentBackground Then
SetTransparentProperties(True)
Else
SetTransparentProperties(False)
End If
End Set
End Property
Public Property DGVBackgroundImage As Image
Get
Return _DGVBackgroundImage
End Get
Set(value As Image)
_DGVBackgroundImage = value
If Not _DGVBackgroundImage Is Nothing Then
SetTransparentProperties(True)
_DGVHasTransparentBackground = True
ElseIf _DGVHasTransparentBackground Then
SetTransparentProperties(True)
Else
SetTransparentProperties(False)
End If
End Set
End Property
Public Sub New()
Me.BorderStyle = Windows.Forms.BorderStyle.None
End Sub
Private Sub SetTransparentProperties(ByRef SetAsTransparent As Boolean)
If SetAsTransparent Then
MyBase.DoubleBuffered = True
MyBase.EnableHeadersVisualStyles = False
Else
MyBase.DoubleBuffered = False
MyBase.EnableHeadersVisualStyles = True
MyBase.ColumnHeadersDefaultCellStyle.BackColor = SystemColors.Control
MyBase.RowHeadersDefaultCellStyle.BackColor = SystemColors.Control
SetCellStyle(Color.White)
End If
End Sub
Protected Overrides Sub PaintBackground(graphics As System.Drawing.Graphics, clipBounds As System.Drawing.Rectangle, gridBounds As System.Drawing.Rectangle)
MyBase.PaintBackground(graphics, clipBounds, gridBounds)
Dim rectSource As New Rectangle(MyBase.Location, MyBase.Size)
Dim rectDest As New Rectangle(0, 0, rectSource.Width, rectSource.Height)
If _DGVHasTransparentBackground Then
If Not IsNothing(MyBase.Parent.BackgroundImage) Then
Dim pb As New PictureBox
pb.Width = Parent.ClientRectangle.Width
pb.Height = Parent.ClientRectangle.Height
pb.BackgroundImageLayout = Parent.BackgroundImageLayout
pb.BackgroundImage = Parent.BackgroundImage
Dim b As New Bitmap(rectSource.Width, rectSource.Height)
Dim bp As New Bitmap(Parent.ClientRectangle.Width, Parent.ClientRectangle.Height)
pb.DrawToBitmap(bp, New Rectangle(0, 0, bp.Width, bp.Height))
graphics.FromImage(b).DrawImage(bp, rectDest, rectSource, GraphicsUnit.Pixel)
graphics.DrawImage(b, rectDest)
Else
Dim myBrush As New SolidBrush(MyBase.Parent.BackColor)
Dim rectDest1 As New Region(New Rectangle(0, 0, MyBase.Width, MyBase.Height))
graphics.FillRegion(myBrush, rectDest1)
End If
End If
If Not _DGVBackgroundImage Is Nothing Then
graphics.DrawImage(_DGVBackgroundImage, rectDest)
End If
End Sub
Protected Overrides Sub OnColumnAdded(e As System.Windows.Forms.DataGridViewColumnEventArgs)
MyBase.OnColumnAdded(e)
If _DGVHasTransparentBackground Then
SetCellStyle(Color.White)
End If
End Sub
Private Sub SetCellStyle(ByVal cellColour As Color)
For Each col As DataGridViewColumn In MyBase.Columns
col.DefaultCellStyle.BackColor = cellColour
col.DefaultCellStyle.SelectionBackColor = cellColour
Next
End Sub
Private Sub myControl_ParentChanged(ByVal sender As Object, ByVal e As EventArgs) Handles Me.ParentChanged
AddHandler Parent.Resize, AddressOf Parent_Resize
End Sub
Sub Parent_Resize(ByVal sender As Object, ByVal e As EventArgs)
Me.Refresh()
End Sub
Protected Overrides Sub OnScroll(e As System.Windows.Forms.ScrollEventArgs)
MyBase.OnScroll(e)
Me.Refresh()
End Sub
End Class