listboxのitem色をどう変えるか(C#)
C#
(3)この例から,vc++6.0で自己描画を定義するよりもc#の下でコントロールを再描画する方が便利であることが分かった.
(1) :
ListBox 。 DrawMode DrawMode.OwnerDrawFixed
DrawMode.OwnerDrawVariable , 。 ListBox
。 ( DrawMode DrawMode.OwnerDrawVariable
), , MeasureItem 。 MeasureItem ,
DrawItem 。
, イベントの 。
(2) listbox drawitem
private void listBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
// Set the DrawMode property to draw fixed sized items.
listBox1.DrawMode = DrawMode.OwnerDrawFixed;
// Draw the background of the ListBox control for each item.
e.DrawBackground();
// Define the default color of the brush as black.
Brush myBrush = Brushes.Black;
// Determine the color of the brush to draw each item based on the index of the item to draw.
switch (e.Index)
{
case 0:
myBrush = Brushes.Red;
break;
case 1:
myBrush = Brushes.Orange;
break;
case 2:
myBrush = Brushes.Purple;
break;
}
// Draw the current item text based on the current Font and the custom brush settings.
e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, myBrush,e.Bounds,StringFormat.GenericDefault);
// If the ListBox has focus, draw a focus rectangle around the selected item.
e.DrawFocusRectangle();
}
(3)この例から,vc++6.0で自己描画を定義するよりもc#の下でコントロールを再描画する方が便利であることが分かった.