Color ComboBox

来源:互联网 发布:网页美工课程 编辑:程序博客网 时间:2024/06/14 14:47
   前不久,由于公司没有任务,无聊之于写个小空间,学习一下GDI+技术,此控件是一个ComboBox,是一个选择颜色的ComboBox。功能不是很强大,仅仅业务制作。代码如下:
    /// <summary>
    /// Color ComboBox
    /// </summary>
    public class ColorComboBox : ComboBox
    {
        #region Private Member
        #region Known_Color
        /// <summary>
        /// 已经知道的颜色
        /// </summary>
        private string[] Known_Color = ("Transparent,Black,DimGray,Gray,DarkGray,Silver,LightGray,Gainsboro,WhiteSmoke,White,RosyBrown,IndianRed,Brown,Firebrick,LightCoral,Maroon,DarkRed,Red,Snow,MistyRose,Salmon,Tomato,DarkSalmon,Coral,OrangeRed,LightSalmon,Sienna,SeaShell,Chocalate,SaddleBrown,SandyBrown,PeachPuff,Peru,Linen,Bisque,DarkOrange,BurlyWood,Tan,AntiqueWhite,NavajoWhite,BlanchedAlmond,PapayaWhip,Mocassin,Orange,Wheat,OldLace,FloralWhite,DarkGoldenrod,Cornsilk,Gold,Khaki,LemonChiffon,PaleGoldenrod,DarkKhaki,Beige,LightGoldenrod,Olive,Yellow,LightYellow,Ivory,OliveDrab,YellowGreen,DarkOliveGreen,GreenYellow,Chartreuse,LawnGreen,DarkSeaGreen,ForestGreen,LimeGreen,PaleGreen,DarkGreen,Green,Lime,Honeydew,SeaGreen,MediumSeaGreen,SpringGreen,MintCream,MediumSpringGreen,MediumAquaMarine,YellowAquaMarine,Turquoise,LightSeaGreen,MediumTurquoise,DarkSlateGray,PaleTurquoise,Teal,DarkCyan,Aqua,Cyan,LightCyan,Azure,DarkTurquoise,CadetBlue,PowderBlue,LightBlue,DeepSkyBlue,SkyBlue,LightSkyBlue,SteelBlue,AliceBlue,DodgerBlue,SlateGray,LightSlateGray,LightSteelBlue,CornflowerBlue,RoyalBlue,MidnightBlue,Lavender,Navy,DarkBlue,MediumBlue,Blue,GhostWhite,SlateBlue,DarkSlateBlue,MediumSlateBlue,MediumPurple,BlueViolet,Indigo,DarkOrchid,DarkViolet,MediumOrchid,Thistle,Plum,Violet,Purple,DarkMagenta,Magenta,Fuchsia,Orchid,MediumVioletRed,DeepPink,HotPink,LavenderBlush,PaleVioletRed,Crimson,Pink,LightPink").Split(',');
        #endregion
        #endregion

        #region Constructor
        /// <summary>
        /// Initializes a new instance of the <see cref="ColorComboBox"/> class.
        /// </summary>
        public ColorComboBox()
        {
            this.DrawMode = DrawMode.OwnerDrawFixed;
            this.DropDownStyle = ComboBoxStyle.DropDownList;
            this.Items.AddRange(this.Known_Color);
            this.SelectedIndex = 0;
            this.DrawItem += new DrawItemEventHandler(ColorComboBox_DrawItem);
        }
        #endregion

        #region ColorComboBox_DrawItem
        /// <summary>
        /// Handles the DrawItem event of the ColorComboBox control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.DrawItemEventArgs"/> instance containing the event data.</param>
        private void ColorComboBox_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index == -1)
            {
                return;
            }
            // If the item is the edit box item, then draw the rectangle white
            // If the item is the selected item, then draw the rectangle blue
            // Otherwise, draw the rectangle filled in beige
            if (e.State == DrawItemState.ComboBoxEdit)
            {
                e.Graphics.FillRectangle(Brushes.White, e.Bounds);
            }
            else if (e.State == DrawItemState.Selected)
            {
                e.Graphics.FillRectangle(Brushes.CornflowerBlue, e.Bounds);
            }
            else
            {
                e.Graphics.FillRectangle(Brushes.Beige, e.Bounds);
            }

            // Cast the sender object  to ComboBox type.
            ComboBox TheBox = sender as ComboBox;
            string itemString = TheBox.Items[e.Index].ToString();
            Font myFont = new Font("Tahoma", 10);
            SolidBrush myBrush = new SolidBrush(Color.FromName(itemString));

            // Draw a Color Swatch
            e.Graphics.FillRectangle(myBrush, e.Bounds.X + 3, e.Bounds.Y + 3, 20, e.Bounds.Height - 5);
            e.Graphics.DrawRectangle(Pens.Black, e.Bounds.X + 3, e.Bounds.Y + 3, 20, e.Bounds.Height - 5);

            // Draw the text in the item.
            e.Graphics.DrawString(itemString, myFont, Brushes.Black, e.Bounds.X + 25, e.Bounds.Y + 1);

            // Draw the focus rectangle around the selected item.
            e.DrawFocusRectangle();
            myBrush.Dispose();
        }
        #endregion      
原创粉丝点击