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
| Imports System.Drawing.Drawing2D
Imports System.Windows.Forms.VisualStyles
Public Class Form1
Private lvEditMode As Boolean = False
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ConfigListview()
End Sub
Private Sub ConfigListview()
With ListView1
.GridLines = True
.FullRowSelect = True
.CheckBoxes = True
.View = View.Details
.Scrollable = True
.MultiSelect = False
.BackColor = Color.Silver
.ForeColor = Color.Black
.OwnerDraw = True
.Font = New Font("Microsoft Sans Serif", 12, FontStyle.Regular)
.Columns.Add("Names", 150, HorizontalAlignment.Center)
.Columns.Add("Nombres", 120, HorizontalAlignment.Center)
.Columns.Add("Prix", 120, HorizontalAlignment.Center)
.Columns.Add("Nombres", 120, HorizontalAlignment.Center)
For i As Integer = 0 To 2
Dim item As New ListViewItem("Productions " & i.ToString)
item.SubItems.Add(i.ToString())
item.SubItems.Add((i + 7).ToString())
item.SubItems.Add((i * i).ToString())
.Items.Add(item)
Next
.Columns(0).Width = 200
.Columns(1).Width = 120
.Columns(2).Width = 120
.Columns(3).Width = -2
End With
End Sub
Private Sub ListView1_DrawColumnHeader(sender As Object, e As DrawListViewColumnHeaderEventArgs) Handles ListView1.DrawColumnHeader
e.Graphics.FillRectangle(New SolidBrush(Color.BlueViolet), New Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
e.Graphics.DrawRectangle(New Pen(New SolidBrush(Color.White), 1), New Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height - 1))
e.Graphics.DrawString(e.Header.Text, e.Font, Brushes.White, e.Bounds, New StringFormat With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center})
End Sub
Private Sub ListView1_DrawSubItem(sender As Object, e As DrawListViewSubItemEventArgs) Handles ListView1.DrawSubItem
Dim lstView As ListView = TryCast(sender, ListView)
Dim itemColor As Color = e.Item.ForeColor
If (e.Item.Selected AndAlso Not lvEditMode) Then
If (e.ColumnIndex = 0 OrElse lstView.FullRowSelect) Then
Using Brush As New SolidBrush(Color.YellowGreen)
e.Graphics.FillRectangle(Brush, e.Bounds)
End Using
End If
End If
If (e.ColumnIndex = 0) Then
Dim state As CheckBoxState = If(e.Item.Checked, CheckBoxState.CheckedNormal, CheckBoxState.UncheckedNormal)
Dim glyphSize As Size = CheckBoxRenderer.GetGlyphSize(e.Graphics, state)
Dim checkPad As Integer = (e.Bounds.Height - glyphSize.Height) / 2
Dim pt As New Point(e.Bounds.X + 2, e.Bounds.Y + 2)
Dim rect As New Rectangle(pt, New Size(18, 18))
If state = CheckBoxState.UncheckedNormal Then
e.Graphics.FillRectangle(New SolidBrush(Color.Red), rect)
e.Graphics.DrawString("X", New Font("Verdana", 10, FontStyle.Bold), New SolidBrush(Color.White), New Rectangle(e.Bounds.X + 4, e.Bounds.Y + 3, e.Bounds.Width, e.Bounds.Height))
ElseIf state = CheckBoxState.CheckedNormal Then
e.Graphics.FillRectangle(New SolidBrush(Color.Green), rect)
rect.Inflate(-4, -4)
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
e.Graphics.DrawLines(New Pen(New SolidBrush(Color.White), 2), New Point() {New Point(rect.Left, rect.Bottom - rect.Right / 2), New Point(rect.Left + rect.Width / 3, rect.Bottom - 2), New Point(rect.Right, rect.Top)})
e.Graphics.SmoothingMode = SmoothingMode.Default
rect.Inflate(+4, +4)
End If
Dim width As Integer = TextRenderer.MeasureText(Space(2), e.SubItem.Font).Width
TextRenderer.DrawText(e.Graphics, e.SubItem.Text, e.SubItem.Font, Rectangle.Inflate(e.Bounds, -width, 0), itemColor, TextFormatFlags.LeftAndRightPadding)
Else
TextRenderer.DrawText(e.Graphics, e.SubItem.Text, e.SubItem.Font, e.Bounds, itemColor, TextFormatFlags.HorizontalCenter)
End If
End Sub
Private Sub ListView1_AfterLabelEdit(sender As Object, e As LabelEditEventArgs) Handles ListView1.AfterLabelEdit
lvEditMode = False
End Sub
Private Sub ListView1_BeforeLabelEdit(sender As Object, e As LabelEditEventArgs) Handles ListView1.BeforeLabelEdit
lvEditMode = True
End Sub
End Class |
Partager