Visual Basic 2005¡ª¡ªÈçºÎÔÚDataGridView¿Ø¼þµÄ´¢´æ¸ñÖÐͬʱÏÔʾ³öÎÄ×ÖÓëͼƬ
2006-09-27 11:43:47
°æÈ¨ÉùÃ÷£ºÔ´´×÷Æ·£¬ÈçÐè×ªÔØ£¬ÇëÓë×÷ÕßÁªÏµ¡£·ñÔò½«×·¾¿·¨ÂÉÔðÈΡ£ |
ÉÏ´ÎÓжÁÕß˵ÎÒдµÃÌ«¼òµ¥£¬ÄÇôÎÒÃǽñÌì¾Íд¸öÄÑÒ»µãµÄ°É£¡ºÜ¶àÓû§Ñ¯ÎÊ£¬ÈçºÎÈçͼ1Ëùʾ£¬ÔÚDataGridView¿Ø¼þµÄ´¢´æ¸ñÖÐͬʱÏÔʾ³öͼƬÓëÎÄ×Ö¡£
![]() ͼ1
DataGridView¿Ø¼þ²¢Ã»ÓÐÄÚ½¨Èκι¦ÄÜÀ´ÈÃÄúÔÚͬһ¸ö´¢´æ¸ñÖÐÏÔʾ³öͼƬÓëÎÄ×Ö¡£½â¾öÖ®µÀ£¬ÊÇͨ¹ýCellPaintµÈʼþÀ´Íê³É×Ô¶©µÄ»æÖÆ×÷Òµ¡£
ÒÔÏÂÎÒÃǽ¨Á¢Ò»¸öÑÜÉú×ÔDataGridViewTextBoxColumnµÄÓû§×Ô¶©Êý¾ÝÐÐÀà±ð£¬½å´ËÓÚ´¢´æ¸ñÄÚµÄÎÄ×ÖÅԱ߻æÖÆÒ»¸öͼƬ¡£ÎÒÃÇʹÓÃDataGridViewCellStyle.PaddingÊôÐÔÀ´µ÷ÕûÎÄ×ÖλÖò¢ÖØÐ´Paint·½·¨ÒÔ±ã»æÖÆÒ»¸öͼƬ£º
Public Class TextAndImageColumn
Inherits DataGridViewTextBoxColumn Private _imageValue As Image Private _imageSize As Size Public Sub New() MyBase.New() Me.CellTemplate = New TextAndImageCell Me.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight End Sub Public Property Image() As Image Get Return Me._imageValue End Get Set(ByVal value As Image) Me._imageValue = value Me._imageSize = value.Size If Me.InheritedStyle IsNot Nothing Then Dim inheritedPadding As Padding = Me.InheritedStyle.Padding Me.DefaultCellStyle.Padding = New Padding( _ ImageSize.Width, inheritedPadding.Top, _
inheritedPadding.Right, inheritedPadding.Bottom)
End If End Set End Property Private ReadOnly Property TextAndImageCellTemplate() As TextAndImageCell Get Return CType(Me.CellTemplate, TextAndImageCell) End Get End Property Friend ReadOnly Property ImageSize() As Size Get Return ImageSize End Get End Property Public Overrides Function Clone() As Object Dim c As TextAndImageColumn = CType(MyBase.Clone, TextAndImageColumn) c._imageValue = Me._imageValue c._imageSize = Me.ImageSize Return c End Function End Class Public Class TextAndImageCell Inherits DataGridViewTextBoxCell Private imageValue As Image Private imageSize As Size Public Property Image() As Image
Get If ((Me.OwningColumn Is Nothing) _ OrElse (Me.OwningTextAndImageColumn Is Nothing)) Then Return imageValue ElseIf (Me.imageValue IsNot Nothing) Then Return Me.imageValue Else Return Me.OwningTextAndImageColumn.Image End If End Get Set(ByVal value As Image) If Not Me.imageValue.Equals(value) Then 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 If End Set End Property Private ReadOnly Property OwningTextAndImageColumn() As TextAndImageColumn Get Return CType(Me.OwningColumn, TextAndImageColumn) End Get End Property Public Overrides Function Clone() As Object Dim c As TextAndImageCell = CType(MyBase.Clone, TextAndImageCell) c.imageValue = Me.imageValue c.imageSize = Me.imageSize Return c End Function Protected Overrides Sub Paint(ByVal graphics As Graphics, _ ByVal clipBounds As Rectangle, ByVal cellBounds As Rectangle, _
ByVal rowIndex As Integer, _
ByVal cellState As DataGridViewElementStates, _
ByVal value As Object, ByVal formattedValue As Object, _
ByVal errorText As String, ByVal cellStyle As DataGridViewCellStyle, _
ByVal advancedBorderStyle As DataGridViewAdvancedBorderStyle, _
ByVal 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 End Class ±¾Îijö×Ô ¡°ÕÂÁ¢Ãñ¡± ²©¿Í£¬×ªÔØÇëÓë×÷ÕßÁªÏµ£¡ ±¾Îijö×Ô 51CTO.COM¼¼Êõ²©¿Í |




ÕÂÁ¢Ãñ
²©¿Íͳ¼ÆÐÅÏ¢
ÈÈÃÅÎÄÕÂ
×îÐÂÆÀÂÛ
ÓÑÇéÁ´½Ó