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 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| 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 = False
.FullRowSelect = True
.CheckBoxes = True
.View = View.Details
.Scrollable = True
.MultiSelect = True
.BackColor = Color.Silver
.ForeColor = Color.Black
.OwnerDraw = True
.Cursor = Cursors.Hand
.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 5
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
If Not (e.Item.Selected AndAlso Not lvEditMode) Then
If (e.ItemIndex Mod 2) <> 0 Then
e.Graphics.FillRectangle(Brushes.LightGray, e.Bounds)
Else
e.Graphics.FillRectangle(Brushes.Silver, e.Bounds)
End If
Else
If (e.ItemState And ListViewItemStates.Selected) <> 0 Then
Using Brush As New SolidBrush(Color.YellowGreen)
e.Graphics.FillRectangle(Brush, New Rectangle(e.Bounds.X + 43, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
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) / 8
Dim pt As New Point(e.Bounds.X + checkPad, e.Bounds.Y - checkPad)
Dim rect As New Rectangle(pt, New Size(20, 20))
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
If state = CheckBoxState.UncheckedNormal Then
FillRectangle(e.Graphics, New SolidBrush(Color.Red), rect, rect.Height - 15)
e.Graphics.FillEllipse(New SolidBrush(Color.White), New Rectangle(rect.Left + 1, rect.Y + 4, rect.Width - 5, rect.Height - 5))
e.Graphics.DrawString("OFF", New Font("Arial", 6, FontStyle.Bold), New SolidBrush(Color.White), New Rectangle(rect.Right - 2, rect.Y + 2, rect.Width, rect.Height), New StringFormat() With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center})
ElseIf state = CheckBoxState.CheckedNormal Then
FillRectangle(e.Graphics, New SolidBrush(Color.Green), rect, rect.Height - 15)
e.Graphics.FillEllipse(New SolidBrush(Color.White), New Rectangle(rect.Right + 3, rect.Y + 4, rect.Width - 5, rect.Height - 5))
e.Graphics.DrawString("ON", New Font("Arial", 6, FontStyle.Bold), New SolidBrush(Color.White), New Rectangle(rect.Left + 2, rect.Y + 3, rect.Width, rect.Height), New StringFormat() With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center})
End If
Dim width As Integer = TextRenderer.MeasureText(Space(rect.Width / 3), e.SubItem.Font).Width
TextRenderer.DrawText(e.Graphics, e.SubItem.Text, e.SubItem.Font, Rectangle.Inflate(e.Bounds, -width, 0), e.Item.ForeColor, TextFormatFlags.LeftAndRightPadding)
Else
TextRenderer.DrawText(e.Graphics, e.SubItem.Text, e.SubItem.Font, e.Bounds, e.Item.ForeColor, TextFormatFlags.HorizontalCenter)
End If
End Sub
Private Sub FillRectangle(g As Graphics, mycolor As SolidBrush, rect As Rectangle, radius As Integer)
Dim area As New Rectangle(rect.X, rect.Y + 2, rect.Width + 20, rect.Height)
Dim path As New Drawing2D.GraphicsPath
path.AddArc(area.Left, area.Top, radius * 2, radius * 2, 180, 90)
path.AddArc(area.Right - (radius * 2), area.Top, radius * 2, radius * 2, 270, 90)
path.AddArc(area.Right - (radius * 2), area.Bottom - (radius * 2), radius * 2, radius * 2, 0, 90)
path.AddArc(area.Left, area.Bottom - (radius * 2), radius * 2, radius * 2, 90, 90)
g.FillPath(mycolor, path)
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
Private Sub ListView1_MouseClick(sender As Object, e As MouseEventArgs) Handles ListView1.MouseClick
Dim lvi As ListViewItem = ListView1.GetItemAt(e.X, e.Y)
If e.X > 20 AndAlso e.Y > 10 Then lvi.Checked = Not lvi.Checked
MyBase.OnMouseClick(e)
End Sub
End Class |
Partager