예제 #1
0
        private void cbColor_DrawItem(object sender, DrawItemEventArgs e)
        {
            ComboBox cb         = (ComboBox)sender;
            Graphics g          = e.Graphics;
            Color    brushColor = DrawUtils.ColorFromIndex(e.Index);

            // Draw the background of the item.
            e.DrawBackground();
            Rectangle largerect = new Rectangle(e.Bounds.X, e.Bounds.Top, e.Bounds.Width - 1, e.Bounds.Height - 1);
            Rectangle colorrect = new Rectangle(4, e.Bounds.Top + 2, e.Bounds.Height - 2, e.Bounds.Height - 5);

            if (DrawUtils.IsNamedColorIndex(e.Index)) // отрисовка рамки цвета пунктов основеых цветов
            {
                using (SolidBrush brush = new SolidBrush(brushColor))
                {
                    g.FillRectangle(brush, colorrect);
                }
                g.DrawRectangle(Pens.Black, colorrect);
            }
            RectangleF textRect = new RectangleF(e.Bounds.X + colorrect.Width + 5, e.Bounds.Y + 1,
                                                 e.Bounds.Width, e.Bounds.Height);

            using (SolidBrush textColor = new SolidBrush(e.ForeColor))
            {
                if (DrawUtils.IsNamedColorIndex(e.Index))
                {// отрисовка пунктов основных цветов
                    g.DrawString(DrawUtils.GetColorNameFromIndex(e.Index), cb.Font, textColor, textRect);
                }
                else
                if (DrawUtils.IsCustomColorIndex(e.Index))     // отрисовка пунктов дополнительных цветов
                {
                    using (SolidBrush brush = new SolidBrush(brushColor))
                    {
                        g.FillRectangle(brush, largerect);
                    }
                    using (Pen pen = new Pen(cb.BackColor))
                    {
                        g.DrawRectangle(pen, largerect);
                    }
                }
                else     // отрисовка последнего пункта: Выбор цвета...
                {
                    g.DrawString(cb.Items[e.Index].ToString(), cb.Font, textColor, largerect);
                }
            }
            // Draw the focus rectangle if the mouse hovers over an item.
            e.DrawFocusRectangle();
        }