OwnerDrawVariable组合框下拉空白 [英] OwnerDrawVariable Combobox DropDown Blank Space

查看:690
本文介绍了OwnerDrawVariable组合框下拉空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了以下自定义组合框来增加项目的高度。一旦完成,当有一个滚动条时,在下拉菜单的末尾出现一个空白。

如何解决问题?

  class MyComboBoxXX:ComboBox 
{
public MyComboBoxXX():base()
{
.DrawMode = DrawMode.OwnerDrawVariable;
this.DropDownStyle = ComboBoxStyle.DropDownList;
this.MaxDropDownItems = 5;
this.IntegralHeight = false;
}

protected override void OnMeasureItem(MeasureItemEventArgs e)
{
e.ItemHeight = 40;
this.DropDownHeight = 40 * 5;
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
e.DrawBackground();
var index = e.Index;
if(index< 0 || index> = Items.Count)return;
使用(var brush = new SolidBrush(e.ForeColor))
{
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();
}
}


解决方案

你仔细观察,看起来DropDown区域在顶部有一个1像素的边界,也在底部。您可以通过向 DropDownHeight 添加2个像素来消除空格。

  protected override void OnMeasureItem(MeasureItemEventArgs e)
{
e.ItemHeight = 40;
this.DropDownHeight =(40 * 5)+ 2; // add 2 pixels to include the border
}

>
结果:


I have developed the following custom combo box to increase the height of the items. Once that is done, a blank space appears at the end of the drop down menu when there is a scrollbar. How can I correct the issue?

class MyComboBoxXX : ComboBox
{
    public MyComboBoxXX():base()
    {
        this.DrawMode = DrawMode.OwnerDrawVariable;
        this.DropDownStyle = ComboBoxStyle.DropDownList;
        this.MaxDropDownItems = 5;
        this.IntegralHeight = false;         
    }

    protected override void OnMeasureItem(MeasureItemEventArgs e)
    {
        e.ItemHeight = 40;
        this.DropDownHeight = 40 * 5;        
    }
    protected override void OnDrawItem(DrawItemEventArgs e)
    {        
        e.DrawBackground();        
        var index = e.Index;    
        if (index < 0 || index >= Items.Count) return;    
        using (var brush = new SolidBrush(e.ForeColor))
        {            
            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();
    }    
}

解决方案

If you look closely, it appears that the DropDown area has a 1 pixel border at the top, and also at the bottom. You can get rid of the space by adding 2 pixels to the DropDownHeight.

protected override void OnMeasureItem(MeasureItemEventArgs e)
{
    e.ItemHeight = 40;
    this.DropDownHeight = (40 * 5) + 2; //add 2 pixels to include the border
}



Result:

这篇关于OwnerDrawVariable组合框下拉空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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