组合框所有者DrawFixed-滚动非常快

本文关键字:非常 滚动 DrawFixed- 所有者 组合 | 更新日期: 2023-09-27 18:00:47

我已经编写了以下代码来增加项目的高度。完成后,在滚动组合框时,与正常的组合框相比,它的移动速度非常快。如何解决此问题?

我已经将绘制模式设置为DrawMode.OwnerDrawFixed;

 private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
    {           
        e.DrawBackground();
        System.Diagnostics.Debug.WriteLine(e.State);
        if (e.Index < 0)
        {
            return;
        }       
        SizeF stringMeasure = e.Graphics.MeasureString(this.Items[e.Index].ToString(), e.Font);
        Rectangle rec = new Rectangle(e.Bounds.Left, e.Bounds.Top + ((e.Bounds.Height - ItemHeight)/2 ), e.Bounds.Width, ItemHeight);
        e.Graphics.DrawString(this.Items[e.Index].ToString(), e.Font, new SolidBrush(this.ForeColor), rec);
        e.DrawFocusRectangle();
    }
    private void comboBox1_MeasureItem(object sender, MeasureItemEventArgs e)
    {
        e.ItemHeight = this.ItemHeight * 2; ;
    }

组合框所有者DrawFixed-滚动非常快

只有在为OwnerDrawVariable设置DrawMode时,才会调用MeasureItem事件。

因此,您需要将DrawMode更改为OwnerDrawVariable,或者将ComboBox的ItemHeight属性设置为this.ItemHeight * 2 的值

不确定滚动速度,那可能只是你的操作系统。我没有注意到太大的区别——您没有使用stringMeasure变量,所以您可以对此进行注释。