/// <summary>
        /// Draw background of the item
        /// </summary>
        /// <param name="g">The Graphic used to draw the background of the item</param>
        /// <param name="bound">Bound of the item</param>
        /// <param name="style">Style of the item</param>
        protected virtual void OnDrawBackground(Graphics g, Rectangle bound, StyleListBoxItemStyle style)
        {
            if (!Selected)
            {
                g.FillRectangle(new SolidBrush(style.BackColor), bound);
            }
            else
            {
                if (style.SelectionStyle == SelectionStyle.Flat)
                {
                    g.FillRectangle(new SolidBrush(style.SelectedBackColor), bound);
                }
                else
                {
                    // Leave some spaces for selection
                    Rectangle rect = new Rectangle(bound.X + 1, bound.Y + 1, bound.Width - 2, bound.Height - 2);

                    // Get lighter color of the background
                    Color c1 = Color.FromArgb(200, style.SelectedBackColor);
                    Color c2 = Color.FromArgb(50, style.SelectedBackColor);

                    g.DrawRectangle(new Pen(style.SelectedBackColor), rect);
                    g.FillRectangle(new LinearGradientBrush(new Point(rect.X, rect.Y), new Point(rect.X, rect.Bottom), c2, c1), rect);
                }
            }

            // Draw focus border
            if (Control != null && Control.FocuedIndex == this.Index)
            {
                int borderMargin = 1;
                if (style.SelectionStyle == SelectionStyle.Crystal)
                {
                    borderMargin = 2;
                }

                Rectangle focusRect = new Rectangle(bound.X + borderMargin, bound.Y + borderMargin, bound.Width - (borderMargin * 2), bound.Height - (borderMargin * 2));
                Pen       focusPen;

                if (this.Selected)
                {
                    focusPen = new Pen(style.SelectedTextColor);
                }
                else
                {
                    focusPen = new Pen(style.TextColor);
                }

                focusPen.DashStyle = DashStyle.Dot;

                g.DrawRectangle(focusPen, focusRect);
            }
        }
        /// <summary>
        /// Measures the height of the item.
        /// </summary>
        /// <param name="g">The Graphics used to measure the item</param>
        /// <returns>The height of the item.</returns>
        public int MeasureItem()
        {
            StyleListBoxItemStyle style = GetStyle();

            if (ImageIndex >= 0 && Control.ImageList != null)
            {
                return(Math.Max(Control.ImageList.Images[ImageIndex].Height, style.Font.Height) + style.Padding.Top + style.Padding.Bottom);
            }
            else
            {
                return(style.Font.Height + style.Padding.Top + style.Padding.Bottom);
            }
        }
        /// <summary>
        /// Constructor
        /// </summary>
        public StyleListBoxItem(String text = "", int imageIndex = -1)
        {
            _style = new StyleListBoxItemStyle();
            _style.OnStyleChange += OnStyleChange;

            SuspendUpdateChange();

            this.Text       = text;
            this.ImageIndex = imageIndex;
            this.Selected   = false;

            ResumeUpdateChange();
        }
        /// <summary>
        /// Draw the text part of the item
        /// </summary>
        /// <param name="g">The Graphics used to draw the text part</param>
        /// <param name="bound">The bound of where to draw the text</param>
        /// <param name="style">The style of the item</param>
        protected virtual void OnDrawText(Graphics g, Rectangle bound, StyleListBoxItemStyle style)
        {
            // Choose the correct text color
            Color textColor;

            if (this.Selected)
            {
                textColor = style.SelectedTextColor;
            }
            else
            {
                textColor = style.TextColor;
            }

            TextRenderer.DrawText(g, this.Text, style.Font, bound, textColor);
        }
        /// <summary>
        /// Clones a new StyleListBoxItemStyle by combining the current
        /// StyleListBoxItemStyle with alternative StyleListBoxItemStyle.
        ///
        /// Note:
        /// If any property of the current StyleListBoxItemStyle has not
        /// set, it will seek for value from the alternative StyleListBoxItemStyle
        /// </summary>
        /// <param name="other">Other alternative StyleListBoxItemStyle</param>
        /// <returns></returns>
        public StyleListBoxItemStyle CloneMerge(StyleListBoxItemStyle other)
        {
            StyleListBoxItemStyle clone = new StyleListBoxItemStyle();

            clone.BackColor             = (this.BackColor == Color.Empty) ? other.BackColor : this.BackColor;
            clone.TextColor             = (this.TextColor == Color.Empty) ? other.TextColor : this.TextColor;
            clone.Padding               = (this.Padding == Padding.Empty) ? other.Padding : this.Padding;
            clone.Font                  = (this.Font == null) ? other.Font : this.Font;
            clone.SelectedBackColor     = (this.SelectedBackColor == Color.Empty) ? other.SelectedBackColor : this.SelectedBackColor;
            clone.SelectedTextColor     = (this.SelectedTextColor == Color.Empty) ? other.SelectedTextColor : this.SelectedTextColor;
            clone.SelectionStyle        = (this.SelectionStyle == SelectionStyle.Inherited) ? other.SelectionStyle : this.SelectionStyle;
            clone.TextAlignment         = (this.TextAlignment == StyleItemAlignment.None) ? other.TextAlignment : this.TextAlignment;
            clone.TextVerticalAlignment = (this.TextVerticalAlignment == StyleItemVerticalAlignment.None) ? other.TextVerticalAlignment : this.TextVerticalAlignment;
            clone.ImageAlignment        = (this.ImageAlignment == StyleItemAlignment.None) ? other.ImageAlignment : this.ImageAlignment;

            return(clone);
        }
