Расширение высоты выбранного элемента в ListBox - PullRequest
3 голосов
/ 16 августа 2011

Есть ли способ, которым я могу иметь высоту 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;
    }
}

1 Ответ

6 голосов
/ 16 августа 2011

Ваш OnMeasureItem ничего не делает, пока DrawMode имеет OwnerDrawFixed.Измените режим на 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);
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...