【问题标题】:Expanding Selected Item height in ListBox展开 ListBox 中的选定项高度
【发布时间】:2011-10-27 16:52:37
【问题描述】:

有没有办法让 SelectedItem 的高度大于 ListBox 中的其他项目?这是我现在拥有的,但它只是作为一个普通的列表框:

public class BuddyListBox : ListBox
{

    public BuddyListBox() 
    {
        this.ResizeRedraw = true;
        this.DoubleBuffered = true;
        this.BorderStyle = BorderStyle.None;
        this.Dock = DockStyle.Fill;
        this.DrawMode = DrawMode.OwnerDrawVariable;
        this.ItemHeight = 16;
    }
    protected override void OnDrawItem(DrawItemEventArgs e)
    {
        if (e.Index == -1 || e.Index >= this.Items.Count)
            return;

        Buddy current = (Buddy)this.Items[e.Index];
        //Bitmap icon = current.StatusImage;

        //e.Graphics.DrawImage(icon, e.Bounds.Left, e.Bounds.Top, 16, 16);
        e.DrawBackground();
        e.Graphics.DrawString(current.Address, e.Font, new SolidBrush(current.Status != BuddyStatus.offline ? e.ForeColor : Color.DarkGray), 16 + e.Bounds.Left, e.Bounds.Top);
        e.Graphics.DrawString(current.Status.ToString(), e.Font, new SolidBrush(Color.LightGray), e.Bounds.Right - (int)(e.Graphics.MeasureString(current.Status.ToString(), e.Font).Width) - this.Margin.Right, e.Bounds.Top);
        e.DrawFocusRectangle();
    }

    protected override void OnSelectedIndexChanged(EventArgs e)
    {
        this.Refresh();
    }

    protected override void OnMeasureItem(MeasureItemEventArgs e)
    {
        if (e.Index == this.SelectedIndex)
            e.ItemHeight = this.ItemHeight * 2;
        else
            e.ItemHeight = this.ItemHeight;
    }
}

【问题讨论】:

  • this.DrawMode = DrawMode.OwnerDrawFixed;
  • 我已经尝试过 OwnerDrawFixed 和 OwnerDrawVariable。

标签: c# winforms listbox


【解决方案1】:

当 DrawMode 为 OwnerDrawFixed 时,您的 OnMeasureItem 没有做任何事情。将模式更改为 OwnerDrawVariable。

不幸的是,MeasureItem 事件仅在创建句柄时发生,所以这里有一个解决方法:

public class BuddyListBox  : ListBox
{
  int thisIndex = -1;

  public BuddyListBox()
  {
    this.DrawMode = DrawMode.OwnerDrawVariable;
  }

  protected override void OnDrawItem(DrawItemEventArgs e)
  {
    if (this.Items.Count > 0)
    {
      if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
        e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds);
      else
        e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds);
      e.Graphics.DrawString(this.Items[e.Index].ToString(), e.Font, Brushes.Black, e.Bounds.Left, e.Bounds.Top);
      base.OnDrawItem(e);
    }
  }

  protected override void OnSelectedIndexChanged(EventArgs e)
  {
    base.OnSelectedIndexChanged(e);
    thisIndex = this.SelectedIndex;
    this.RecreateHandle();
  }

  protected override void OnMeasureItem(MeasureItemEventArgs e)
  {
    if (e.Index > -1)
    {
      if (e.Index == thisIndex)
        e.ItemHeight = 32;
      else
        e.ItemHeight = 16;
    }
    base.OnMeasureItem(e);
  }
}

【讨论】:

  • @Kevin 用一个工作示例更新了答案。 MeasureItem 事件仅在创建控件句柄时发生,因此您必须自己调用 RecreateHandle。但这也暂时将 SelectedIndex 设置为 -1,因此您也需要一个变量来存储 SelectedIndex 值。
  • 我不确定重新创建Handle 是否比我的解决方案更昂贵,将项目高度设置为Windows API,因为本机ListBox 有这个选项,它只是没有包裹在.Net中。如果使用Windows API调用没有问题,那么take a read
猜你喜欢
  • 2012-05-18
  • 1970-01-01
  • 2016-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多