C# 重绘Combobox时,选择下拉框内容后不在Combobox上显示

.Net技术 码拜 4年前 (2017-05-06) 1277次浏览
public partial class ComboBoxEx : ComboBox
    {
        private string imagePath = AppDomain.CurrentDomain.BaseDirectory + "Image\{0}.png";
        private Image _normalImage = null;
        private Image _hoverImage = null;
        private Image _disableImage = null;
        private bool mouseHover;
        private bool mouseDown;
        public ComboBoxEx()
        {
            base.SetStyle(
                ControlStyles.UserPaint |
                ControlStyles.OptimizedDoubleBuffer |
                ControlStyles.AllPaintingInWmPaint |
                ControlStyles.ResizeRedraw |
                ControlStyles.SupportsTransparentBackColor,
                true);
            this.DrawMode = DrawMode.OwnerDrawFixed;
            this.DropDownStyle = ComboBoxStyle.DropDownList;
            base.UpdateStyles();
        }
        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            if (!DesignMode && this.Items.Count != 0)
            {
                this.DropDownHeight = this.Items.Count * 17;
            }
            ResetBitmap();
        }
        private void ResetBitmap()
        {
            this._normalImage = Image.FromFile(string.Format(imagePath,"combobox-normal"));
            this._hoverImage = Image.FromFile(string.Format(imagePath,"combobox-hover"));
            this._disableImage = Image.FromFile(string.Format(imagePath,"combobox-disable"));
        }
       
        protected override void OnMouseEnter(EventArgs e)
        {
            mouseHover = true;
            this.Invalidate();
            base.OnMouseEnter(e);
        }
        protected override void OnMouseLeave(EventArgs e)
        {
            mouseHover = false;
            this.Invalidate();
            base.OnLeave(e);
        }
        protected override void OnMouseDown(MouseEventArgs mevent)
        {
            if (mevent.Button == MouseButtons.Left)
            {
                mouseDown = true;
                this.Invalidate();
                base.OnMouseDown(mevent);
            }
        }
        protected override void OnMouseUp(MouseEventArgs mevent)
        {
            mouseDown = false;
            this.Invalidate();
            base.OnMouseUp(mevent);
        }
        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr GetWindowDC(IntPtr handle);
        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr ReleaseDC(IntPtr handle, IntPtr hDC);
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == 0xf || m.Msg == 0x133)
            {
                IntPtr hDC = GetWindowDC(m.HWnd);
                if (hDC.ToInt32() == 0) return;
                Graphics g = Graphics.FromHdc(hDC);
                OverrideDropDown(g);
                OverrideControlBorder(g);
                ReleaseDC(m.HWnd, hDC);
            }
        }
        private static int DropDownButtonWidth = 17;
        private void OverrideDropDown(Graphics g)
        {
            if (DesignMode) return;
            Rectangle rect = new Rectangle(this.Width - DropDownButtonWidth - 3, 0, DropDownButtonWidth + 3, this.Height);
            g.FillRectangle(new SolidBrush(Color.FromArgb(236, 240, 241)), rect);
            if (!this.Enabled)
            {
                g.DrawImage(this._disableImage, new Rectangle(this.Width - 15, 6, 9, 9));
            }
            else if (mouseDown)
            {
                g.DrawImage(this._hoverImage, new Rectangle(this.Width - 15, 6, 9, 9));
            }
            else if (mouseHover)
            {
                g.DrawImage(this._hoverImage, new Rectangle(this.Width - 15, 6, 9, 9));
            }
            else if (this.Focused)
            {
                g.DrawImage(this._hoverImage, new Rectangle(this.Width - 15, 6, 9, 9));
            }
            else
            {
                g.DrawImage(this._normalImage, new Rectangle(this.Width - 15, 6, 9, 9));
            }
        }
        private void OverrideControlBorder(Graphics g)
        {
            if (!this.Enabled)
            {
                g.DrawRectangle(new Pen(Color.FromArgb(189, 195, 199), 2), new Rectangle(0, 0, this.Width, this.Height));
            }
            else if (mouseDown)
            {
                g.DrawRectangle(new Pen(Color.FromArgb(41, 128, 185), 3), new Rectangle(0, 0, this.Width - 1, this.Height - 1));
            }
            else if (mouseHover)
            {
                g.DrawRectangle(new Pen(Color.FromArgb(41, 128, 185), 2), new Rectangle(0, 0, this.Width, this.Height));
            }
            else if (this.Focused)
            {
                g.DrawRectangle(new Pen(Color.FromArgb(41, 128, 185), 3), new Rectangle(0, 0, this.Width - 1, this.Height - 1));
            }
            else
            {
                g.DrawRectangle(new Pen(Color.FromArgb(127, 140, 141), 2), new Rectangle(0, 0, this.Width, this.Height));
            }
        }
        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            Graphics g = e.Graphics;
            Rectangle r = e.Bounds;
            Font fn = null;
            if (e.Index >= 0)
            {
                fn = e.Font;
                string s = this.Items[e.Index].ToString();
                StringFormat sf = new StringFormat();
                sf.Alignment = StringAlignment.Near;
                if (e.State == (DrawItemState.NoAccelerator | DrawItemState.NoFocusRect))
                {
                    e.Graphics.FillRectangle(new SolidBrush(Color.White), r);
                    e.Graphics.DrawString(s, fn, new SolidBrush(Color.FromArgb(127, 140, 141)), r, sf);
                    e.DrawFocusRectangle();
                }
                else
                {
                    e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(41, 128, 185)), r);
                    e.Graphics.DrawString(s, fn, new SolidBrush(Color.FromArgb(255, 255, 255)), r, sf);
                    e.DrawFocusRectangle();
                }
                if (!this.Focused)
                {
                    e.Graphics.FillRectangle(new SolidBrush(Color.White), r);
                    e.Graphics.DrawString(s, fn, new SolidBrush(Color.Black), r, sf);
                    e.DrawFocusRectangle();
                }
            }
            base.OnDrawItem(e);
        }
    }

选择时可以看到下拉框内容,点击内容后不在combobox上显示

解决方案

20

来顶下,看有没有有高手回复

10

把 ControlStyles.UserPaint 去掉就好了

10

本人的理解是假如你加上了ControlStyles.UserPaint ,
你还得 在protected override void OnPaint(PaintEventArgs e) 方法里绘制选择以后的内容

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明C# 重绘Combobox时,选择下拉框内容后不在Combobox上显示
喜欢 (0)
[1034331897@qq.com]
分享 (0)