using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using WinFormControl; namespace ButcherFactory.WeightCount_ { public partial class DiscontWeightSetDialog : Form { BindingList list; public DiscontWeightSetDialog() { InitializeComponent(); this.Load += DiscontWeightSetDialog_Load; } void DiscontWeightSetDialog_Load(object sender, EventArgs e) { list = DiscontSetting.Load(); mGrid.DataSource = list; mGrid.Refresh(); BindTotalLbl(); } private void addBtn_Click(object sender, EventArgs e) { var name = nameBox.Text.Trim(); if (string.IsNullOrEmpty(name)) throw new Exception("请输入扣重项"); if (list.Any(x => x.Name == name)) throw new Exception("名称已存在"); decimal discont; if (!decimal.TryParse(discontBox.Text.Trim(), out discont)) throw new Exception("标准重输入不正确"); if (discont == 0) throw new Exception("标准值不能为0"); list.Insert(0, new DiscontSetting { Name = name, Standard = discont, Number = 0 }); mGrid.Refresh(); } private void okBtn_Click(object sender, EventArgs e) { DiscontSetting.Save(list); DialogResult = DialogResult.OK; } SolidBrush btnBrush = new SolidBrush(Color.LightBlue); private void mGrid_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { if (e.ColumnIndex >= 0 && e.RowIndex >= 0) { if (this.mGrid.Columns[e.ColumnIndex].HeaderText == "数量") { StringFormat sf = StringFormat.GenericDefault.Clone() as StringFormat;//设置重绘入单元格的字体样式 sf.FormatFlags = StringFormatFlags.DisplayFormatControl; sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Center; sf.Trimming = StringTrimming.EllipsisCharacter; e.PaintBackground(e.CellBounds, false);//重绘边框 var entity = this.mGrid.Rows[e.RowIndex].DataBoundItem as DiscontSetting; //设置要写入字体的大小 System.Drawing.Font myFont = new System.Drawing.Font("宋体", 15F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); SizeF sizeDel = e.Graphics.MeasureString("-", myFont); SizeF sizeMod = e.Graphics.MeasureString(entity.Number.ToString(), myFont); SizeF sizeLook = e.Graphics.MeasureString("+", myFont); float fDel = sizeDel.Width / (sizeDel.Width + sizeMod.Width + sizeLook.Width); // float fMod = sizeMod.Width / (sizeDel.Width + sizeMod.Width + sizeLook.Width); float fLook = sizeLook.Width / (sizeDel.Width + sizeMod.Width + sizeLook.Width); //设置每个“按钮的边界” RectangleF rectDel = new RectangleF(e.CellBounds.Left, e.CellBounds.Top, e.CellBounds.Width * fDel, e.CellBounds.Height); RectangleF rectMod = new RectangleF(rectDel.Right, e.CellBounds.Top, e.CellBounds.Width * fMod, e.CellBounds.Height); RectangleF rectLook = new RectangleF(rectMod.Right, e.CellBounds.Top, e.CellBounds.Width * fLook, e.CellBounds.Height); e.Graphics.FillRectangles(btnBrush, new RectangleF[] { rectDel }); e.Graphics.DrawString("-", myFont, Brushes.Black, rectDel, sf); //绘制“按钮” e.Graphics.DrawString(entity.Number.ToString(), myFont, Brushes.Black, rectMod, sf); e.Graphics.FillRectangles(btnBrush, new RectangleF[] { rectLook }); e.Graphics.DrawString("+", myFont, Brushes.Black, rectLook, sf); e.Handled = true; } } } private void mGrid_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) { if (e.ColumnIndex >= 0 && e.RowIndex >= 0) { if (this.mGrid.Columns[e.ColumnIndex].HeaderText == "删除") { var itemName = mGrid.CurrentRow.Cells[1].Value.ToString(); var tag = list.First(x => x.Name == itemName); list.Remove(tag); mGrid.Refresh(); BindTotalLbl(); return; } Point curPosition = e.Location;//当前鼠标在当前单元格中的坐标 if (this.mGrid.Columns[e.ColumnIndex].HeaderText == "数量") { Graphics g = this.mGrid.CreateGraphics(); System.Drawing.Font myFont = new System.Drawing.Font("宋体", 15F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); var entity = this.mGrid.Rows[e.RowIndex].DataBoundItem as DiscontSetting; SizeF sizeDel = g.MeasureString("-", myFont); SizeF sizeMod = g.MeasureString(entity.Number.ToString(), myFont); SizeF sizeLook = g.MeasureString("+", myFont); float fDel = sizeDel.Width / (sizeDel.Width + sizeMod.Width + sizeLook.Width); float fMod = sizeMod.Width / (sizeDel.Width + sizeMod.Width + sizeLook.Width); float fLook = sizeLook.Width / (sizeDel.Width + sizeMod.Width + sizeLook.Width); Rectangle rectTotal = new Rectangle(0, 0, this.mGrid.Columns[e.ColumnIndex].Width, this.mGrid.Rows[e.RowIndex].Height); RectangleF rectDel = new RectangleF(rectTotal.Left, rectTotal.Top, rectTotal.Width * fDel, rectTotal.Height); RectangleF rectMod = new RectangleF(rectDel.Right, rectTotal.Top, rectTotal.Width * fMod, rectTotal.Height); RectangleF rectLook = new RectangleF(rectMod.Right, rectTotal.Top, rectTotal.Width * fLook, rectTotal.Height); //判断当前鼠标在哪个“按钮”范围内 if (rectDel.Contains(curPosition))//- SubStract(entity); else if (rectMod.Contains(curPosition)) { var keyBoard = new NumberPad(); if (keyBoard.ShowDialog() == true) { var v = 0; if (int.TryParse(keyBoard.Result, out v) && v >= 0) { entity.Number = v; mGrid.Refresh(); BindTotalLbl(); } else throw new Exception("输入数量有误!"); } } else if (rectLook.Contains(curPosition))//+ Add(entity); } } } private void SubStract(DiscontSetting entity) { if (entity.Number == 0) return; entity.Number -= 1; mGrid.Refresh(); BindTotalLbl(); } private void Add(DiscontSetting entity) { entity.Number += 1; mGrid.Refresh(); BindTotalLbl(); } void BindTotalLbl() { totalLbl.Text = string.Format("{0:#0.##}", list.Sum(x => x.Weight)); } } }