扩大ListBox中选定的项目高度 [英] Expanding Selected Item height in ListBox

查看:184
本文介绍了扩大ListBox中选定的项目高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有我可以拥有的SelectedItem的身高比在ListBox中的项目的其余部分大的方法是什么?这就是我现在所拥有的,但它只是作为一个正常的列表框:

 公共类BuddyListBox:列表框
{

公共BuddyListBox()
{
this.ResizeRedraw = TRUE;
this.DoubleBuffered = TRUE;
this.BorderStyle = BorderStyle.None;
this.Dock = DockStyle.Fill;
this.DrawMode = DrawMode.OwnerDrawVariable;
this.ItemHeight = 16;
}
保护覆盖无效的OnDrawItem(DrawItemEventArgs E)
{
如果(e.Index == -1 || e.Index> = this.Items.Count)
返回;

好友电流=(好友)this.Items [e.Index]
//位图的图标= current.StatusImage;

//e.Graphics.DrawImage(icon,e.Bounds.Left,e.Bounds.Top,16,16);
e.DrawBackground();
e.Graphics.DrawString(current.Address,e.Font,新SolidBrush(current.Status = BuddyStatus.offline e.ForeColor!?Color.DarkGray),16 + e.Bounds.Left,e.Bounds 。最佳);
e.Graphics.DrawString(current.Status.ToString(),e.​​Font,新SolidBrush(Color.LightGray),e.Bounds.Right - (INT)(e.Graphics.MeasureString(current.Status。 toString()方法,e.Font).WIDTH) - this.Margin.Right,e.Bounds.Top);
e.DrawFocusRectangle();
}

保护覆盖无效OnSelectedIndexChanged(EventArgs的发送)
{
this.Refresh();
}

保护覆盖无效OnMeasureItem的(MeasureItemEventArgs E)
{
如果(e.Index == this.SelectedIndex)
e.ItemHeight =这.ItemHeight * 2;
,否则
e.ItemHeight = this.ItemHeight;
}
}


解决方案

您而DrawMode被OwnerDrawFixed OnMeasureItem的没有做任何事情。 。改变模式OwnerDrawVariable



不幸的是,手柄被创建的时候MeasureItem事件只发生,所以这里是一个变通:

 公共类BuddyListBox:列表框
{
INT thisIndex = -1;

公共BuddyListBox()
{
this.DrawMode = DrawMode.OwnerDrawVariable;
}

保护覆盖无效的OnDrawItem(DrawItemEventArgs E)
{
如果(this.Items.Count大于0)
{
如果((e.State&安培; DrawItemState.Selected)== DrawItemState.Selected)
e.Graphics.FillRectangle(SystemBrushes.Highlight,e.Bounds);
,否则
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);
}
}

保护覆盖无效OnSelectedIndexChanged(EventArgs的发送)
{
base.OnSelectedIndexChanged(E);
thisIndex = this.SelectedIndex;
this.RecreateHandle();
}

保护覆盖无效OnMeasureItem的(MeasureItemEventArgs E)
{
如果(e.Index -1个)
{
如果(e.Index == thisIndex)
e.ItemHeight = 32;
,否则
e.ItemHeight = 16;
}
base.OnMeasureItem(E);
}
}


Is there a way that I can have the SelectedItem's height larger than the rest of the items in a ListBox? This is what I have right now, yet it just acts as a normal 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;
    }
}

解决方案

Your OnMeasureItem isn't doing anything while the DrawMode is OwnerDrawFixed. Change the mode to OwnerDrawVariable.

Unfortunately, the MeasureItem event only happens when the handle gets created, so here is a work-around:

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

这篇关于扩大ListBox中选定的项目高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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