Example #1
0
        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            if (Items.Count == 0)
            {
                return;
            }

            var g = e.Graphics;

            using (var brush = new SolidBrush(BackColor))
            {
                g.FillRectangle(brush, 0, e.Bounds.Y, e.Bounds.Width, 16);
            }

            var boxColor = ColorTranslator.FromHtml(BoxColor);

            using (var pen = new Pen(boxColor))
            {
                g.DrawRectangle(pen, 0, e.Bounds.Y + 1, 12, 12);
            }

            if (CheckedIndices.Contains(e.Index))
            {
                using (var brush = new SolidBrush(boxColor))
                {
                    g.FillRectangle(brush, 2, e.Bounds.Y + 3, 9, 9);
                }
            }

            var size = g.MeasureString(Text, Font);

            using (var brush = new SolidBrush(ForeColor))
            {
                g.DrawString(Items[e.Index].ToString(), Font, brush,
                             new Rectangle(16,            // standard icon size
                                           e.Bounds.Y,
                                           e.Bounds.Width - 16,
                                           (int)size.Height),
                             new StringFormat
                {
                    Trimming    = StringTrimming.EllipsisCharacter,
                    FormatFlags = StringFormatFlags.LineLimit | StringFormatFlags.NoWrap
                });
            }
        }
        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            if (Items.Count == 0)
            {
                return;                              //sanity check
            }
            IColoredListBoxItem item = (IColoredListBoxItem)Items[e.Index];

            // Draw background
            e.DrawBackground();

            // Draw checkbox
            CheckBoxRenderer.DrawCheckBox(e.Graphics, new Point(e.Bounds.Left + 1, e.Bounds.Top + 1), CheckedIndices.Contains(e.Index) ? CheckBoxState.CheckedNormal : CheckBoxState.UncheckedNormal);

            // Draw warning icon
            if (item.ShowWarning && WarningIcon != null)
            {
                e.Graphics.DrawImage(WarningIcon, e.Bounds.Left + e.Bounds.Height + 2, e.Bounds.Top, WarningIcon.Width, WarningIcon.Height);
            }
            else             // Draw color rectangle
            {
                using (SolidBrush bg = new SolidBrush(item.Color))
                {
                    e.Graphics.FillRectangle(bg, e.Bounds.Left + e.Bounds.Height + 2, e.Bounds.Top + 2, e.Bounds.Height, e.Bounds.Height - 5);
                }
                using (Pen outline = new Pen(Color.Black))
                {
                    e.Graphics.DrawRectangle(outline, e.Bounds.Left + e.Bounds.Height + 2, e.Bounds.Top + 2, e.Bounds.Height, e.Bounds.Height - 5);
                }
            }

            // Draw text
            int       offset     = e.Bounds.Left + e.Bounds.Height * 2 + 4;
            Rectangle textbounds = new Rectangle(offset, e.Bounds.Top, e.Bounds.Width - offset, e.Bounds.Height);

            e.Graphics.DrawString(item.ToString(), e.Font, new SolidBrush(e.ForeColor), textbounds.Left, textbounds.Top);

            // Draw focus rectangle
            if (e.State == DrawItemState.Focus)
            {
                ControlPaint.DrawFocusRectangle(e.Graphics, textbounds, e.ForeColor, e.BackColor);
            }
        }