读书人

[]在DataGrid中添加图片列的代码何解

发布时间: 2012-01-21 21:31:43 作者: rapoo

[求助]在DataGrid中添加图片列的代码何解
在网上找到一段代码,主要功能在DataGrid中添加图片列,但我不知代码的意思,不知那位兄弟姐妹帮忙解释一下,先谢谢了。

Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Data
Imports System.Collections

Public Class DataGridImageCell
Inherits DataGridTextBoxColumn
Private theImages1 As ArrayList
Private _bRect As Boolean
Private _rowNumber As Integer = 0

Property RowNumber() As Integer
Get
Return _rowNumber
End Get
Set(ByVal Value As Integer)
_rowNumber = Value
End Set
End Property

Property bRect() As Boolean
Get
Return _bRect
End Get
Set(ByVal Value As Boolean)
_bRect = Value
End Set
End Property

Public Sub New()
bRect = False
End Sub

Public Property theImages() As ArrayList
Get
Return theImages1
End Get
Set(ByVal Value As ArrayList)
theImages1 = Value
End Set
End Property

Public Sub PaintRect()
Me.Invalidate()
End Sub

Protected Overloads Overrides Sub Paint(ByVal g As System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal [source] As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal backBrush As System.Drawing.Brush, ByVal foreBrush As System.Drawing.Brush, ByVal alignToRight As Boolean)


Dim o As Object = Me.GetColumnValueAtRow([source], rowNum)

If Not (o Is Nothing) Then
Dim i As Integer = Fix(o)
g.FillRectangle(backBrush, bounds)

Dim bmp As Bitmap = CType(theImages(i), Bitmap)
Dim cellDrawOption As GridImageCellDrawOption = GridImageCellDrawOption.FitToCell

Dim gu As System.Drawing.GraphicsUnit = System.Drawing.GraphicsUnit.Point

Dim srcRect As RectangleF = bmp.GetBounds(gu)
Dim destRect As Rectangle = Rectangle.Empty
Dim saveRegion As [Region] = g.Clip

Select Case cellDrawOption
Case GridImageCellDrawOption.FitToCell
destRect = bounds
Case GridImageCellDrawOption.NoResize
destRect = New Rectangle(bounds.X, bounds.Y, Fix(srcRect.Width), Fix(srcRect.Height))
g.Clip = New [Region](bounds)
Case GridImageCellDrawOption.FitProportionally
If (True) Then
Dim srcRatio As Single = srcRect.Width / srcRect.Height
Dim tarRatio As Single = System.Convert.ToSingle(bounds.Width) / bounds.Height
destRect = bounds


If tarRatio < srcRatio Then
destRect.Height = Fix(destRect.Width * srcRatio)
Else
destRect.Width = Fix(destRect.Height * srcRatio)
End If
End If
Case Else
End Select

If Not destRect.IsEmpty Then
Dim destRectF As New RectangleF(destRect.X, destRect.Y, destRect.Width, destRect.Height)
Dim srcRectF As New RectangleF(srcRect.X, srcRect.Y, srcRect.Width, srcRect.Height)

g.DrawImage(bmp, destRectF, srcRectF, gu)

End If
g.Clip = saveRegion
End If
If bRect = True And rowNum = RowNumber Then
g.DrawRectangle(System.Drawing.Pens.Black, bounds)
bRect = False
End If
End Sub 'Paint

Protected Overloads Overrides Sub Edit(ByVal [source] As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal bounds As System.Drawing.Rectangle, ByVal [readOnly] As Boolean, ByVal instantText As String, ByVal cellIsVisible As Boolean)


'overriden to avoid editing
End Sub

Public Enum GridImageCellDrawOption
FitToCell = 0
NoResize = 1
FitProportionally = 2
End Enum 'GridImageCellDrawOption
End Class


[解决办法]
在winform页面上添加调用,在DataGridImageCell内设置DataGrid的图片添加属性,同时可以动态的加入。

读书人网 >VB Dotnet

热点推荐