列表框DRAWITEM HotLight国家中的OwnerDraw模式? [英] ListBox DrawItem HotLight State in the OwnerDraw mode?

查看:229
本文介绍了列表框DRAWITEM HotLight国家中的OwnerDraw模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 OwnerDrawFixed 作为我的WinForms应用程序自定义ListBox控件DrawMode。

I'm using OwnerDrawFixed as a DrawMode for the custom ListBox control in my WinForms app.

我要重新绘制背景(或做一些其他的动作)的一个ListBoxItem当用户将鼠标悬停在列表框中的项目,也就是说,在鼠标移动...

I want to repaint the background (or do some other action) of the ListBoxItem when the user hovers over the listbox item, that is, at the MouseMove...

DrawItemState.HotLight 作品从未为ListBox,所以我不知道如何来模拟它,如何解决这一问题。

DrawItemState.HotLight never works for the ListBox, so i wonder how to emulate it, how to workaround this problem.

推荐答案

我花了仅两年为您找到答案,但在这里它是:

It took me only two years to find the answer for you, but here it is:

DrawItemState.HotLight仅适用于业主绘制菜单,而不是列表框。对于列表框,你要跟踪的项目自己:

The DrawItemState.HotLight only applies to owner drawn menus, not the listbox. For the ListBox, you have to keep track of the item yourself:

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();
    }
  }
}

这篇关于列表框DRAWITEM HotLight国家中的OwnerDraw模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