コード例 #1
0
        protected override void OnPaint(PaintEventArgs e)
        {
            var rtl = RightToLeft == RightToLeft.Yes;

            AdjustScroll();
            var startI  = VerticalScroll.Value / ItemHeight - 1;
            var finishI = (VerticalScroll.Value + ClientSize.Height) / ItemHeight + 1;

            startI  = Math.Max(startI, 0);
            finishI = Math.Min(finishI, VisibleItems.Count);
            var y = 0;

            for (var i = startI; i < finishI; i++)
            {
                y = i * ItemHeight - VerticalScroll.Value;

                if (ImageList != null && VisibleItems[i].ImageIndex >= 0)
                {
                    if (rtl)
                    {
                        e.Graphics.DrawImage(ImageList.Images[VisibleItems[i].ImageIndex], Width - 1 - LeftPadding, y);
                    }
                    else
                    {
                        e.Graphics.DrawImage(ImageList.Images[VisibleItems[i].ImageIndex], 1, y);
                    }
                }

                var textRect = new Rectangle(LeftPadding, y, ClientSize.Width - 1 - LeftPadding,
                                             ItemHeight - 1);
                if (rtl)
                {
                    textRect = new Rectangle(1, y, ClientSize.Width - 1 - LeftPadding, ItemHeight - 1);
                }

                if (i == SelectedItemIndex)
                {
                    Brush selectedBrush =
                        new LinearGradientBrush(new Point(0, y - 3),
                                                new Point(0, y + ItemHeight),
                                                Colors.SelectedBackColor2, Colors.SelectedBackColor);
                    e.Graphics.FillRectangle(selectedBrush, textRect);
                    using (var pen = new Pen(Colors.SelectedBackColor2))
                        e.Graphics.DrawRectangle(pen, textRect);
                }
                //if (i == hoveredItemIndex)
                // e.Graphics.DrawRectangle(Pens.Red, textRect);

                if (i == HighlightedItemIndex)
                {
                    using (var pen = new Pen(Colors.HighlightingColor))
                        e.Graphics.DrawRectangle(pen, textRect);
                }

                var sf = new StringFormat();
                if (rtl)
                {
                    sf.FormatFlags = StringFormatFlags.DirectionRightToLeft;
                }

                var args = new PaintItemEventArgs(e.Graphics, e.ClipRectangle)
                {
                    Font         = Font,
                    TextRect     = new RectangleF(textRect.Location, textRect.Size),
                    StringFormat = sf,
                    IsSelected   = i == SelectedItemIndex,
                    IsHovered    = i == HighlightedItemIndex,
                    Colors       = Colors
                };
                //call drawing
                VisibleItems[i].OnPaint(args);
            }
        }