DgvProgressBarCell.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System.Drawing;
  2. using System.Windows.Forms;
  3. namespace AutoPatcherAdmin
  4. {
  5. internal sealed class DgvProgressBarCell : DataGridViewTextBoxCell
  6. {
  7. public override Type ValueType => typeof(int);
  8. public override object DefaultNewRowValue => 0;
  9. protected override void Paint(
  10. Graphics g, Rectangle clipBounds, Rectangle cellBounds,
  11. int rowIndex, DataGridViewElementStates elementState,
  12. object? value, object? formattedValue, string? errorText,
  13. DataGridViewCellStyle cellStyle,
  14. DataGridViewAdvancedBorderStyle advancedBorderStyle,
  15. DataGridViewPaintParts paintParts)
  16. {
  17. // Draw background + border, skip default text rendering
  18. base.Paint(g, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue,
  19. errorText, cellStyle, advancedBorderStyle,
  20. paintParts & ~DataGridViewPaintParts.ContentForeground);
  21. int pct = value is int p ? Math.Clamp(p, 0, 100) : 0;
  22. const int pad = 5;
  23. const int barH = 7;
  24. int barY = cellBounds.Y + (cellBounds.Height - barH) / 2;
  25. int barX = cellBounds.X + pad;
  26. int barW = cellBounds.Width - pad * 2;
  27. if (barW <= 0) return;
  28. // Track — semi-transparent dark overlay so it's readable on any row colour
  29. using (var trackBrush = new SolidBrush(Color.FromArgb(55, 0, 0, 0)))
  30. g.FillRectangle(trackBrush, barX, barY, barW, barH);
  31. // Fill — white with enough alpha to be clearly visible on all status colours
  32. if (pct > 0)
  33. {
  34. int fillW = Math.Max(1, (int)(barW * pct / 100.0));
  35. using var fillBrush = new SolidBrush(Color.FromArgb(180, 255, 255, 255));
  36. g.FillRectangle(fillBrush, barX, barY, fillW, barH);
  37. }
  38. // Percentage text
  39. if (pct > 0)
  40. {
  41. using var sf = new StringFormat
  42. {
  43. Alignment = StringAlignment.Center,
  44. LineAlignment = StringAlignment.Center,
  45. Trimming = StringTrimming.None
  46. };
  47. using var textBrush = new SolidBrush(cellStyle.ForeColor);
  48. g.DrawString($"{pct}%", cellStyle.Font ?? SystemFonts.DefaultFont, textBrush, (RectangleF)cellBounds, sf);
  49. }
  50. }
  51. }
  52. internal sealed class DgvProgressBarColumn : DataGridViewColumn
  53. {
  54. public DgvProgressBarColumn() : base(new DgvProgressBarCell())
  55. {
  56. HeaderText = "Progress";
  57. Width = 80;
  58. MinimumWidth = 50;
  59. ReadOnly = true;
  60. SortMode = DataGridViewColumnSortMode.Automatic;
  61. }
  62. }
  63. }