自定义绘制下拉框

Posted lanwah

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自定义绘制下拉框相关的知识,希望对你有一定的参考价值。

一、OwnerDrawComboBox类实现

    /// <summary>
    /// 自定义绘制下拉框(绘制项,绘制边框(未解决下拉框边框的绘制))
    /// </summary>
    public class OwnerDrawComboBox : ComboBox
    {
        private const int WM_PAINT = 0xF;

        private Color _selectedBackColor = Color.Blue;
        /// <summary>
        /// 边框颜色
        /// </summary>
        [Description("边框颜色"), Category("外观")]
        public Color SelectedBackColor { get => _selectedBackColor; set => _selectedBackColor = value; }

        public OwnerDrawComboBox() : base()
        {
            this.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
        }

        /// <summary>
        /// 自定义绘制下拉框项
        /// </summary>
        /// <param name="e"></param>
        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            this.DrawItemInternal(e);
        }

        private void DrawItemInternal(DrawItemEventArgs e)
        {
            if (e.Index < 0)
            {
                return;
            }

            //// Draw the background of the item.
            //e.DrawBackground();

            // 绘制背景色
            var color = e.BackColor;
            var foreColor = e.ForeColor;
            if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
            {
                color = this.SelectedBackColor;
            }
            e.Graphics.FillRectangle(new SolidBrush(color), e.Bounds);


            // Draw each string in the array, using a different size, color,
            e.Graphics.DrawString(this.GetItemString(this.Items[e.Index]), e.Font, new SolidBrush(foreColor), e.Bounds);

            // Draw the focus rectangle if the mouse hovers over an item.
            e.DrawFocusRectangle();
        }

        /// <summary>
        /// 重绘,绘制边框
        /// </summary>
        /// <param name="m"></param>
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if ((m.Msg == WM_PAINT))
            {
                using (var g = Graphics.FromHwnd(this.Handle))
                {
                    //using (var pen = new Pen(this.BorderColor, 2))
                    //{
                    //    //g.DrawRectangle(pen, this.ClientRectangle);
                    //}

                    System.Windows.Forms.ControlPaint.DrawBorder(g, this.ClientRectangle, this.SelectedBackColor, ButtonBorderStyle.Solid);
                }
            }
        }

        private string GetItemString(object item)
        {
            if (!this.DisplayMember.IsNullOrEmpty())
            {
                var value = item.GetObjectValue(this.DisplayMember);
                if (null != value)
                {
                    return $"{value}";
                }
            }

            return $"{item}";
        }
    }

二、扩展方法ExtendUnity类相关实现

    public static class ExtendUnity
    {
        /// <summary>
        /// 字符串判空
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsNullOrEmpty(this string value)
        {
            if (string.IsNullOrEmpty(value) || string.IsNullOrWhiteSpace(value))
            {
                return true;
            }

            return false;
        }
        /// <summary>
        /// object类型通过反射加载字段值
        /// </summary>
        /// <param name="obj">对象</param>
        /// <param name="fieldName">属性名称</param>
        /// <returns></returns>
        public static object GetObjectValue(this object obj, string fieldName)
        {
            var value = obj;
            string[] fields = fieldName.Split(\'.\');
            foreach (string field in fields)
            {
                if (value == null)
                {
                    break;
                }

                Type type = value.GetType();
                if (type.GetProperty(field) == null)
                {
                    value = null;
                    break;
                }
                else
                {
                    value = type.GetProperty(field).GetValue(value, null);
                }
            }

            return value;
        }
    }

三、效果图

四、存在问题

自定义绘制下拉框(绘制项,绘制边框(未解决下拉框边框的绘制))

以上是关于自定义绘制下拉框的主要内容,如果未能解决你的问题,请参考以下文章

win32day10-组合框/列表框/滚动条/控件的自绘制

Html页面中select下拉列表框别样用法,一个方法一劳永逸。

jQuery自定义漂亮的下拉框插件8种效果演示

自定义下拉框

jQuery自定义漂亮的下拉框插件8种效果演示

下拉列表框Spinner-采用自定义布局文件作为Spinner样式