Winform重画ComboBox背景色

//返回hWnd参数所指定的窗口的设备环境。
 [System.Runtime.InteropServices.DllImport("user32.dll")]
        static extern IntPtr GetWindowDC(IntPtr hWnd);     

        [System.Runtime.InteropServices.DllImport("user32.dll")]
        //函数释放设备上下文环境(DC)  
        static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);    
        int WM_PAINT = 0xf; //要求一个窗口重画自己,即Paint事件时         


        /// <summary>
        /// 
        /// </summary>
        /// <param name="m"></param>
        protected override void WndProc(ref Message m)
        { 

            base.WndProc(ref m);
            if (m.Msg == WM_PAINT)
            {
                IntPtr hDC = GetWindowDC(m.HWnd);
                if (hDC.ToInt32() == 0) //如果取设备上下文失败则返回     
                {
                    return;
                }

                PaintComboBox(Graphics.FromHdc(hDC));
                ReleaseDC(m.HWnd, hDC);
            }
        }

 

private void PaintComboBox(Graphics g)
        {
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;


            int iDropDownButtonWidth = 17;
            int iDropDownButtonHeight = this.Height;
            int iDropDownButtonLocatinX = 17;
            int iDropDownButtonLocatinY = 0;
            
            if (!Util.PublicFunction.IsHigherWinXP())
            {
                iDropDownButtonWidth = 17;
                iDropDownButtonHeight = this.Height-2;
                iDropDownButtonLocatinX = 19;
                iDropDownButtonLocatinY = 1;
            }
            //下拉按钮
            Rectangle dropDownRectangle = new Rectangle(ClientRectangle.Width - iDropDownButtonLocatinX, iDropDownButtonLocatinY, iDropDownButtonWidth, iDropDownButtonHeight);


            //背景色刷
            Brush bkgBrush;

            //字体色刷
            Brush fcBrush;

            //设置背景色和字体色
            bkgBrush = new SolidBrush(this._backColor);
            fcBrush = new SolidBrush(this._foreColor);

            //画3D边框
            //ControlPaint.DrawBorder3D(g, new Rectangle(0, 0, this.Width, this.Height), Border3DStyle.SunkenInner, Border3DSide.All);


            int iBackColorX = 0;
            //为了字体正常,Enable时只是重画按钮区域
            if (this.Enabled)
            {
                iBackColorX = this.Width - 20;
            }
            //画背景
            g.FillRectangle(bkgBrush, iBackColorX, 0, ClientRectangle.Width, ClientRectangle.Height);

            //为了字体正常,Disable时才重画文本
            if (!this.Enabled)
            {
                //画文本
                g.DrawString(base.Text, this.Font, fcBrush, 2, this.ClientSize.Height / 2, new StringFormat() { LineAlignment = StringAlignment.Center });
            }

            //画边框
            //g.DrawRectangle(_BorderPen, new Rectangle(0, 0, this.Width, this.Height));
            ControlPaint.DrawBorder(g, new Rectangle(0, 0, this.Width, this.Height), borderColor, ButtonBorderStyle.Solid);

            //画下拉按钮
            if (Util.PublicFunction.IsHigherWinXP())
            {
                ControlPaint.DrawComboButton(g, dropDownRectangle, this.Enabled ? System.Windows.Forms.ButtonState.Flat : System.Windows.Forms.ButtonState.All);
            }
            else
            {
                ComboBoxRenderer.DrawDropDownButton(g, dropDownRectangle, this.Enabled ? ComboBoxState.Normal : ComboBoxState.Disabled);
            }

            g.Dispose();
            bkgBrush.Dispose();
            fcBrush.Dispose();

        }

 

这篇文章中我们重点需要实现的是(3)、(4)两项功能,下面我们来介绍具体实现的方法。 第一步,实现ImageComboBoxItem类。 要实现显示图标,当然要给每个项添加与图标相关的信息了,ImageComboBoxItem类应该包括以下内容:文本(Text)、缩进的级别(Level)、图标的索引(ImageIndex、ImageKey),用户数据(Tag)。ImageComboBoxItem类实现了ISerializable接口,实现自定义序列化。ImageComboBoxItem类的类视图如下: 图3 ImageComboxItem类视图 ImageComboBoxItem类的代码如下: [Serializable] [DefaultProperty("Text")] [TypeConverter( typeof(ExpandableObjectConverter))] public class ImageComboBoxItem : IDisposable, ISerializable ...{ Fields#region Fields private ImageComboBox _imageComboBox; private string _text = "ImageComboBoxItem"; private ImageComboBoxItemImageIndexer _imageIndexer; private object _tag; private int _level; #endregion Constructors#region Constructors public ImageComboBoxItem() ...{ } public ImageComboBoxItem(string text) : this(text, -1, 0) ...{ } public ImageComboBoxItem( string text, int imageIndex) : this(text, imageIndex, 0) ...{ } public ImageComboBoxItem( string text, string imageKey) : this(text, imageKey, 0) ...{ } public ImageComboBoxItem( string text, int imageIndex, int level) : this() ...{ _text = text; ImageIndexer.Index = imageIndex; _level = level; } public ImageComboBoxItem( string text, string imageKey, int level) : this() ...{ _text = text; ImageIndexer.Key = imageKey; _level = level; } protected ImageComboBoxItem( SerializationInfo info, StreamingContext context) : this() ...{ Deserialize(info, context); } #endregion Properties#region Properties [Localizable(true)]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值