using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Windows.Forms; using System.Drawing; using System.Data; using System.Text; namespace Mesnac.Controls.Feeding { /// /// 配方步骤 /// [ToolboxBitmap(typeof(System.Windows.Forms.DataGridView))] public partial class RecipeMixing : System.Windows.Forms.DataGridView { public RecipeMixing() { InitializeComponent(); this.InitMethod(); } public RecipeMixing(IContainer container) { container.Add(this); InitializeComponent(); this.InitMethod(); } /// /// 初始化方法 /// private void InitMethod() { this.SelectionMode = DataGridViewSelectionMode.FullRowSelect; //设置整行选择模式 this.MultiSelect = false; //禁用多行选项 this.ReadOnly = true; //禁止编辑 this.MouseDown += new MouseEventHandler(RecipeMixing_MouseDown); this.MouseLeave += new EventHandler(RecipeMixing_MouseLeave); this.MouseMove += new MouseEventHandler(RecipeMixing_MouseMove); } #region 自定义属性 private string _dataName; private DataTable _data; private DataTable _cloneData; //克隆数据源 private string _finishStatusName; private OnOffStatuses _finishStatus; private string _lengthMixerName; private int _lengthMixer; protected bool _bHaveAction; /// /// 是否有动画 /// public bool bHaveAction { get { return _bHaveAction; } set { _bHaveAction = value; } } /// /// 数据源动画属性 /// public string DataName { get { return _dataName; } set { _dataName = value; } } /// /// 一车结束标志动画属性 /// public string FinishStatusName { get { return _finishStatusName; } set { _finishStatusName = value; } } /// /// 当前步骤动画属性 /// public string LengthMixerName { get { return _lengthMixerName; } set { _lengthMixerName = value; } } /// /// 初始化数据 /// private void InitData() { try { if (this._cloneData != null) { this.DataSource = null; this._cloneData.Dispose(); this._cloneData = null; } if (this._data != null && this._data.Rows.Count > 0) { this._cloneData = this._data.Copy(); //this._cloneData.Columns[0].AllowDBNull = true; //for (int i = 0; i < this._cloneData.Rows.Count; i++) //{ // this._cloneData.Rows[i]["步骤"] = DBNull.Value; //} ////this._cloneData.Columns["步骤"].DataType = typeof(string); //int j=1; //for (int i = 0; i < this._cloneData.Rows.Count; i++) //{ // if (i + 1 == this._cloneData.Rows.Count) // { // this._cloneData.Rows[i]["步骤"] = j; // continue; // } // if (this._cloneData.Rows[i + 1]["条件"].ToString().Trim() == "同时执行") // { // continue; // } // this._cloneData.Rows[i]["步骤"] = j; // j++; //} DataRow row = this._cloneData.NewRow(); if (this._cloneData.Columns.Contains("步骤")) { row["步骤"] = 0; } if (this._cloneData.Columns.Contains("条件")) { row["条件"] = "准备中..."; } this._cloneData.Rows.InsertAt(row, 0); this.DataSource = this._cloneData; //绑定克隆数据源 //禁用排序 for (int i = 0; i < this.ColumnCount; i++) { this.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable; } //设定动作列宽 if (this.Columns.Contains("动作")) { this.Columns["动作"].Width = 80; } //清除选中行 this.ClearSelection(); //设定初始选中行 this.Rows[0].Selected = true; this.CurrentCell = this.Rows[0].Cells[0]; } } catch (Exception ex) { Console.WriteLine(ex.Message); } } /// /// 数据源 /// public DataTable Data { get { return _data; } set { _data = value; if (this._cloneData == null || this._cloneData.TableName != this._data.TableName) { this.InitData(); } } } /// /// 一车结束标志 /// public OnOffStatuses FinishStatus { get { return _finishStatus; } set { if ((int)value > 1) { this._finishStatus = OnOffStatuses.On; } else if ((int)value < 0) { this._finishStatus = OnOffStatuses.Off; } else { this._finishStatus = value; } //如果一车标志结束,则重新绑定数据源 if (this._finishStatus == OnOffStatuses.On) { this.InitData(); } } } /// /// 当前步骤 /// public int LengthMixer { get { return _lengthMixer; } set { _lengthMixer = value; if (this._cloneData != null) { if (this._cloneData.Columns.Contains("步骤")) { if (this._cloneData.Rows.Count > 0) { this._lengthMixer = FactSetp(_lengthMixer); for (int i = 0; i < this._cloneData.Rows.Count; i++) { DataRow row = this._cloneData.Rows[i]; object val = row["步骤"]; if (val != DBNull.Value) { if (Convert.ToInt32(val) == this._lengthMixer) { this.ClearSelection(); if (i < this.Rows.Count) { this.Rows[i].Selected = true; this.CurrentCell = this.Rows[i].Cells[0]; break; } else { if (this.Rows.Count > 0) { this.Rows[0].Selected = true; this.CurrentCell = this.Rows[0].Cells[0]; break; } } } } } } } } } } int FactSetp(int LengthMixer) { int factsetp = 0; int returnsetp = 0; for (int i = 1; i < this._cloneData.Rows.Count; i++) { if (this._cloneData.Rows.Count>(i+1) && this._cloneData.Rows[i + 1]["条件"].ToString().Trim() == "同时执行") { continue; } factsetp++; if (LengthMixer == factsetp) returnsetp = Convert.ToInt32(this._cloneData.Rows[i]["步骤"]); } return returnsetp; } #endregion #region 数据列 private string _mixingTimeName; private double _mixingTime; private string _mixingTempName; private double _mixingTemp; private string _mixingEnergyName; private double _mixingEnergy; private string _mixingPowerName; private double _mixingPower; private string _mixingPressName; private double _mixingPress; private string _mixingSpeedName; private double _mixingSpeed; /// /// 时间动画属性 /// public string MixingTimeName { get { return _mixingTimeName; } set { _mixingTimeName = value; } } /// /// 温度动画属性 /// public string MixingTempName { get { return _mixingTempName; } set { _mixingTempName = value; } } /// /// 能量动画属性 /// public string MixingEnergyName { get { return _mixingEnergyName; } set { _mixingEnergyName = value; } } /// /// 功率动画属性 /// public string MixingPowerName { get { return _mixingPowerName; } set { _mixingPowerName = value; } } /// /// 压力动画属性 /// public string MixingPressName { get { return _mixingPressName; } set { _mixingPressName = value; } } /// /// 转速动画属性 /// public string MixingSpeedName { get { return _mixingSpeedName; } set { _mixingSpeedName = value; } } /// /// 更新数据 /// /// private void UpdateData(string columnName, double value) { if (this._cloneData != null) { if (this._cloneData.Columns.Contains(columnName)) { if (this._cloneData.Columns[columnName].ReadOnly) { this._cloneData.Columns[columnName].ReadOnly = false; } if (this.CurrentRow != null) { if (this._cloneData.Rows.Count > 1 && this.CurrentRow.Index > 0 && this.CurrentRow.Index < this._cloneData.Rows.Count) { DataRow row = this._cloneData.Rows[this.CurrentRow.Index]; if (this._cloneData.Rows[this.CurrentRow.Index]["条件"].ToString().Trim() == "同时执行") { DataRow rowPre = this._cloneData.Rows[this.CurrentRow.Index - 1]; if (rowPre != null) rowPre[columnName] = value; } if (row != null) row[columnName] = value; } } } } } /// /// 时间 /// public double MixingTime { get { return _mixingTime; } set { _mixingTime = value; this.UpdateData("时间", value); } } /// /// 温度 /// public double MixingTemp { get { return _mixingTemp; } set { _mixingTemp = value; this.UpdateData("温度", value); } } /// /// 能量 /// public double MixingEnergy { get { return _mixingEnergy; } set { _mixingEnergy = value; this.UpdateData("能量", value); } } /// /// 功率 /// public double MixingPower { get { return _mixingPower; } set { _mixingPower = value; this.UpdateData("功率", value); } } /// /// 压力 /// public double MixingPress { get { return _mixingPress; } set { _mixingPress = value; this.UpdateData("压力", value); } } /// /// 转速 /// public double MixingSpeed { get { return _mixingSpeed; } set { _mixingSpeed = value; this.UpdateData("转速", value); } } #endregion #region 拖动 const int Band = 5; const int MinWidth = 50; const int MinHeight = 60; /// /// 鼠标在事件源的位置 /// private Point p = new Point(); private Point p1 = new Point(); private enum EnumMousePointPosition { MouseSizeNone = 0, //'无 MouseSizeRight = 1, //'拉伸右边框 MouseSizeLeft = 2, //'拉伸左边框 MouseSizeBottom = 3, //'拉伸下边框 MouseSizeTop = 4, //'拉伸上边框 MouseSizeTopLeft = 5, //'拉伸左上角 MouseSizeTopRight = 6, //'拉伸右上角 MouseSizeBottomLeft = 7, //'拉伸左下角 MouseSizeBottomRight = 8, //'拉伸右下角 MouseDrag = 9 // '鼠标拖动 } private EnumMousePointPosition m_MousePointPosition; void RecipeMixing_MouseDown(object sender, MouseEventArgs e) { p.X = e.X; p.Y = e.Y; p1.X = e.X; p1.Y = e.Y; } void RecipeMixing_MouseLeave(object sender, EventArgs e) { m_MousePointPosition = EnumMousePointPosition.MouseSizeNone; this.Cursor = Cursors.Arrow; } void RecipeMixing_MouseMove(object sender, MouseEventArgs e) { Control lCtrl = (sender as Control); if (e.Button == MouseButtons.Left) { switch (m_MousePointPosition) { case EnumMousePointPosition.MouseDrag: if (this.Left > 930) { this.Left = 929; return; } if (this.Top > 374) { this.Top = 373; return; } lCtrl.Left = lCtrl.Left + e.X - p.X; lCtrl.Top = lCtrl.Top + e.Y - p.Y; break; //case EnumMousePointPosition.MouseSizeRight: // lCtrl.Width = lCtrl.Width + e.X - p1.X; // p1.X = e.X; // p1.Y = e.Y; //记录光标拖动的当前点 // break; case EnumMousePointPosition.MouseSizeLeft: lCtrl.Left = lCtrl.Left + e.X - p.X; lCtrl.Width = lCtrl.Width - (e.X - p.X); break; case EnumMousePointPosition.MouseSizeBottom: lCtrl.Height = lCtrl.Height + e.Y - p1.Y; p1.X = e.X; p1.Y = e.Y; //'记录光标拖动的当前点 break; default: break; } if (lCtrl.Width < MinWidth) lCtrl.Width = MinWidth; if (lCtrl.Height < MinHeight) lCtrl.Height = MinHeight; } else { m_MousePointPosition = MousePointPosition(lCtrl.Size, e); //'判断光标的位置状态 switch (m_MousePointPosition) //'改变光标 { case EnumMousePointPosition.MouseSizeNone: this.Cursor = Cursors.Arrow; //'箭头 break; case EnumMousePointPosition.MouseDrag: this.Cursor = Cursors.SizeAll; //'四方向 break; case EnumMousePointPosition.MouseSizeRight: this.Cursor = Cursors.SizeWE; //东西 break; case EnumMousePointPosition.MouseSizeLeft: this.Cursor = Cursors.SizeWE; //东西 break; case EnumMousePointPosition.MouseSizeBottom: this.Cursor = Cursors.SizeNS; //'南北 break; default: break; } } } private EnumMousePointPosition MousePointPosition(Size size, System.Windows.Forms.MouseEventArgs e) { if ((e.X >= -1 * Band) | (e.X <= size.Width) | (e.Y >= -1 * Band) | (e.Y <= size.Height)) { if (e.X < Band) { if (e.Y < Band) { return EnumMousePointPosition.MouseSizeNone; } else { if (e.Y > -1 * Band + size.Height) { return EnumMousePointPosition.MouseSizeNone; } else { return EnumMousePointPosition.MouseSizeLeft; } } } else { if (e.X > -1 * Band + size.Width) { if (e.Y < Band) { return EnumMousePointPosition.MouseSizeNone; } else { if (e.Y > -1 * Band + size.Height) { return EnumMousePointPosition.MouseSizeNone; } else { return EnumMousePointPosition.MouseSizeRight; } } } else { if (e.Y < Band) { return EnumMousePointPosition.MouseSizeNone; } else { if (e.Y > -1 * Band + size.Height) { return EnumMousePointPosition.MouseSizeBottom; } else { return EnumMousePointPosition.MouseDrag; } } } } } else { return EnumMousePointPosition.MouseSizeNone; } } #endregion } }