实现自绘 ComboBox 源代码 (C#)

 

 Implementing an OwnerDrawn ComboBox

源程序下载

实现自绘 ComboBox 源代码 (C#)_第1张图片

 

Introduction
Hi there, I've been waiting for ages and ages for someone to kindly post the code for an owner drawn ComboBox but alas, to no avail. That's right, I'm your average, lowly leech and I've been stealing code off CodeProject for a long time now. Well looks like this time I actually had to go write something on my own. My guess is it was too simple a problem for most of you to bother with. But please do keep writing articles for relative beginners because most of us have to start with the simple things first. Luckily, writing the code turned out to be childs play with the .NET framework.

 

Ok, lets begin. First thing to do is drop a ComboBox onto a form and change it's DrawMode property to OwnerDrawFixed. This will mean that all the items appearing in the ComboBox will have the same dimensons. If you're going to draw items in the ComboBox that are of variable sizes then you would change the property to OwnerDrawVariable and be forced to deal with a MeasureItem event. I won't be covering that here. Next, create a DataSource for the ComboBox, as well as a corresponding collection of images. In my case I simply used two Arrays The following code should now be easy enough to follow

 

Conclusion
Ok, that's it, my very first CodeProject article, go easy on me. The code started out quite long and tedious but I whittled it down as my understanding of DrawItemEventArgs became a bit clearer. I'm not too sure if it's cool to have the ComboBox default backcolor to be anything other than white. But that seems to be more a matter of taste.  

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author
Senkwe Chanda


Nada
Occupation: Web Developer
Location: South Africa

 

The Code

using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace WindowsApplication7 { public class Form1 : System.Windows.Forms.Form { private System.ComponentModel.Container components = null; private String[] arr; private Image[] imageArr; private System.Windows.Forms.ComboBox comboBox1; public Font myFont; public Form1() { InitializeComponent(); myFont = new System.Drawing.Font("Comic Sans", 11); arr = new String[4]; arr[0] = "Smiley Red Face"; arr[1] = "Smiley Cry"; arr[2] = "Smiley Big Grin"; arr[3] = "Smiley Suss"; this.comboBox1.DataSource = arr; //set the combo's data source to out array imageArr = new Image[4]; /* I stole these images off CP. I specifically chose these images because they are all little 16x16 sized gifs. If you're going to use your own images make sure they are all the same size, otherwise your code won't work as advertised. In that case you will have to set the DrawMode property of your combobox to OwnnerDrawVariable and catch the MeasureItem event*/ imageArr[0] = new Bitmap("C://smiley_redface.gif"); imageArr[1] = new Bitmap("C://smiley_cry.gif"); imageArr[2] = new Bitmap("C://smiley_biggrin.gif"); imageArr[3] = new Bitmap("C://smiley_suss.gif"); } //You have your windows forms designer generated code here [STAThread] static void Main() { Application.Run(new Form1()); } private void comboBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { // Let's highlight the currently selected item like any well // behaved combo box should e.Graphics.FillRectangle(Brushes.Bisque, e.Bounds); e.Graphics.DrawString(arr[e.Index], myFont, Brushes.Blue, new Point(imageArr[e.Index].Width*2,e.Bounds.Y)); e.Graphics.DrawImage(imageArr[e.Index], new Point(e.Bounds.X, e.Bounds.Y)); //is the mouse hovering over a combobox item?? if((e.State & DrawItemState.Focus)==0) { //this code keeps the last item drawn from having a Bisque background. e.Graphics.FillRectangle(Brushes.White, e.Bounds); e.Graphics.DrawString(arr[e.Index], myFont, Brushes.Blue, new Point(imageArr[e.Index].Width*2,e.Bounds.Y)); e.Graphics.DrawImage(imageArr[e.Index], new Point(e.Bounds.X, e.Bounds.Y)); } } } }

Conclusion
Ok, that's it, my very first CodeProject article, go easy on me. The code started out quite long and tedious but I whittled it down as my understanding of DrawItemEventArgs became a bit clearer. I'm not too sure if it's cool to have the ComboBox default backcolor to be anything other than white. But that seems to be more a matter of taste.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

 

About the Author
Senkwe Chanda Nada
Occupation: Web Developer
Location: South Africa   

 

 

--------------------------------------------------------------------------------------

                  绘 ComboBox 的Item(C#)

 

作者:Zealot
为了让程序更具个性,有时候我们需要在ComboBox中绘制图形。效果如图所示。

实现自绘 ComboBox 源代码 (C#)_第2张图片实现自绘 ComboBox 源代码 (C#)_第3张图片实现自绘 ComboBox 源代码 (C#)_第4张图片

源代码:

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Drawing2D; using System.Text; using System.Windows.Forms; partial class Form2 : Form { public Form2() { InitializeComponent(); this.comboBox1.Items.Add("Solid Brush"); this.comboBox1.Items.Add("Horizontal"); this.comboBox1.Items.Add("Min"); this.comboBox1.Items.Add("Vertical"); this.comboBox1.Items.Add("Forward Diagonal"); this.comboBox1.Items.Add("Backward Diagonal"); this.comboBox1.Items.Add("Cross"); this.comboBox1.Items.Add("Large Grid"); this.comboBox1.Items.Add("Max"); this.comboBox1.Items.Add("Diagonal Cross"); this.comboBox1.SelectedIndex = 0; this.comboBox1.DrawMode = DrawMode.OwnerDrawVariable; this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList; this.comboBox1.DropDownWidth = 190; this.comboBox1.DropDownHeight = 300; this.comboBox1.MeasureItem += new MeasureItemEventHandler(comboBox1_MeasureItem); this.comboBox1.DrawItem += new DrawItemEventHandler(comboBox1_DrawItem); } void comboBox1_MeasureItem(object sender, MeasureItemEventArgs e) { string displayText = this.comboBox1.Items[e.Index].ToString(); SizeF stringSize = e.Graphics.MeasureString(displayText, this.Font); e.ItemHeight = (int)stringSize.Height; e.ItemWidth = (int)stringSize.Width; } private String RemoveSpaces(String str1) { int start; int at; int end; String str2 = String.Copy(str1); at = 0; end = str2.Length - 1; start = 0; while ((start <= end) && (at > -1)) { // start+count must be a position within str2. at = str2.IndexOf(" ", start); if (at == -1) break; str2 = str2.Remove(at, 1); start = at + 1; } return str2; } private HatchStyle getHatchStyle(String s) { String str = RemoveSpaces(s); return (HatchStyle)Enum.Parse(typeof(HatchStyle), str, true); } void comboBox1_DrawItem(object sender, DrawItemEventArgs e) { //throw new NotImplementedException(); e.DrawBackground(); Rectangle r = e.Bounds; if (e.Index != -1) { if (e.Index > 0) { Rectangle rd = r; rd.Width = rd.Left + 25; Rectangle rt = r; r.X = rd.Right; string displayText = this.comboBox1.Items[e.Index].ToString(); HatchStyle hs = this.getHatchStyle(displayText); using (HatchBrush b = new HatchBrush(hs,e.ForeColor,e.BackColor)) { e.Graphics.FillRectangle(b,rd); } StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Near; using(SolidBrush sb = new SolidBrush(Color.White)) { if((e.State & DrawItemState.Focus) == 0) { sb.Color = SystemColors.Window; e.Graphics.FillRectangle(sb,r); sb.Color = SystemColors.WindowText; e.Graphics.DrawString(displayText,this.Font,sb,r,sf); } else { sb.Color = SystemColors.Highlight; e.Graphics.FillRectangle(sb,r); sb.Color = SystemColors.HighlightText; e.Graphics.DrawString(displayText,this.Font,sb,r,sf); } } } // if (e.Index > 0) else { using(SolidBrush sb = new SolidBrush(Color.White)) { if((e.State & DrawItemState.Focus) == 0) { sb.Color = SystemColors.Window; e.Graphics.FillRectangle(sb,e.Bounds); string displayText = this.comboBox1.Items[e.Index].ToString(); sb.Color = SystemColors.WindowText; e.Graphics.DrawString(displayText,this.Font,sb,e.Bounds); } else { sb.Color = SystemColors.Highlight; e.Graphics.FillRectangle(sb,e.Bounds); string displayText = this.comboBox1.Items[e.Index].ToString(); sb.Color = SystemColors.HighlightText; e.Graphics.DrawString(displayText,this.Font,sb,e.Bounds); } } } e.DrawFocusRectangle(); } }  

 

  

你可能感兴趣的:(String,object,C#,Arrays,download,Components)