diff --git a/CutToLength/BinLayoutView.cs b/CutToLength/BinLayoutView.cs new file mode 100644 index 0000000..f0572e6 --- /dev/null +++ b/CutToLength/BinLayoutView.cs @@ -0,0 +1,73 @@ +using System.Drawing; +using System.Drawing.Drawing2D; +using System.Windows.Forms; + +namespace CutToLength +{ + class BinLayoutView : Control + { + public Bin Bin { get; set; } + + private const int BorderPixels = 15; + private const int BinHeightPixels = 100; + + private readonly HatchBrush hBrush = new HatchBrush(HatchStyle.DiagonalCross, Color.Pink, Color.Transparent); + + public BinLayoutView() + { + SetStyle(ControlStyles.ResizeRedraw | ControlStyles.OptimizedDoubleBuffer, true); + } + + protected override void OnPaint(PaintEventArgs e) + { + base.OnPaint(e); + + if (Bin == null) + return; + + var displayWidth = Width - BorderPixels * 2.0f; + var maxHeight = Height - BorderPixels * 2.0f; + + if (displayWidth <= 0) return; + if (maxHeight <= 0) return; + + var displayHeight = maxHeight < BinHeightPixels ? maxHeight : BinHeightPixels; + + var x = (Width - displayWidth) / 2.0f; + var y = (Height - displayHeight) / 2.0f; + + var rect = new RectangleF(x, y, displayWidth, displayHeight); + + var id = 1; + var scale = displayWidth / (float)Bin.Length; + + if (Bin != null) + { + for (int i = 0; i < Bin.Items.Count; i++) + { + var item = Bin.Items[i]; + + var w = item.Length / Bin.Length * displayWidth; + var r = new RectangleF(x, y, (float)w, displayHeight); + + e.Graphics.DrawRectangle(Pens.Blue, r.X, r.Y, r.Width, r.Height); + e.Graphics.DrawString(id++.ToString(), Font, Brushes.Blue, r, new StringFormat + { + Alignment = StringAlignment.Center, + LineAlignment = StringAlignment.Center + }); + + x += (float)item.Length * scale; + + if (i < Bin.Items.Count - 1) + x += (float)Bin.Spacing; + } + } + + var scrapRect = new RectangleF(x + 1, y, (float)Bin.RemainingLength * scale, displayHeight); + + e.Graphics.FillRectangle(hBrush, scrapRect); + e.Graphics.DrawRectangle(Pens.Black, rect.X, rect.Y, rect.Width, rect.Height); + } + } +} diff --git a/CutToLength/Class1.cs b/CutToLength/Class1.cs deleted file mode 100644 index e62235d..0000000 --- a/CutToLength/Class1.cs +++ /dev/null @@ -1,70 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; - -namespace CutToLength -{ - class Class1 : Control - { - public Bin Bin { get; set; } - - private const int BorderPixels = 15; - private const int BinHeightPixels = 100; - - public Class1() - { - SetStyle(ControlStyles.ResizeRedraw | ControlStyles.OptimizedDoubleBuffer, true); - } - - protected override void OnPaint(PaintEventArgs e) - { - base.OnPaint(e); - - var displayWidth = Width - BorderPixels * 2.0f; - var maxHeight = Height - BorderPixels * 2.0f; - - if (displayWidth <= 0) return; - if (maxHeight <= 0) return; - - var displayHeight = maxHeight < BinHeightPixels ? maxHeight : BinHeightPixels; - - var x = (Width - displayWidth) / 2.0f; - var y = (Height - displayHeight) / 2.0f; - - var rect = new RectangleF(x, y, displayWidth, displayHeight); - - var drawX = (float)x; - var id = 1; - var scale = displayWidth / Bin.Length; - var totalLength = 0.0; - - if (Bin != null) - { - foreach (var item in Bin.Items) - { - var xc = item.Length - var w = item.Length / Bin.Length * displayWidth; - var r = new RectangleF(drawX, y, (float)w, displayHeight); - - e.Graphics.DrawRectangle(Pens.Blue, r.X, r.Y, r.Width, r.Height); - e.Graphics.DrawString(id++.ToString(), Font, Brushes.Blue, r, new StringFormat - { - Alignment = StringAlignment.Center, - LineAlignment = StringAlignment.Center - }); - - drawX = r.Right; - } - } - - var scrapRect = new RectangleF(drawX, y, rect.Right - drawX, displayHeight); - - e.Graphics.FillRectangle(Brushes.Red, scrapRect); - e.Graphics.DrawRectangle(Pens.Black, rect.X, rect.Y, rect.Width, rect.Height); - } - } -} diff --git a/CutToLength/CutToLength.csproj b/CutToLength/CutToLength.csproj index c13c9db..53a4c8b 100644 --- a/CutToLength/CutToLength.csproj +++ b/CutToLength/CutToLength.csproj @@ -50,7 +50,7 @@ - + Component diff --git a/CutToLength/Form2.Designer.cs b/CutToLength/Form2.Designer.cs index db7e805..b950bf4 100644 --- a/CutToLength/Form2.Designer.cs +++ b/CutToLength/Form2.Designer.cs @@ -36,7 +36,7 @@ this.remainingLengthDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.utilizationDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.binBindingSource = new System.Windows.Forms.BindingSource(this.components); - this.class11 = new CutToLength.Class1(); + this.class11 = new CutToLength.BinLayoutView(); this.splitContainer1 = new System.Windows.Forms.SplitContainer(); this.uIItemBindingSource = new System.Windows.Forms.BindingSource(this.components); ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit(); @@ -160,7 +160,7 @@ #endregion private System.Windows.Forms.DataGridView dataGridView1; - private Class1 class11; + private BinLayoutView class11; private System.Windows.Forms.DataGridViewTextBoxColumn spacingDataGridViewTextBoxColumn; private System.Windows.Forms.DataGridViewTextBoxColumn lengthDataGridViewTextBoxColumn; private System.Windows.Forms.DataGridViewTextBoxColumn usedLengthDataGridViewTextBoxColumn;