using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Text; using System.Windows.Forms; using System.Drawing; using System.Drawing.Drawing2D; using ryControls; namespace ryControls { /// /// /// [ToolboxBitmap(typeof(System.Windows.Forms.ComboBox))] public partial class ComboBoxEx : ComboBox { /// /// /// public ComboBoxEx() : base() { base.DoubleBuffered = true; base.FlatStyle = FlatStyle.Flat; DrawMode = DrawMode.OwnerDrawFixed; DropDownStyle = ComboBoxStyle.DropDownList; //ItemHeight = 50; DrawItem += OnDrawItem; } private void OnDrawItem(object sender, DrawItemEventArgs e) { if (e.Index < 0) { return; } //鼠标选中在这个项上 if ((e.State & DrawItemState.Selected) != 0) { //渐变画刷 LinearGradientBrush brush = new LinearGradientBrush(e.Bounds, Color.FromArgb(255, 251, 237), Color.FromArgb(255, 236, 181), LinearGradientMode.Vertical); //填充区域 Rectangle borderRect = new Rectangle(3, e.Bounds.Y, e.Bounds.Width - 5, e.Bounds.Height - 2); e.Graphics.FillRectangle(brush, borderRect); //画边框 Pen pen = new Pen(Color.FromArgb(229, 195, 101)); e.Graphics.DrawRectangle(pen, borderRect); } else { SolidBrush brush = new SolidBrush(Color.FromArgb(255, 255, 255)); e.Graphics.FillRectangle(brush, e.Bounds); } Rectangle textRect = new Rectangle(2, e.Bounds.Y + 1, e.Bounds.Width, e.Bounds.Height - 2); switch (this.Items[e.Index]) { case clsCbbItem item: Image img = item.Img; //图片绘制的区域 Rectangle imgRect = new Rectangle(6, e.Bounds.Y + 3, 16, 16); e.Graphics.SmoothingMode = SmoothingMode.HighQuality; e.Graphics.CompositingQuality = CompositingQuality.HighQuality; e.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor; e.Graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half; e.Graphics.DrawImage(img, imgRect); //文本内容显示区域 textRect = new Rectangle(imgRect.Right + 2, imgRect.Y, e.Bounds.Width - imgRect.Width, e.Bounds.Height - 2); break; } //获得项文本内容,绘制文本 string itemText = this.Items[e.Index].ToString(); //文本格式垂直居中 StringFormat strFormat = new StringFormat() { LineAlignment = StringAlignment.Center }; e.Graphics.DrawString(itemText, new Font("微软雅黑", 11), Brushes.Black, textRect, strFormat); } /// /// /// /// /// /// public void AddItem(string text,string tag,Image ico) { this.Items.Add(new clsCbbItem(text,tag, ico)); } /// /// /// /// /// /// public void AddItem(string text,string tag, string ImagePath) { this.Items.Add(new clsCbbItem(text,tag, Image.FromFile(ImagePath))); } //导入API函数 [System.Runtime.InteropServices.DllImport("user32.dll ")] static extern IntPtr GetWindowDC(IntPtr hWnd);//返回hWnd参数所指定的窗口的设备环境。 [System.Runtime.InteropServices.DllImport("user32.dll ")] static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC); //函数释放设备上下文环境(DC) /// /// /// /// protected override void WndProc(ref Message m) { base.WndProc(ref m); //WM_PAINT = 0xf; 要求一个窗口重画自己,即Paint事件时 //WM_CTLCOLOREDIT = 0x133;当一个编辑型控件将要被绘制时发送此消息给它的父窗口; //通过响应这条消息,所有者窗口可以通过使用给定的相关显示设备的句柄来设置编辑框的文本和背景颜色 //windows消息值表,可参考:http://hi.baidu.com/dooy/blog/item/0e770a24f70e3b2cd407421b.html if (m.Msg == 0xf || m.Msg == 0x133) { IntPtr hDC = GetWindowDC(m.HWnd); if (hDC.ToInt32() == 0) //如果取设备上下文失败则返回 { return; } //建立Graphics对像 Graphics g = Graphics.FromHdc(hDC); //画边框的 ControlPaint.DrawBorder(g, new Rectangle(0, 0, Width, Height), SkinHelp.DefalutBorderColor, ButtonBorderStyle.Solid); //画坚线 //ControlPaint.DrawBorder(g, new Rectangle(Width - Height, 0, Height, Height), Color.Red, ButtonBorderStyle.Solid); //g.DrawLine(new Pen(Brushes.Blue, 2), new PointF(this.Width - this.Height, 0), new PointF(this.Width - this.Height, this.Height)); //释放DC ReleaseDC(m.HWnd, hDC); } } } }