protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); e.Graphics.TranslateTransform(this.AutoScrollPosition.X, this.AutoScrollPosition.Y); List <int> indices = GetVisibleIndices(e.Graphics.VisibleClipBounds); if (CacheItems != null) { Rectangle rect = this.Bounds; rect.Offset(-this.AutoScrollPosition.X, -this.AutoScrollPosition.Y); List <int> allIndices = GetVisibleIndices(rect); if (allIndices.Count > 0 && (cachedIndices.Count == 0 || cachedIndices.Count < allIndices.Count || (cachedIndices[0] > allIndices[0] || cachedIndices[cachedIndices.Count - 1] < allIndices[allIndices.Count - 1]))) { CacheItemEventArgs ne = new CacheItemEventArgs(allIndices); CacheItems(this, ne); if (ne.Success) { cachedIndices = allIndices; } } } foreach (int i in indices) { if (i >= VirtualListSize) { throw new ArgumentException("index out of range", "i"); } Size bs = new Size((int)m_TileBorder.Width * 2, (int)m_TileBorder.Width * 2); // border size Size single = new Size(1, 1); Point p = GetItemLocation(i); Point mp = new Point(p.X + m_TileMargin.Left, p.Y + m_TileMargin.Top); //margined point Point bp = new Point(mp.X + (int)m_TileBorder.Width, mp.Y + (int)m_TileBorder.Width); //bordered point Point pp = new Point(bp.X + m_TilePadding.Left, bp.Y + m_TilePadding.Top); //padded point //m_TileSize + m_TilePadding.Size + bs + m_TileMargin.Size Rectangle mr = new Rectangle(mp, m_TileSize + m_TilePadding.Size + bs); Rectangle br = new Rectangle(bp, m_TileSize + m_TilePadding.Size); Rectangle pr = new Rectangle(pp, m_TileSize); if (this.SelectedIndices.Contains(i)) { e.Graphics.FillRectangle(m_TileHighlightColor, mr); } if (DrawItem != null) { DrawItem(this, new DrawTileListItemEventArgs(e.Graphics, this.Font, br, i, (this.m_FocusIndex == i ? DrawItemState.Selected : DrawItemState.None))); } else { e.Graphics.FillRectangle(Brushes.Pink, pr); // tile itself //TODO: Rewrite text drawing completely string text = i.ToString(); SizeF strLayout = new SizeF(TileSize.Width, TilePadding.Bottom); SizeF strSize = e.Graphics.MeasureString(text, this.Font, strLayout, TextStringFormat); PointF strPos = new PointF(br.X + (br.Width - strSize.Width) / 2, br.Top + TilePadding.Top + TileSize.Height + (strLayout.Height / 2 - strSize.Height / 2)); //new RectangleF(new PointF((ImagesListView.TileSize.Width - strSize.Width) / 2 + e.Bounds.Left, e.Bounds.Y + ImagesListView.TilePadding.Top + ImagesListView.TileSize.Height + (e.Font.Height - strSize.Height / 2)) if (TilePadding.Bottom - strSize.Height < 2) { return; //No space for text } e.Graphics.DrawString(text, this.Font, SystemBrushes.ControlText, new RectangleF(strPos, strSize), TextStringFormat); } if (m_TileBorder.Width > 0) { e.Graphics.DrawRectangle(m_TileBorder, new Rectangle(mr.Location, mr.Size - single)); } if (this.m_FocusIndex == i) // not sure yet on should it be in DrawItem or here... { ControlPaint.DrawFocusRectangle(e.Graphics, mr); } } //e.Graphics.DrawRectangle(new Pen(Color.FromArgb(64, Color.Black),1), new Rectangle(new Point((int)e.Graphics.ClipBounds.Location.X, (int)e.Graphics.ClipBounds.Location.Y), new Size((int)e.Graphics.ClipBounds.Width - 1, (int)e.Graphics.ClipBounds.Height-1))); }
protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); e.Graphics.TranslateTransform(this.AutoScrollPosition.X, this.AutoScrollPosition.Y); List<int> indices = GetVisibleIndices(e.Graphics.VisibleClipBounds); if (CacheItems != null) { Rectangle rect = this.Bounds; rect.Offset(-this.AutoScrollPosition.X, -this.AutoScrollPosition.Y); List<int> allIndices = GetVisibleIndices(rect); if (allIndices.Count > 0 && (cachedIndices.Count == 0 || cachedIndices.Count < allIndices.Count || (cachedIndices[0] > allIndices[0] || cachedIndices[cachedIndices.Count - 1] < allIndices[allIndices.Count - 1]))) { CacheItemEventArgs ne = new CacheItemEventArgs(allIndices); CacheItems(this, ne); if (ne.Success) cachedIndices = allIndices; } } foreach(int i in indices) { if (i >= VirtualListSize) throw new ArgumentException("index out of range", "i"); Size bs = new Size((int)m_TileBorder.Width * 2, (int)m_TileBorder.Width * 2); // border size Size single = new Size(1, 1); Point p = GetItemLocation(i); Point mp = new Point(p.X + m_TileMargin.Left , p.Y + m_TileMargin.Top); //margined point Point bp = new Point(mp.X + (int)m_TileBorder.Width, mp.Y + (int)m_TileBorder.Width); //bordered point Point pp = new Point(bp.X + m_TilePadding.Left, bp.Y + m_TilePadding.Top); //padded point //m_TileSize + m_TilePadding.Size + bs + m_TileMargin.Size Rectangle mr = new Rectangle(mp, m_TileSize + m_TilePadding.Size + bs); Rectangle br = new Rectangle(bp, m_TileSize + m_TilePadding.Size); Rectangle pr = new Rectangle(pp, m_TileSize); if (this.SelectedIndices.Contains(i)) { e.Graphics.FillRectangle(m_TileHighlightColor, mr); } if (DrawItem != null) DrawItem(this, new DrawTileListItemEventArgs(e.Graphics, this.Font, br, i, (this.m_FocusIndex == i ? DrawItemState.Selected : DrawItemState.None))); else { e.Graphics.FillRectangle(Brushes.Pink, pr); // tile itself //TODO: Rewrite text drawing completely string text = i.ToString(); SizeF strLayout = new SizeF(TileSize.Width, TilePadding.Bottom); SizeF strSize = e.Graphics.MeasureString(text, this.Font, strLayout, TextStringFormat); PointF strPos = new PointF(br.X + (br.Width - strSize.Width) / 2, br.Top + TilePadding.Top + TileSize.Height + (strLayout.Height / 2 - strSize.Height / 2)); //new RectangleF(new PointF((ImagesListView.TileSize.Width - strSize.Width) / 2 + e.Bounds.Left, e.Bounds.Y + ImagesListView.TilePadding.Top + ImagesListView.TileSize.Height + (e.Font.Height - strSize.Height / 2)) if (TilePadding.Bottom - strSize.Height < 2) return; //No space for text e.Graphics.DrawString(text, this.Font, SystemBrushes.ControlText, new RectangleF(strPos, strSize), TextStringFormat); } if (m_TileBorder.Width > 0) e.Graphics.DrawRectangle(m_TileBorder, new Rectangle(mr.Location, mr.Size - single)); if (this.m_FocusIndex == i) // not sure yet on should it be in DrawItem or here... { ControlPaint.DrawFocusRectangle(e.Graphics, mr); } } //e.Graphics.DrawRectangle(new Pen(Color.FromArgb(64, Color.Black),1), new Rectangle(new Point((int)e.Graphics.ClipBounds.Location.X, (int)e.Graphics.ClipBounds.Location.Y), new Size((int)e.Graphics.ClipBounds.Width - 1, (int)e.Graphics.ClipBounds.Height-1))); }