• <bdo id='RJe4O'></bdo><ul id='RJe4O'></ul>
    <i id='RJe4O'><tr id='RJe4O'><dt id='RJe4O'><q id='RJe4O'><span id='RJe4O'><b id='RJe4O'><form id='RJe4O'><ins id='RJe4O'></ins><ul id='RJe4O'></ul><sub id='RJe4O'></sub></form><legend id='RJe4O'></legend><bdo id='RJe4O'><pre id='RJe4O'><center id='RJe4O'></center></pre></bdo></b><th id='RJe4O'></th></span></q></dt></tr></i><div id='RJe4O'><tfoot id='RJe4O'></tfoot><dl id='RJe4O'><fieldset id='RJe4O'></fieldset></dl></div>
    1. <tfoot id='RJe4O'></tfoot>

      1. <legend id='RJe4O'><style id='RJe4O'><dir id='RJe4O'><q id='RJe4O'></q></dir></style></legend>

        <small id='RJe4O'></small><noframes id='RJe4O'>

        展开 ListBox 中的选定项高度

        时间:2023-10-07
            <tfoot id='0za6Q'></tfoot>
            <i id='0za6Q'><tr id='0za6Q'><dt id='0za6Q'><q id='0za6Q'><span id='0za6Q'><b id='0za6Q'><form id='0za6Q'><ins id='0za6Q'></ins><ul id='0za6Q'></ul><sub id='0za6Q'></sub></form><legend id='0za6Q'></legend><bdo id='0za6Q'><pre id='0za6Q'><center id='0za6Q'></center></pre></bdo></b><th id='0za6Q'></th></span></q></dt></tr></i><div id='0za6Q'><tfoot id='0za6Q'></tfoot><dl id='0za6Q'><fieldset id='0za6Q'></fieldset></dl></div>

              <small id='0za6Q'></small><noframes id='0za6Q'>

                <bdo id='0za6Q'></bdo><ul id='0za6Q'></ul>

              • <legend id='0za6Q'><style id='0za6Q'><dir id='0za6Q'><q id='0za6Q'></q></dir></style></legend>
                    <tbody id='0za6Q'></tbody>

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

                  问题描述

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

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

                  推荐答案

                  当 DrawMode 为 OwnerDrawFixed 时,您的 OnMeasureItem 没有执行任何操作.将模式更改为 OwnerDrawVariable.

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

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

                  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 中的选定项高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:ListBox 和 ListView 突出显示 .NET 4.0 下一篇:获取 ListBoxItem 的索引 - WPF

                  相关文章

                • <i id='RbR2X'><tr id='RbR2X'><dt id='RbR2X'><q id='RbR2X'><span id='RbR2X'><b id='RbR2X'><form id='RbR2X'><ins id='RbR2X'></ins><ul id='RbR2X'></ul><sub id='RbR2X'></sub></form><legend id='RbR2X'></legend><bdo id='RbR2X'><pre id='RbR2X'><center id='RbR2X'></center></pre></bdo></b><th id='RbR2X'></th></span></q></dt></tr></i><div id='RbR2X'><tfoot id='RbR2X'></tfoot><dl id='RbR2X'><fieldset id='RbR2X'></fieldset></dl></div>
                • <legend id='RbR2X'><style id='RbR2X'><dir id='RbR2X'><q id='RbR2X'></q></dir></style></legend>

                  <small id='RbR2X'></small><noframes id='RbR2X'>

                    <bdo id='RbR2X'></bdo><ul id='RbR2X'></ul>
                • <tfoot id='RbR2X'></tfoot>