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

来源:互联网 发布:绿色版软件是什么意思 编辑:程序博客网 时间:2024/05/16 05:03

转自: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);            }        }

阅读全文
0 0