WinForm窗体中ComboBox控件自定义高度和选项文本居中、选项高亮

转自:http://m.blog.csdn.net/wangzl1163/article/details/74932668

WinForm窗体中的ComboBox控件是有TextBox控件和ListBox控件组合而成,其本身是不能自定义高度的只能根据字体的大小而自动生成高度。另外,选中后的选项文本在ComboBox中显示默认是依靠ComboBox的Top边和Left边来定位的而且不能更改。在项目中这样的设定是很不美观的。为了使ComboBox更加美观设计上要求可以自定义控件的高度和选中项文本的垂直居中和水平居中的。

       为了实现这种自定义控件高度和选中项文本对齐样式必须要重绘ComboBox的各个选项。通过控制各个选项的坐标来达到选中项文本的对齐样式。

       具体实现如下。

1、设置ComboBox的DrawMode属性为OwnerDrawVariable或者OwnerDrawFixed。

2、设置ItemHeight属性的值为目标ComboBox高度减去6即ComboBox的高度=ItemHeight+6。

3、可以根据设计要求设置其他的属性,比如DropDownHeight、Size、Cursor、FlatStyle、Font、ForeColor等。

4、核心实现。通过ComboBox的DrawItem事件来绘制各个选项。具体代码如下。

private void ComboBox_DrawItem(object sender, DrawItemEventArgs e)
        {
            ComboBox cmb = sender as ComboBox;  //当前的ComboBox控件
            SolidBrush myBrush = new SolidBrush(cmb.ForeColor);  //字体颜色
            Font ft = cmb.Font;    //获取在属性中设置的字体
            
            //选项的文本
            string itemText = cmb.GetItemText(cmb.Items[e.Index]);//cmb.Items[e.Index].ToString(); 
            // 计算字符串尺寸(以像素为单位)
            SizeF ss = e.Graphics.MeasureString(itemText, cmb.Font);

            // 水平居中
            float left = 0;
            //left = (float)(e.Bounds.Width - ss.Width) / 2;  //如果需要水平居中取消注释
            if (left < 0) left = 0f;

            // 垂直居中
            float top = (float)(e.Bounds.Height - ss.Height) / 2;
            if (top <= 0) top = 0f;

            // 输出
            e.DrawBackground();
            e.Graphics.DrawString(itemText,ft,myBrush,new RectangleF(
                e.Bounds.X+left,    //设置X坐标偏移量
                e.Bounds.Y+top,     //设置Y坐标偏移量
                e.Bounds.Width, e.Bounds.Height), StringFormat.GenericDefault);

            //e.Graphics.DrawString(cmb.GetItemText(cmb.Items[e.Index]), ft, myBrush, e.Bounds, StringFormat.GenericDefault);
            e.DrawFocusRectangle();
        }


5、ComboBox绑定数据。

      绑定数据时把获取到的数据赋值给ComboBox控件的DataSource属性即可同时要指定要展示的列和对应的Value。

 private void FrmFirstView_Load(object sender, EventArgs e)
        {
            SetControlStyle();
            
            passwordProtectedCmb.DataSource = protectedquestions.rs;
            passwordProtectedCmb.DisplayMember = "question";
            passwordProtectedCmb.ValueMember = "id";

            //List<Interface.Model.SysData.sys_dic> list = new metadataService().GetSubjectDic();
            //cbbSubject.DataSource = list;
        }

在上面的基础上实现选项高亮即鼠标放到选项上以及选中选项后背景高亮显示其他颜色,其中包含了不使用背景高亮时填充背景色的方法

private void ComboBox_DrawItem(object sender, DrawItemEventArgs e)
        {
            try
            {
                //匿名方法  
                Func<Color> SetBackColor = () =>
                {
                    if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
                    {
                        return Color.Lime; //高亮背景色  
                    }
                    return e.BackColor;
                };

                ComboBox cmb = sender as ComboBox;  //当前的ComboBox控件
                SolidBrush myBrush = new SolidBrush(cmb.ForeColor);  //字体颜色
                Font ft = cmb.Font;    //获取在属性中设置的字体

                //选项的文本
                string itemText = cmb.GetItemText(cmb.Items[e.Index]);//cmb.Items[e.Index].ToString(); 
                                                                      // 计算字符串尺寸(以像素为单位)
                SizeF ss = e.Graphics.MeasureString(itemText, cmb.Font);

                // 水平居中
                float left = 0;
                //left = (float)(e.Bounds.Width - ss.Width) / 2;  //如果需要水平居中取消注释
                if (left < 0) left = 0f;

                // 垂直居中
                float top = (float)(e.Bounds.Height - ss.Height) / 2;
                if (top <= 0) top = 0f;

                // 输出
                e.DrawBackground();

                //选项背景高亮
                Rectangle rectangle1 = new Rectangle(e.Bounds.Left, e.Bounds.Top,
                    e.Bounds.Width, e.Bounds.Height);
                e.Graphics.FillRectangle(new SolidBrush(SetBackColor()), rectangle1);

                //填充选项背景
                int x = 0, y = 0;
                Rectangle rectangle = new Rectangle(e.Bounds.Left+x, e.Bounds.Top +y,
                    e.Bounds.Width-2*x, e.Bounds.Height - 2*y);
                e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(255,ColorTranslator.FromHtml("#ffffff"))), rectangle);

                e.Graphics.DrawString(itemText, ft, myBrush, new RectangleF(
                    e.Bounds.X + left,    //设置X坐标偏移量
                    e.Bounds.Y + top,     //设置Y坐标偏移量
                    e.Bounds.Width, e.Bounds.Height), StringFormat.GenericDefault);

                //e.Graphics.DrawString(cmb.GetItemText(cmb.Items[e.Index]), ft, myBrush, e.Bounds, StringFormat.GenericDefault);
                e.DrawFocusRectangle();
            }
            catch (Exception ex)
            {
                log.ErrorFormat("FrmAccountActivate窗体:" + ex.Message, ex);
            }
        }

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值