小编Vig*_*aji的帖子

组合框文本垂直居中对齐

我在 .net framework 1.1 上创建了自定义组合框,我可以自定义绘制下拉项,但我不能在左中设置或绘制组合框文本,组合框文本总是呈现左上角,但我需要文本应该呈现在左中.

[ToolboxBitmap(typeof(ComboBox))]
public class MenComboBox :ComboBox
{
    private Image _image = Image.FromFile("Expand.png");
    public MenComboBox()
    {
        this.DrawMode = DrawMode.OwnerDrawFixed;
        this.BackColor = Color.White;
        this.ItemHeight = 18;
        this.Font = new Font("Arial",12f,FontStyle.Regular);
    }       
    protected override void OnDrawItem(DrawItemEventArgs e)
    {

            if (!DesignMode)
            {
                if (e.Index > -1)
                {
                    int textHeight = (int)e.Graphics.MeasureString(this.Items[e.Index].ToString(), e.Font).Height;
                    Point textPos = new Point(e.Bounds.X + 4, e.Bounds.Y + ((this.ItemHeight - textHeight) / 2));
                    if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
                    {
                        e.Graphics.FillRectangle(Brushes.Blue, e.Bounds);
                        e.Graphics.DrawString(this.Items[e.Index].ToString(),e.Font,Brushes.White,textPos);
                    }
                    else
                    {
                        e.Graphics.FillRectangle(Brushes.White, …
Run Code Online (Sandbox Code Playgroud)

.net c# .net-1.1 combobox winforms

3
推荐指数
1
解决办法
4428
查看次数

标签 统计

.net ×1

.net-1.1 ×1

c# ×1

combobox ×1

winforms ×1