001 002 003 004 005 006 007 008 009 010
011 012 013 014 015 016 017 018 019 020
021 022 023 024 025 026 027 028 029 030
031 032 033 034 035 036 037 038 039 040
041 042 043 044 045 046 047 048 049 050
051 052 053 054 055 056 057 058 059 060
061 062 063 064 065 066 067 068 069 070
071 072 073 074 075 076 077 078 079 080
|
Public Class formMain
Inherits System.Windows.Forms.Form
#Region " Windows フォーム デザイナで生成されたコード "
#End Region
Private Sub formMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
listBoxColor.DrawMode = DrawMode.OwnerDrawVariable
AddHandler listBoxColor.MeasureItem, AddressOf ListBox_MeasureItem
AddHandler listBoxColor.DrawItem, AddressOf ListBox_DrawItem
listBoxColor.Items.Clear()
listBoxColor.BeginUpdate()
listBoxColor.Items.Add(Color.Red)
listBoxColor.Items.Add(Color.Orange)
listBoxColor.Items.Add(Color.Yellow)
listBoxColor.Items.Add(Color.GreenYellow)
listBoxColor.Items.Add(Color.Green)
listBoxColor.Items.Add(Color.Blue)
listBoxColor.Items.Add(Color.Purple)
listBoxColor.Items.Add(Color.ForestGreen)
listBoxColor.Items.Add(Color.MediumSlateBlue)
listBoxColor.Items.Add(Color.SkyBlue)
listBoxColor.Items.Add(Color.Cornsilk)
listBoxColor.EndUpdate()
End Sub
Private Sub ListBox_MeasureItem(ByVal sender As System.Object, ByVal e As Windows.Forms.MeasureItemEventArgs)
e.ItemWidth = 200
e.ItemHeight = 20
End Sub
Private Sub ListBox_DrawItem(ByVal sender As System.Object, ByVal e As Windows.Forms.DrawItemEventArgs)
' 背景を白でクリア
e.Graphics.FillRectangle(New SolidBrush(Color.White), e.Bounds)
Dim listBox As ListBox = CType(sender, ListBox)
Dim col As Color = CType(listBox.Items(e.Index), Color)
Dim b As New SolidBrush(col)
' リストアイテムの色で■を描画
e.Graphics.FillRectangle(b, New Rectangle(e.Bounds.X + 5, e.Bounds.Y + 5, 10, 10))
Dim sizeText As SizeF = e.Graphics.MeasureString(col.ToString, e.Font)
' リストアイテムのテキストを描画
e.Graphics.DrawString(col.ToString, e.Font, b, e.Bounds.X + 25, e.Bounds.Y + (20 - sizeText.Height) / 2)
' 項目が選択されている場合
If e.State And DrawItemState.Selected Then
Dim boundFocus As New Rectangle(e.Bounds.X + 1, e.Bounds.Y + 1, listBox.GetItemRectangle(e.Index).Width - 2, listBox.GetItemRectangle(e.Index).Height - 2)
Dim colFocus As Color = Color.FromArgb(&H20, col.R, col.G, col.B) ' αチャンネルを使用
Dim bFocus As New SolidBrush(colFocus)
Dim p As New Pen(col)
' ハイライト表示
e.Graphics.FillRectangle(bFocus, boundFocus)
e.Graphics.DrawRectangle(p, boundFocus)
bFocus.Dispose()
p.Dispose()
End If
b.Dispose()
End Sub
End Class
|