Results 1 to 3 of 3

Thread: DataGridView With Transparent Background Or Background Image

  1. #1

    Thread Starter
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    DataGridView With Transparent Background Or Background Image

    It is not possible to make a DataGridView truly transparent so this class simulates transparency by copying the parent background. Making use of this process also makes it possible to add a background image directly to the DataGridView.

    Additional Properties:

    DGVHasTransparentBackground Boolean: sets transparency
    DGVBackgroundImage Image: set to Nothing to return to

    Known Issues:

    Double buffering is recommended for all systems, essential on slower systems.
    Scrolling causes a bit of a graphic 'kick' both with and without scrollbars.

    vb.net Code:
    1. Imports System.ComponentModel
    2.  
    3. Public Class TransparentDGV
    4.  
    5.     ' original by Deumber of Stack Overflow Forum
    6.     ' adapted by IanRyder of VBForums
    7.     ' corrections, improvements and additional features by dunfiddlin of VBForums
    8.  
    9.     Inherits DataGridView
    10.  
    11.     Private _DGVHasTransparentBackground As Boolean
    12.     Private _DGVBackgroundImage As Image
    13.  
    14.     <Category("Transparency"), _
    15.       Description("Select whether the control has a Transparent Background.")> Public Property DGVHasTransparentBackground As Boolean
    16.         Get
    17.             Return _DGVHasTransparentBackground
    18.         End Get
    19.         Set(value As Boolean)
    20.             _DGVHasTransparentBackground = value
    21.             If _DGVHasTransparentBackground Then
    22.                 SetTransparentProperties(True)
    23.             Else
    24.                 SetTransparentProperties(False)
    25.             End If
    26.         End Set
    27.     End Property
    28.  
    29.     Public Property DGVBackgroundImage As Image
    30.         Get
    31.             Return _DGVBackgroundImage
    32.         End Get
    33.         Set(value As Image)
    34.             _DGVBackgroundImage = value
    35.             If Not _DGVBackgroundImage Is Nothing Then
    36.                 SetTransparentProperties(True)
    37.                 _DGVHasTransparentBackground = True
    38.             ElseIf _DGVHasTransparentBackground Then
    39.                 SetTransparentProperties(True)
    40.             Else
    41.                 SetTransparentProperties(False)
    42.             End If
    43.         End Set
    44.     End Property
    45.  
    46.     Public Sub New()
    47.         Me.BorderStyle = Windows.Forms.BorderStyle.None
    48.     End Sub
    49.  
    50.     Private Sub SetTransparentProperties(ByRef SetAsTransparent As Boolean)
    51.         If SetAsTransparent Then
    52.             MyBase.DoubleBuffered = True
    53.             MyBase.EnableHeadersVisualStyles = False
    54.         Else
    55.             MyBase.DoubleBuffered = False
    56.             MyBase.EnableHeadersVisualStyles = True
    57.             MyBase.ColumnHeadersDefaultCellStyle.BackColor = SystemColors.Control
    58.             MyBase.RowHeadersDefaultCellStyle.BackColor = SystemColors.Control
    59.             SetCellStyle(Color.White)
    60.         End If
    61.     End Sub
    62.  
    63.     Protected Overrides Sub PaintBackground(graphics As System.Drawing.Graphics, clipBounds As System.Drawing.Rectangle, gridBounds As System.Drawing.Rectangle)
    64.         MyBase.PaintBackground(graphics, clipBounds, gridBounds)
    65.         Dim rectSource As New Rectangle(MyBase.Location, MyBase.Size)
    66.         Dim rectDest As New Rectangle(0, 0, rectSource.Width, rectSource.Height)
    67.  
    68.         If _DGVHasTransparentBackground Then
    69.             If Not IsNothing(MyBase.Parent.BackgroundImage) Then
    70.                 Dim pb As New PictureBox
    71.                 pb.Width = Parent.ClientRectangle.Width
    72.                 pb.Height = Parent.ClientRectangle.Height
    73.                 pb.BackgroundImageLayout = Parent.BackgroundImageLayout
    74.                 pb.BackgroundImage = Parent.BackgroundImage
    75.  
    76.                 Dim b As New Bitmap(rectSource.Width, rectSource.Height)
    77.                 Dim bp As New Bitmap(Parent.ClientRectangle.Width, Parent.ClientRectangle.Height)
    78.                 pb.DrawToBitmap(bp, New Rectangle(0, 0, bp.Width, bp.Height))
    79.                 graphics.FromImage(b).DrawImage(bp, rectDest, rectSource, GraphicsUnit.Pixel)
    80.                 graphics.DrawImage(b, rectDest)
    81.             Else
    82.                 Dim myBrush As New SolidBrush(MyBase.Parent.BackColor)
    83.                 Dim rectDest1 As New Region(New Rectangle(0, 0, MyBase.Width, MyBase.Height))
    84.                 graphics.FillRegion(myBrush, rectDest1)
    85.             End If
    86.         End If
    87.  
    88.         If Not _DGVBackgroundImage Is Nothing Then
    89.             graphics.DrawImage(_DGVBackgroundImage, rectDest)
    90.         End If
    91.     End Sub
    92.  
    93.     Protected Overrides Sub OnColumnAdded(e As System.Windows.Forms.DataGridViewColumnEventArgs)
    94.         MyBase.OnColumnAdded(e)
    95.         If _DGVHasTransparentBackground Then
    96.             SetCellStyle(Color.White)
    97.         End If
    98.     End Sub
    99.  
    100.     Private Sub SetCellStyle(ByVal cellColour As Color)
    101.         For Each col As DataGridViewColumn In MyBase.Columns
    102.             col.DefaultCellStyle.BackColor = cellColour
    103.             col.DefaultCellStyle.SelectionBackColor = cellColour
    104.         Next
    105.     End Sub
    106.  
    107.     Private Sub myControl_ParentChanged(ByVal sender As Object, ByVal e As EventArgs) Handles Me.ParentChanged
    108.         AddHandler Parent.Resize, AddressOf Parent_Resize
    109.     End Sub
    110.  
    111.     Sub Parent_Resize(ByVal sender As Object, ByVal e As EventArgs)
    112.         Me.Refresh()
    113.     End Sub
    114.  
    115.     Protected Overrides Sub OnScroll(e As System.Windows.Forms.ScrollEventArgs)
    116.         MyBase.OnScroll(e)
    117.         Me.Refresh()
    118.     End Sub
    119. End Class
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: DataGridView With Transparent Background Or Background Image

    Nice stuff. I found a bug though. It disappears in design mode after you run it. To get it back you have to close the designer and re-open it.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  3. #3
    Registered User
    Join Date
    Jul 2013
    Posts
    1

    Re: DataGridView With Transparent Background Or Background Image

    how to use this type of code, because i try this code and it convert into components class, where i insert this code to make my datagridview transparent back color even the cell

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width