2014年3月10日 星期一

vb.net datagridview background image

參考引用來源:
--
 Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports System.Drawing

Namespace testproject
Public Class imagedgv
Inherits DataGridView
Private img As Image
Public Property Img() As Image
Get
Return img
End Get
Set
img = value
End Set
End Property

Public Sub New()
MyBase.New()
SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
End Sub

Protected Overloads Overrides Sub OnSizeChanged(e As EventArgs)
MyBase.OnSizeChanged(e)
img = New Bitmap(img, Me.Width - Me.RowHeadersWidth, Me.Height - Me.ColumnHeadersHeight)
End Sub

Protected Overloads Overrides Sub PaintBackground(graphics As Graphics, clipBounds As Rectangle, gridBounds As Rectangle)
MyBase.PaintBackground(graphics, clipBounds, gridBounds)
graphics.DrawImage(img, New Point(Me.RowHeadersWidth, Me.ColumnHeadersHeight))
End Sub

Protected Overloads Overrides Sub OnCellPainting(e As DataGridViewCellPaintingEventArgs)
MyBase.OnCellPainting(e)
If e.ColumnIndex > -1 AndAlso e.RowIndex > -1 Then
If Me(e.ColumnIndex, e.RowIndex).Style.BackColor <> Color.Red Then
Me(e.ColumnIndex, e.RowIndex).Style.BackColor = Color.Transparent
Else
Me(e.ColumnIndex, e.RowIndex).Style.BackColor = Color.Red
End If
End If
End Sub

Protected Overloads Overrides Sub OnMouseClick(e As MouseEventArgs)
Dim hti As DataGridView.HitTestInfo = Me.HitTest(e.X, e.Y)
If hti.Type = DataGridViewHitTestType.Cell Then
Dim c As DataGridViewCell = Me(hti.ColumnIndex, hti.RowIndex)
If c.Style.BackColor = Color.Red Then
c.Style.BackColor = Color.Transparent
c.Style.SelectionBackColor = Color.Transparent
Else
c.Style.BackColor = Color.Red
c.Style.SelectionBackColor = Color.Red
End If
End If
MyBase.OnMouseClick(e)
End Sub
End Class
End Namespace

沒有留言:

張貼留言