1. 程式人生 > 其它 >自定義繪製下拉框

自定義繪製下拉框

一、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;
        }
    }

三、效果圖

四、存在問題

自定義繪製下拉框(繪製項,繪製邊框(未解決下拉框邊框的繪製))