protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); var g = e.Graphics; g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half; g.DrawRectangle(Pens.MediumVioletRed, 0.5f, 0.5f, this.Width - 1, this.Height - 1); int totalAvailableWidth = this.Width; int[] columnWidths = ColumnPositioning.CalculateColumnWidths(this.Columns, totalAvailableWidth, 10, out int totalOccupiedWidth, out int totalFillableWidth); Debug.Assert(columnWidths.Length == this.Columns.Length); const int columnHeight = 20; int scrollPosition = this.GetHScrollValue(); int x = -scrollPosition; for (int i = 0; i < this.Columns.Length; ++i) { // Column out of bounds (right). Don't need to draw any more columns. if (x > totalAvailableWidth) { break; } var col = this.Columns[i]; var columnWidth = columnWidths[i]; if (x + columnWidth < 0) { // Column out of bounds (left). Go to next column until we find one to draw. x += columnWidth; continue; } double effectiveFillPercentage = (double)(columnWidth - col.Width) / totalFillableWidth * 100.0; Brush fillBrush = Brushes.Gray; g.FillRectangle(fillBrush, x, 0, columnWidth, columnHeight); int borderWidth = i == this.Columns.Length - 1 ? columnWidth - 1 : columnWidth; g.DrawRectangle(Pens.Black, x + 0.5f, 0.5f, borderWidth, columnHeight - 1.0f); TextRenderer.DrawText(g, FormattableString.Invariant($"{col.Width}/{col.FillWeight} = {columnWidth} | {effectiveFillPercentage:F1} %"), this.Font, new Point(x, 0), Color.White); x += columnWidth; } var sb = new StringBuilder(); foreach (var width in columnWidths) { sb.AppendLine(width.ToString()); } sb.AppendLine($"Total available: {totalAvailableWidth}"); sb.AppendLine($"Total fillable: {totalFillableWidth}"); sb.AppendLine($"Total occupied: {totalOccupiedWidth}"); TextRenderer.DrawText(g, sb.ToString(), this.Font, new Point(0, columnHeight), Color.Black); }
protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); if (e.Button != MouseButtons.Left) { return; } int[] columnWidths = ColumnPositioning.CalculateColumnWidths(this.Columns, this.Width, 10, out _, out _); ColumnPositioning.HitTest(columnWidths, this.GetHScrollValue(), e.X, 5, out int columnIndex, out bool resizeHandle); if (resizeHandle) { this.resizeDragInfo = new ResizeDragInfo(this.Columns[columnIndex], e.X); } }
protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); if (this.resizeDragInfo.Active) { this.Cursor = Cursors.VSplit; ColumnPositioning.ResizeDrag(this.resizeDragInfo, e.X, 10); this.RecalculateScrollBar(); return; } int[] columnWidths = ColumnPositioning.CalculateColumnWidths(this.Columns, this.Width, 10, out _, out _); ColumnPositioning.HitTest(columnWidths, this.GetHScrollValue(), e.X, 5, out _, out bool resizeHandle); this.Cursor = resizeHandle ? Cursors.VSplit : Cursors.Default; }