Exemple #6
0
        /// <summary>
        /// Constructor
        /// </summary>
        public StyleListBox()
        {
            itemList         = new StyleListBoxItemCollection(this);
            itemSelectedList = new StyleListBoxSelectedItemCollection();
            itemCacheHeight  = new List <int>();

            defaultItemStyle                   = new StyleListBoxItemStyle();
            defaultItemStyle.BackColor         = Color.White;
            defaultItemStyle.TextColor         = Color.Black;
            defaultItemStyle.SelectedBackColor = Color.Blue;
            defaultItemStyle.SelectedTextColor = Color.White;
            defaultItemStyle.Padding           = new Padding(2, 2, 2, 2);
            defaultItemStyle.Font              = new Font(this.Font, FontStyle.Regular);
            defaultItemStyle.SelectionStyle    = SelectionStyle.Flat;
            defaultItemStyle.OnStyleChange    += OnDefaultStyleChange;

            this.DoubleBuffered = true;
            SelectionMode       = SelectionMode.One;

            // Handles collection events
            itemList.OnItemAdded            += OnCollectionItemAdded;
            itemList.OnItemRemoved          += OnCollectionItemRemoved;
            itemList.OnItemChanged          += OnCollectionItemChanged;
            itemList.OnItemSelectionChanged += OnCollectionItemSelectionChanged;

            // Create scroll bar
            scroll = new VScrollBar();

            scroll.Width   = 20;
            scroll.Left    = this.Width - scroll.Width;
            scroll.Top     = 0;
            scroll.Height  = this.Height;
            scroll.Visible = false;

            scroll.Minimum     = 0;
            scroll.Maximum     = 0;
            scroll.Value       = 0;
            scroll.SmallChange = 50;
            scroll.LargeChange = 50;

            scroll.ValueChanged += Scroll_OnValueChanged;
            scroll.KeyDown      += Scroll_OnKeyDown;

            this.Controls.Add(scroll);
        }
 /// <summary>
 /// Measure the size of the text area
 /// </summary>
 /// <param name="g">The Graphic used to draw the text</param>
 /// <param name="style">Style of the item</param>
 /// <returns></returns>
 protected virtual Size OnMeasureText(Graphics g, StyleListBoxItemStyle style)
 {
     return(TextRenderer.MeasureText(this.Text, style.Font));
 }
        /// <summary>
        /// Overridable method to draw item on specified bounds.
        /// </summary>
        /// <param name="g">The Graphics used to draw the item.</param>
        /// <param name="bound">The Rectangle that specfies the bounds of the item</param>
        /// <param name="style">The Style used to draw the item.</param>
        public virtual void OnDrawItem(Graphics g, Rectangle bound, StyleListBoxItemStyle style)
        {
            int leftX  = style.Padding.Left;
            int rightX = bound.Right - style.Padding.Right;
            int textX;

            Size      textSize = OnMeasureText(g, style);
            Rectangle textBound;
            Rectangle imageBound = new Rectangle();

            OnDrawBackground(g, bound, style);

            // Draw image
            if (Control.ImageList != null && this.ImageIndex >= 0)
            {
                // Calculate the bound of the image
                Image image = Control.ImageList.Images[this.ImageIndex];

                if (style.ImageAlignment == StyleItemAlignment.Left || style.ImageAlignment == StyleItemAlignment.None)
                {
                    if (image.Height < textSize.Height)
                    {
                        imageBound = new Rectangle(leftX, bound.Y + style.Padding.Top + (image.Height - textSize.Height) / 2, image.Width, image.Height);
                    }
                    else
                    {
                        imageBound = new Rectangle(leftX, bound.Y + style.Padding.Top, image.Width, image.Height);
                    }

                    leftX += image.Width;
                }
                else
                {
                    rightX -= image.Width;

                    if (image.Height > textSize.Height)
                    {
                        imageBound = new Rectangle(rightX, bound.Y + style.Padding.Top + (image.Height - textSize.Height) / 2, image.Width, image.Height);
                    }
                    else
                    {
                        imageBound = new Rectangle(rightX, bound.Y + style.Padding.Top, image.Width, image.Height);
                    }
                }

                OnDrawImage(g, imageBound, image);
            }

            // Calculate the text bound
            if (style.TextAlignment == StyleItemAlignment.Right)
            {
                textX = rightX - textSize.Width;
            }
            else
            {
                textX = leftX;
            }

            if (style.TextVerticalAlignment == StyleItemVerticalAlignment.Top)
            {
                textBound = new Rectangle(textX, bound.Y + style.Padding.Top, textSize.Width, textSize.Height);
            }
            else if (style.TextVerticalAlignment == StyleItemVerticalAlignment.Bottom)
            {
                textBound = new Rectangle(textX, bound.Bottom - style.Padding.Bottom - textSize.Height, textSize.Width, textSize.Height);
            }
            else
            {
                if (textSize.Height < imageBound.Height)
                {
                    textBound = new Rectangle(textX, bound.Y + style.Padding.Top + (imageBound.Height - textSize.Height) / 2, textSize.Width, textSize.Height);
                }
                else
                {
                    textBound = new Rectangle(textX, bound.Y + style.Padding.Top, textSize.Width, textSize.Height);
                }
            }

            // Draw text
            OnDrawText(g, textBound, style);
        }
        /// <summary>
        /// Draws list item on specified bounds.
        /// </summary>
        /// <param name="g">The Graphics used to draw the item.</param>
        /// <param name="bound">The Rectangle that specifies the bounds of the item</param>
        public void DrawItem(Graphics g, Rectangle bound)
        {
            StyleListBoxItemStyle style = GetStyle();

            OnDrawItem(g, bound, style);
        }