1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
Public Class frmDGVPaint
Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim l As TestStrings = New TestStrings()
Dim bds As BindingSource = New BindingSource()
bds.DataSource = l
Me.dgv.DataSource = bds
End Sub
Private Sub DataGridView1_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles dgv.CellPainting
If Me.dgv.Columns("Title").Index = e.ColumnIndex AndAlso e.RowIndex >= 0 Then
If e.Value IsNot Nothing Then
' Check for the string in the cell.
Dim s As String = CType(e.Value, String)
Dim fnt As Font = e.CellStyle.Font
'bounds du cell
Dim layoutRect As RectangleF = New Rectangle(e.CellBounds.X + 1, e.CellBounds.Y + 1, _
e.CellBounds.Width - 4, e.CellBounds.Height - 4)
' Set character ranges
Dim characterRanges As CharacterRange() = _
{New CharacterRange(0, 4), New CharacterRange(5, 4)}
' Set string format.
Dim fmt As New StringFormat(StringFormat.GenericDefault)
fmt.SetMeasurableCharacterRanges(characterRanges)
' Measure two ranges in string (obtient les regions de decoupage des sous-chaines).
Dim strRegions() As Region = e.Graphics.MeasureCharacterRanges(s, _
fnt, layoutRect, fmt)
Dim g As Graphics = e.Graphics
'clipper les limites de dessin
g.Clip = New Region(e.CellBounds)
' Redssine toute la cellule(hard way de l'api).
g.FillRectangle(Brushes.White, e.CellBounds)
' je t'epargne ces lignes(voir exemple msdn)
' Draw the grid lines (only the right and bottom lines;
' DataGridView takes care of the others).
' Draw text
g.DrawString(CStr(e.Value), e.CellStyle.Font, _
Brushes.Black, layoutRect.X + 2, layoutRect.Y + 2, _
fmt)
' Draw rectangle pour la 1ere s/chaine .
Dim measureRect1 As RectangleF = _
strRegions(0).GetBounds(e.Graphics)
g.DrawRectangle(New Pen(Brushes.Yellow, 1), _
Rectangle.Round(measureRect1))
' Draw rectangle pour la 2ere s/chaine(la tienne) .
Dim measureRect2 As RectangleF = _
strRegions(1).GetBounds(e.Graphics)
g.DrawRectangle(New Pen(Color.Red, 1), _
Rectangle.Round(measureRect2))
e.Handled = True
End If
End If
End Sub
Private Sub DataGridView1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles dgv.Paint
'Dim Rect1 As Rectangle
'Dim SizePolice As SizeF
'SizePolice = e.Graphics.MeasureString(Me.DataGridViewPIPC.Item(1, 3).Value.ToString.Substring(5, 4), Me.DataGridViewPIPC.Item(1, 3).InheritedStyle.Font)
'Rect1.X = Me.DataGridViewPIPC.GetColumnDisplayRectangle(1, True).X + e.Graphics.MeasureString(Me.DataGridViewPIPC.Item(1, 3).Value.ToString.Substring(0, 5), Me.DataGridViewPIPC.Item(1, 3).InheritedStyle.Font).Width
'Rect1.Y = Me.DataGridViewPIPC.GetRowDisplayRectangle(3, True).Y + 4
'Rect1.Height = SizePolice.Height - 1
'Rect1.Width = SizePolice.Width
'e.Graphics.DrawRectangle(New Pen(Brushes.LightGreen, 2), Rect1)
End Sub
End Class |