2012-06-25 61 views
0

我用下面的代碼,taken from here,創造datagridview的我自己的自定義列,所以,我現在可以在同一個小區的圖片和文字:將邊緣定製datagridview的列

Public Class TextAndImageColumn 
    Inherits DataGridViewTextBoxColumn 
    Private imageValue As Image 
    Private m_imageSize As Size 
    Public Sub New() 
     Me.CellTemplate = New TextAndImageCell 
    End Sub 
    Public Overloads Overrides Function Clone() As Object 
     Dim c As TextAndImageColumn = TryCast(MyBase.Clone, TextAndImageColumn) 
     c.imageValue = Me.imageValue 
     c.m_imageSize = Me.m_imageSize 
     Return c 
    End Function 
    Public Property Image() As Image 
     Get 
      Return Me.imageValue 
     End Get 
     Set(ByVal value As Image) 
      Me.imageValue = value 
      Me.m_imageSize = value.Size 
      Dim inheritedPadding As Padding = Me.DefaultCellStyle.Padding 
      Me.DefaultCellStyle.Padding = New Padding(ImageSize.Width, inheritedPadding.Top, inheritedPadding.Right, inheritedPadding.Bottom) 
     End Set 
    End Property 
    Private ReadOnly Property TextAndImageCellTemplate() As TextAndImageCell 
     Get 
      Return TryCast(Me.CellTemplate, TextAndImageCell) 
     End Get 
    End Property 
    Friend ReadOnly Property ImageSize() As Size 
     Get 
      Return m_imageSize 
     End Get 
    End Property 
End Class 
Public Class TextAndImageCell 
    Inherits DataGridViewTextBoxCell 
    Private imageValue As Image 
    Private imageSize As Size 
    Public Overloads Overrides Function Clone() As Object 
     Dim c As TextAndImageCell = TryCast(MyBase.Clone, TextAndImageCell) 
     c.imageValue = Me.imageValue 
     c.imageSize = Me.imageSize 
     Return c 
    End Function 
    Public Property Image() As Image 
     Get 
      If Me.OwningColumn Is Nothing OrElse Me.OwningTextAndImageColumn Is Nothing Then 
       Return imageValue 
      Else 
       If Not (Me.imageValue Is Nothing) Then 
        Return Me.imageValue 
       Else 
        Return Me.OwningTextAndImageColumn.Image 
       End If 
      End If 
     End Get 
     Set(ByVal value As Image) 
      Me.imageValue = value 
      Me.imageSize = value.Size 
      Dim inheritedPadding As Padding = Me.InheritedStyle.Padding 
      Me.Style.Padding = New Padding(imageSize.Width, inheritedPadding.Top, inheritedPadding.Right, inheritedPadding.Bottom) 
     End Set 
    End Property 
    Protected Overloads Overrides Sub Paint(graphics As Graphics, clipBounds As Rectangle, cellBounds As Rectangle, 
              rowIndex As Integer, cellState As DataGridViewElementStates, value As Object, formattedValue As Object, 
              errorText As String, cellStyle As DataGridViewCellStyle, advancedBorderStyle As DataGridViewAdvancedBorderStyle, 
              paintParts As DataGridViewPaintParts) 
     MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts) 
     If Not (Me.Image Is Nothing) Then 
      Dim container As System.Drawing.Drawing2D.GraphicsContainer = graphics.BeginContainer 
      graphics.SetClip(cellBounds) 
      graphics.DrawImageUnscaled(Me.Image, cellBounds.Location) 
      graphics.EndContainer(container) 
     End If 
    End Sub 
    Private ReadOnly Property OwningTextAndImageColumn() As TextAndImageColumn 
     Get 
      Return TryCast(Me.OwningColumn, TextAndImageColumn) 
     End Get 
    End Property 
End Class 

它工作得很好,除了我使用的圖像正好在單元格的邊緣。我想給它一點利潤。我怎樣才能做到這一點?

回答

1

您可以將屬性添加到TextAndImageCell,如:

Private m_imagePadding As New Padding(3) 

Public Property ImagePadding() As Padding 
    Get 
     Return m_imagePadding 
    End Get 
    Set(ByVal value As Padding) 
     m_imagePadding = value 
    End Set 
End Property 

和實施(在Paint),如:

graphics.DrawImageUnscaled(Me.Image, _ 
     New Point(cellBounds.Location.X + m_imagePadding.Left, _ 
        cellBounds.Location.Y + m_imagePadding.Top)) 

還需要在TextAndImageColumn改變:

Me.DefaultCellStyle.Padding = New Padding(ImageSize.Width + _ 
    TextAndImageCellTemplate.ImagePadding.Right, inheritedPadding.Top, _ 
    inheritedPadding.Right, inheritedPadding.Bottom) 

顯然還有改進的空間(觸發器在觸摸板上重繪改變行,排列行高,文本填充等),但這樣的事情應該工作。

+0

完美。謝謝。 – Urbycoz