それはあなたのための答えを見つけるためにわずか2年かかりましたが、ここでは、次のとおりです。
DrawItemState.HotLightは所有者のみ描画されたメニューではなく、リストボックスに適用されます。 ListBoxの場合は、自分で項目を追跡する必要があります。
public partial class Form1 : Form
{
private int _MouseIndex = -1;
public Form1()
{ InitializeComponent(); }
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
Brush textBrush = SystemBrushes.WindowText;
if (e.Index > -1)
{
if (e.Index == _MouseIndex)
{
e.Graphics.FillRectangle(SystemBrushes.HotTrack, e.Bounds);
textBrush = SystemBrushes.HighlightText;
}
else
{
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds);
textBrush = SystemBrushes.HighlightText;
}
else
e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds);
}
e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, textBrush, e.Bounds.Left + 2, e.Bounds.Top);
}
}
private void listBox1_MouseMove(object sender, MouseEventArgs e)
{
int index = listBox1.IndexFromPoint(e.Location);
if (index != _MouseIndex)
{
_MouseIndex = index;
listBox1.Invalidate();
}
}
private void listBox1_MouseLeave(object sender, EventArgs e)
{
if (_MouseIndex > -1)
{
_MouseIndex = -1;
listBox1.Invalidate();
}
}
}
さて、2年後にも私を助けました! ;)リストボックスの「ホットトラッキング」のための素敵な、簡単な方法...ありがとう! – ChandlerPelhams
そしてLarsTechは何年も多くの人を助けてくれるでしょう...私は同様の答えを探していて、ここでそれを見つけました。ありがとう。 – ThN
これはどのようにしてコンボボックスに対して行うことができますか? ComboboxにはIndexFromPointはありません。 –