protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); e.Graphics.FillRectangle(new SolidBrush(this.renderer.ColorBackground), this.ClientRectangle); e.Graphics.SetClip(new Rectangle( this.ClientRectangle.X + this.Padding.Left, this.ClientRectangle.Y + this.Padding.Top, this.ClientRectangle.Width - this.Padding.Horizontal, this.ClientRectangle.Height - this.Padding.Vertical), System.Drawing.Drawing2D.CombineMode.Intersect); if (this.cachedEventItemUserPaint == null) { this.cachedEventItemUserPaint = new TiledViewItemDrawEventArgs(this); } if (this.model.Count > 0) { int firstIndex = this.PickModelIndexAt(e.ClipRectangle.Left, e.ClipRectangle.Top, true, true); int lastIndex = this.PickModelIndexAt(e.ClipRectangle.Right - 1, e.ClipRectangle.Bottom - 1, true, true); Point firstItemPos = this.GetModelIndexLocation(firstIndex); int firstSelected = this.selection.Count > 0 ? this.selection.Min(s => s.ModelIndex) : -1; int lastSelected = this.selection.Count > 0 ? this.selection.Max(s => s.ModelIndex) : -1; Point basePos = firstItemPos; Point curPos = basePos; int itemsInRow = 0; for (int i = firstIndex; i <= lastIndex; i++) { this.cachedEventItemUserPaint.Handled = false; this.cachedEventItemUserPaint.Graphics = e.Graphics; this.cachedEventItemUserPaint.ModelIndex = i; this.cachedEventItemUserPaint.Item = this.model.GetItemAt(i); this.cachedEventItemUserPaint.ItemRect = new Rectangle(curPos.X, curPos.Y, this.tileSize.Width, this.tileSize.Height); this.cachedEventItemUserPaint.IsHovered = i == this.hoverIndex; this.cachedEventItemUserPaint.IsSelected = i >= firstSelected && i <= lastSelected && this.selection.Any(s => s.ModelIndex == i); this.OnPaintItem(this.cachedEventItemUserPaint); itemsInRow++; if (itemsInRow == this.tilesPerRow) { curPos.X = basePos.X; curPos.Y += this.tileSize.Height + this.spacing.Height; itemsInRow = 0; } else { curPos.X += this.tileSize.Width + this.spacing.Width; } } } if (!this.Enabled) { e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(128, this.renderer.ColorBackground)), this.ClientRectangle); } }
protected virtual void OnPaintItem(TiledViewItemDrawEventArgs e) { if (this.ItemUserPaint != null) { this.ItemUserPaint(this, e); if (e.Handled) { return; } } string text; Image icon; this.GetItemAppearance(e.ModelIndex, e.Item, out text, out icon); bool isItemEditing = this.IsEditing && e.ModelIndex == this.editedItem.ModelIndex; bool hasText = !string.IsNullOrEmpty(text); SizeF textSize = SizeF.Empty; Rectangle itemRect = e.ItemRect; if (hasText) { textSize = e.Graphics.MeasureString( text, this.renderer.FontRegular, itemRect.Width, this.itemStringFormat); } if (icon != null) { int iconAreaHeight = itemRect.Height - (int)Math.Ceiling(textSize.Height); if (icon.Width > itemRect.Width || icon.Height > itemRect.Height) { SizeF iconSize = icon.Size; float factor = 1.0f; if ((float)itemRect.Width / iconSize.Width < (float)itemRect.Height / iconSize.Height) { factor = (float)itemRect.Width / iconSize.Width; } else { factor = (float)itemRect.Height / iconSize.Height; } iconSize.Height = iconSize.Height * factor; iconSize.Width = iconSize.Width * factor; e.Graphics.DrawImage(icon, new RectangleF( itemRect.X + (itemRect.Width - iconSize.Width) * 0.5f, itemRect.Y + Math.Max(0, iconAreaHeight - iconSize.Height) * 0.5f, iconSize.Width, iconSize.Height)); } else { e.Graphics.DrawImageUnscaled(icon, new Rectangle( itemRect.X + (itemRect.Width - icon.Width) / 2, itemRect.Y + Math.Max(0, iconAreaHeight - icon.Height) / 2, icon.Width, icon.Height)); } } if (this.Enabled && !isItemEditing) { if (e.IsSelected || (Control.MouseButtons != MouseButtons.None && this.highlightHoverItems && e.IsHovered)) { this.renderer.DrawSelection(e.Graphics, itemRect, true); } else if (this.highlightHoverItems && e.IsHovered) { this.renderer.DrawSelection(e.Graphics, itemRect, false); } } if (hasText && !isItemEditing) { if (icon != null) { this.itemStringFormat.LineAlignment = StringAlignment.Far; } else { this.itemStringFormat.LineAlignment = StringAlignment.Center; } e.Graphics.DrawString( text, this.renderer.FontRegular, new SolidBrush(this.renderer.ColorText), itemRect, this.itemStringFormat); } }