⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datagridimagecell.vb

📁 vb.net+SQL开发工具 是一个很好的管理系统的源码
💻 VB
字号:

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 'New
    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 'Edit
    Public Enum GridImageCellDrawOption
        FitToCell = 0
        NoResize = 1
        FitProportionally = 2
    End Enum 'GridImageCellDrawOption
End Class 'DataGridImageCell

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -