/// <summary>
        /// Raises the <see cref="E:System.Windows.Forms.ComboBox.DrawItem"/> event.
        /// </summary>
        /// <param name="e">A <see cref="T:System.Windows.Forms.DrawItemEventArgs"/> that contains the event data. </param>
        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            base.OnDrawItem(e);

            if (e.Index < 0)
            {
                return;
            }

            if (DesignMode)
            {
                return;
            }

            e.DrawBackground();

            var boundsRect = e.Bounds;
            var lastRight  = 0;

            Color brushForeColor;

            if ((e.State & DrawItemState.Selected) == 0)
            {
                // Item is not selected. Use BackColorOdd & BackColorEven
                var backColor = Convert.ToBoolean(e.Index % 2) ? BackColorOdd : BackColorEven;
                using (var brushBackColor = new SolidBrush(backColor))
                {
                    e.Graphics.FillRectangle(brushBackColor, e.Bounds);
                }
                brushForeColor = Color.Black;
            }
            else
            {
                // Item is selected. Use ForeColor = White
                brushForeColor = Color.White;
            }

            using (var linePen = new Pen(SystemColors.GrayText))
            {
                using (var brush = new SolidBrush(brushForeColor))
                {
                    if (ColumnNames.Count == 0)
                    {
                        e.Graphics.DrawString(Convert.ToString(Items[e.Index]), Font, brush, boundsRect);
                    }
                    else
                    {
                        // If the ComboBox is displaying a RightToLeft language, draw it this way.
                        if (RightToLeft.Equals(RightToLeft.Yes))
                        {
                            // Define a StringFormat object to make the string display RTL.
                            var rtl = new StringFormat
                            {
                                Alignment   = StringAlignment.Near,
                                FormatFlags = StringFormatFlags.DirectionRightToLeft
                            };

                            // Draw the strings in reverse order from high column index to zero column index.
                            for (var colIndex = ColumnNames.Count - 1; colIndex >= 0; colIndex--)
                            {
                                if (!Convert.ToBoolean(ColumnWidths[colIndex]))
                                {
                                    continue;
                                }

                                var item = Convert.ToString(FilterItemOnProperty(Items[e.Index], ColumnNames[colIndex]));

                                boundsRect.X     = lastRight;
                                boundsRect.Width = _columnWidths[colIndex];
                                lastRight        = boundsRect.Right;

                                // Draw the string with the RTL object.
                                e.Graphics.DrawString(item, Font, brush, boundsRect, rtl);

                                if (colIndex > 0)
                                {
                                    e.Graphics.DrawLine(linePen, boundsRect.Right, boundsRect.Top, boundsRect.Right, boundsRect.Bottom);
                                }
                            }
                        }
                        // If the ComboBox is displaying a LeftToRight language, draw it this way.
                        else
                        {
                            // Display the strings in ascending order from zero to the highest column.
                            for (var colIndex = 0; colIndex < ColumnNames.Count; colIndex++)
                            {
                                if (Convert.ToBoolean(_columnWidths[colIndex]))
                                {
                                    var item = Convert.ToString(FilterItemOnProperty(Items[e.Index], ColumnNames[colIndex]));

                                    boundsRect.X     = lastRight;
                                    boundsRect.Width = _columnWidths[colIndex];
                                    lastRight        = boundsRect.Right;
                                    e.Graphics.DrawString(item, Font, brush, boundsRect);

                                    if (colIndex < ColumnNames.Count - 1)
                                    {
                                        e.Graphics.DrawLine(linePen, boundsRect.Right, boundsRect.Top, boundsRect.Right, boundsRect.Bottom);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            e.DrawFocusRectangle();
        }
示例#2
0
        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            base.OnDrawItem(e);

            if (DesignMode)
            {
                return;
            }

            e.DrawBackground();

            Rectangle boundsRect = e.Bounds;
            int       lastRight  = 0;

            Color brushForeColor;

            if ((e.State & DrawItemState.Selected) == 0)
            {
                // Item is not selected. Use BackColorOdd & BackColorEven
                Color backColor;
                backColor = Convert.ToBoolean(e.Index % 2) ? _backColorOdd : _backColorEven;
                using (SolidBrush brushBackColor = new SolidBrush(backColor))
                {
                    e.Graphics.FillRectangle(brushBackColor, e.Bounds);
                }
                brushForeColor = Color.Black;
            }
            else
            {
                GraphicsPath pa = new GraphicsPath();
                StyleHelp.DrawArc(e.Bounds, pa, 6, EGroupPos.None);
                LinearGradientBrush lgbrush = new LinearGradientBrush(e.Bounds, Color.Transparent, Color.Transparent, LinearGradientMode.Vertical);
                lgbrush.InterpolationColors = ColorHelper.GetBlend4();
                e.Graphics.FillRectangle(lgbrush, e.Bounds);
                Rectangle rect = e.Bounds;
                rect.Width--;
                rect.Height--;
                using (Pen pen = new Pen(_officeColorTable.ComboBoxBorderHoverColor))
                {
                    e.Graphics.DrawRectangle(pen, rect);
                }
                // Item is selected. Use ForeColor = White
                //brushForeColor = Color.White;
                brushForeColor = Color.Black;
            }
            if (_bShowColumnName && e.Index == 0)
            {
                GraphicsPath pa = new GraphicsPath();
                StyleHelp.DrawArc(e.Bounds, pa, 6, EGroupPos.None);
                LinearGradientBrush lgbrush = new LinearGradientBrush(e.Bounds, Color.Transparent, Color.Transparent, LinearGradientMode.Vertical);
                lgbrush.InterpolationColors = ColorHelper.GetBlendSliver();
                e.Graphics.FillRectangle(lgbrush, e.Bounds);
                Rectangle rect = e.Bounds;
                rect.Width--;
                rect.Height--;
                using (Pen pen = new Pen(Color.FromArgb(200, 225, 229, 240)))
                {
                    e.Graphics.DrawRectangle(pen, rect);
                }
            }

            using (Pen linePen = new Pen(SystemColors.GrayText))
            {
                using (SolidBrush brush = new SolidBrush(brushForeColor))
                {
                    if (!Convert.ToBoolean(_columnNames.Count))
                    {
                        e.Graphics.DrawString(Convert.ToString(Items[e.Index]), Font, brush, boundsRect);
                    }
                    else
                    {
                        // If the ComboBox is displaying a RightToLeft language, draw it this way.
                        if (RightToLeft.Equals(RightToLeft.Yes))
                        {
                            // Define a StringFormat object to make the string display RTL.
                            StringFormat rtl = new StringFormat();
                            rtl.Alignment   = StringAlignment.Near;
                            rtl.FormatFlags = StringFormatFlags.DirectionRightToLeft;

                            // Draw the strings in reverse order from high column index to zero column index.
                            for (int colIndex = _columnNames.Count - 1; colIndex >= 0; colIndex--)
                            {
                                if (Convert.ToBoolean(_columnWidths[colIndex]))
                                {
                                    string item = Convert.ToString(FilterItemOnProperty(Items[e.Index], _columnNames[colIndex]));

                                    boundsRect.X     = lastRight;
                                    boundsRect.Width = (int)_columnWidths[colIndex];
                                    lastRight        = boundsRect.Right;

                                    // Draw the string with the RTL object.
                                    e.Graphics.DrawString(item, Font, brush, boundsRect, rtl);

                                    if (colIndex > 0)
                                    {
                                        e.Graphics.DrawLine(linePen, boundsRect.Right, boundsRect.Top, boundsRect.Right, boundsRect.Bottom);
                                    }
                                }
                            }
                        }
                        // If the ComboBox is displaying a LeftToRight language, draw it this way.
                        else
                        {
                            // Display the strings in ascending order from zero to the highest column.
                            for (int colIndex = 0; colIndex < _columnNames.Count; colIndex++)
                            {
                                if (Convert.ToBoolean(_columnWidths[colIndex]))
                                {
                                    string item = Convert.ToString(FilterItemOnProperty(Items[e.Index], _columnNames[colIndex]));

                                    boundsRect.X     = lastRight;
                                    boundsRect.Width = (int)_columnWidths[colIndex];
                                    lastRight        = boundsRect.Right;
                                    e.Graphics.DrawString(item, Font, brush, boundsRect);

                                    if (colIndex < _columnNames.Count - 1)
                                    {
                                        e.Graphics.DrawLine(linePen, boundsRect.Right, boundsRect.Top, boundsRect.Right, boundsRect.Bottom);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            e.DrawFocusRectangle();
        }
        /// <summary>
        /// Raises the <see cref="E:System.Windows.Forms.ComboBox.DrawItem"/> event.
        /// </summary>
        /// <param name="e">A <see cref="T:System.Windows.Forms.DrawItemEventArgs"/> that contains the event data. </param>
        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            if (e.Index < 0 || DesignMode)
            {
                base.OnDrawItem(e);
                return;
            }

            e.DrawBackground();

            var   boundsRect = e.Bounds;
            Image icon       = FilterItemOnProperty(Items[e.Index], "Icon") as Image;
            var   lastRight  = OwningColumn.CategoryColumnName == null || e.State.HasFlag(DrawItemState.ComboBoxEdit) || icon != null ? 0 : IndentSize;

            var drawUncategorizedItemSpecial = IsUncategorizedItemSpecial(e.Index);

            if (e.State.HasFlag(DrawItemState.ComboBoxEdit))
            {
                if (drawUncategorizedItemSpecial)
                {
                    return;
                }
                boundsRect.Height--;
            }

            Color brushForeColor = Color.Black;

            if ((e.State & DrawItemState.Selected) == 0)
            {
                // Item is not selected. Use BackColorOdd & BackColorEven
                var backColor = Convert.ToBoolean(e.Index % 2) ? BackColorOdd : BackColorEven;
                using (var brushBackColor = new SolidBrush(backColor))
                    e.Graphics.FillRectangle(brushBackColor, boundsRect);
            }

            Font font;

            object category;

            if (IsItemFirstInCategory(e.Index, out category) && !e.State.HasFlag(DrawItemState.ComboBoxEdit))
            {
                var categoryRect = new Rectangle(boundsRect.X, boundsRect.Y, boundsRect.Width, boundsRect.Height / 2);
                if ((e.State & DrawItemState.Selected) != 0)
                {
                    // Item is selected. Repaint background for the category portion of the line.
                    using (var brushBackColor = new SolidBrush(Color.White))
                        e.Graphics.FillRectangle(brushBackColor, categoryRect);
                }

                var sCategory = Convert.ToString(category);
                font = OwningColumn.FontForCategories ?? Font;
                using (var brush = new SolidBrush(brushForeColor))
                {
                    if (e.Index > 0)
                    {
                        using (var linePen = new Pen(SystemColors.GrayText))
                            e.Graphics.DrawLine(linePen, categoryRect.X, categoryRect.Y, categoryRect.Right, categoryRect.Y);
                    }
                    categoryRect.Y      += kSeparatorLineHeight;
                    categoryRect.Height -= kSeparatorLineHeight;
                    e.Graphics.DrawString(sCategory, font, brush, categoryRect);
                }
                boundsRect.Y       = categoryRect.Bottom;
                boundsRect.Height -= categoryRect.Height;
            }

            if ((e.State & DrawItemState.Selected) != 0)
            {
                // Item is selected. Use ForeColor = White
                brushForeColor = Color.White;
            }

            if (e.State.HasFlag(DrawItemState.ComboBoxEdit))
            {
                lastRight += 2;
            }

            font = Font;
            if (drawUncategorizedItemSpecial)
            {
                if (OwningColumn.FontForUncategorizedItems != null)
                {
                    font = OwningColumn.FontForUncategorizedItems;
                }
            }
            else if (OwningColumn.CategoryColumnName != null)
            {
                boundsRect.Width -= IndentSize;
            }

            using (var linePen = new Pen(SystemColors.GrayText))
            {
                using (var brush = new SolidBrush(brushForeColor))
                {
                    if (ColumnNames.Count == 0)
                    {
                        e.Graphics.DrawString(Convert.ToString(Items[e.Index]), font, brush, boundsRect);
                    }
                    else
                    {
                        if (drawUncategorizedItemSpecial)
                        {
                            if (e.Index > 0)
                            {
                                e.Graphics.DrawLine(linePen, boundsRect.X, boundsRect.Y, boundsRect.Right, boundsRect.Y);
                            }
                            boundsRect.Width  -= IndentSize;
                            boundsRect.Y      += kSeparatorLineHeight;
                            boundsRect.Height -= kSeparatorLineHeight;
                        }

                        if (icon != null)
                        {
                            int top    = boundsRect.Y;
                            int height = boundsRect.Height;
                            int left   = boundsRect.X;
                            int width  = IndentSize - 2;
                            if (icon.Height < height)
                            {
                                top   += ((boundsRect.Height - icon.Height) / 2);
                                height = icon.Height;
                            }
                            if (icon.Width < width)
                            {
                                left += ((boundsRect.Width - icon.Width) / 2);
                                width = icon.Width;
                            }
                            e.Graphics.DrawImage(icon, left, top, width, height);
                            lastRight += IndentSize;
                        }

                        if (drawUncategorizedItemSpecial)
                        {
                            string item = Convert.ToString(FilterItemOnProperty(Items[e.Index], "SpecialUse"));
                            boundsRect.X     = lastRight;
                            boundsRect.Width = boundsRect.Width;
                            e.Graphics.DrawString(item, font, brush, boundsRect, new StringFormat());
                        }
                        else
                        {
                            // Default (L-to-R) is to display the strings in ascending order from zero to the highest column.
                            int loopStart                    = 0;
                            Func <int, bool> notDone         = i => i < ColumnNames.Count;
                            Func <int, int>  loopIncrementer = i => i + 1;
                            StringFormat     format          = new StringFormat();

                            // If the ComboBox is displaying a RightToLeft language, draw it this way.
                            if (RightToLeft.Equals(RightToLeft.Yes))
                            {
                                // Define a StringFormat object to make the string display RTL.
                                format = new StringFormat
                                {
                                    Alignment   = StringAlignment.Near,
                                    FormatFlags = StringFormatFlags.DirectionRightToLeft
                                };

                                // Draw the strings in reverse order from high column index to zero column index.
                                loopStart       = ColumnNames.Count - 1;
                                notDone         = i => i >= 0;
                                loopIncrementer = i => i - 1;
                            }

                            for (var colIndex = loopStart; notDone(colIndex); colIndex = loopIncrementer(colIndex))
                            {
                                if (m_columnWidths[colIndex] > 0)
                                {
                                    string item = Convert.ToString(FilterItemOnProperty(Items[e.Index], ColumnNames[colIndex]));

                                    boundsRect.X     = lastRight;
                                    boundsRect.Width = m_columnWidths[colIndex];
                                    lastRight        = boundsRect.Right;
                                    e.Graphics.DrawString(item, font, brush, boundsRect, format);

                                    if (lastRight + 7 >= e.Bounds.Right)
                                    {
                                        break;
                                    }

                                    if (notDone(loopIncrementer(colIndex)) && !drawUncategorizedItemSpecial)
                                    {
                                        e.Graphics.DrawLine(linePen, boundsRect.Right - 1, boundsRect.Top, boundsRect.Right - 1, boundsRect.Bottom);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            e.DrawFocusRectangle();
        }