WinForm表格指示灯自定义控件

    2

这是一个表格形式的指示灯自定义控件,以下是WinForm代码实现

  public partial class IndicatorGrid : Control
  {
      #region 私有字段
      private IndicatorState[,] indicators; // 二维数组表示行列指示灯状态
      //private Bitmap bufferBimap;
      private int cellWidth;
      private int cellHeight;
      private int rowHeight = 40;
      private int selectedRow = -1;
      private int selectedColumn = -1;
      private ContextMenuStrip contextMenuStrip;
      private ToolTip toolTip;
      private bool isAllowClosePopForm = true;
      #endregion



      #region 属性
      public int SelectedRow
      {
          get => selectedRow;          
      }
      public int SelectedColumn
      {
          get => selectedColumn;
      }
      public bool IsShowToolTip { get; set; } = true;
      public int MinIndicatorDiameter = 30; // 指示灯的最小直径       
      public int PopWidth { get; set; } = 400;
      public int PopHeight { get; set; } = 145;
      public bool IsFormCloseDelay { get; set; } = true;
      public int FormCloseDelay { get; set; } = 8;
      /// <summary>
      /// 标题添加模式,如果为true表示覆盖,false表示追加
      /// </summary>
      public bool PopFormTitleMode { get; set; } = false;
      /// <summary>
      /// 标题默认显示模式,如果为true表示行列显示
      /// </summary>
      public bool IsRowColumn { get; set; } = true;
      /// <summary>
      /// 显示指示灯数字
      /// </summary>
      public bool IsEnableNumber { get; set; } = true;
      /// <summary>
      /// 是否处于运行状态
      /// </summary>
      public bool IsRun { get; set; } = false;
      /// <summary>
      /// 操作是否为二级菜单
      /// </summary>
      public bool IsSecondLevelMenu { get; set; } = false;
      /// <summary>
      /// 是否自适应宽度
      /// </summary>
      public bool IsAutoWidth { get; set; } = false;
      /// <summary>
      /// 是否自适应宽度
      /// </summary>
      public bool IsAutoHeight { get; set; } = false;
      /// <summary>
      /// 行高是否自适应
      /// </summary>
      public bool IsAutoRowHeight { get; set; } = true;
      /// <summary>
      /// 指示灯是否为圆
      /// </summary>
      public bool IsCircle { get; set; } = true;
      /// <summary>
      /// 是否为默认宽高
      /// </summary>
      public bool IsDefaultWidthHeight { get; set; } = false;
      /// <summary>
      /// 行数目
      /// </summary>
      public int Rows { get; set; } = 8;
      /// <summary>
      /// 列数目
      /// </summary>
      public int Columns { get; set; } = 64;
      /// <summary>
      /// 单元格宽度
      /// </summary>
      public int CellWidth
      {
          get { return cellWidth; }
          set { cellWidth = value; }
      }
      /// <summary>
      /// 单元格高度
      /// </summary>
      public int CellHeight
      {
          get { return cellHeight; }
          set { cellHeight = value; }
      }
      /// <summary>
      /// 行高
      /// </summary>
      public int RowHeight
      {
          get
          {
              return rowHeight;
          }
          set
          {
              rowHeight = value;
          }
      }
      /// <summary>
      /// 边框颜色
      /// </summary>
      public Color BorderColor { get; set; } = Color.Black;
      /// <summary>
      /// 矩形圆角半径
      /// </summary>
      public int BorderRadius { get; set; } = 3; // 设置圆角的半径
      /// <summary>
      /// 字体颜色和大小
      /// </summary>
      public Font NumberFont { get; set; } = new Font("宋体", 8);
      /// <summary>
      /// 数字颜色
      /// </summary>
      public Color NumberColor { get; set; } = Color.Black;
      /// <summary>
      /// 合格颜色
      /// </summary>
      public Color PassColor { get; set; } = Color.Lime;
      /// <summary>
      /// 空闲颜色
      /// </summary>
      public Color IdlColor { get; set; } = Color.DeepPink;
      /// <summary>
      /// 异常颜色
      /// </summary>
      public Color ExceptionColor { get; set; } = Color.Red;
      /// <summary>
      /// 禁用颜色
      /// </summary>
      public Color DisabledColor { get; set; } = Color.Gray;
      /// <summary>
      /// 老化过程中颜色
      /// </summary>
      public Color BurnInColor { get; set; } = Color.Yellow;
      public ContextMenuStrip ContextMenuScript
      {
          get { return contextMenuStrip; }
          set
          {
              contextMenuStrip = value;
          }
      }
      /// <summary>
      /// 获取指示灯状态
      /// </summary>
      public IndicatorState[,] Indicators
      {
          get { return indicators; }
      }

      #endregion

      #region 启用禁用委托
      /// <summary>
      /// 启用指示灯委托
      /// </summary>
      public Action<LedEventArgs> EnableLedEvent;
      /// <summary>
      /// 禁用指示灯委托
      /// </summary>
      public Action<LedEventArgs> DisEnableLedEvent;
      /// <summary>
      /// 启用指示灯行委托
      /// </summary>
      public Action<LedEventArgs> EnableRowLedEvent;
      /// <summary>
      /// 禁用指示灯行委托
      /// </summary>
      public Action<LedEventArgs> DisEnableRowLedEvent;


      /// <summary>
      /// 启用指示灯列委托
      /// </summary>
      public Action<LedEventArgs> EnableColumnLedEvent;
      /// <summary>
      /// 禁用指示灯列委托
      /// </summary>
      public Action<LedEventArgs> DisEnableColumnLedEvent;

      /// <summary>
      /// 启用所有指示灯
      /// </summary>
      public Action<LedEventArgs> EnableAllLedEvent;
      /// <summary>
      /// 禁用所有指示灯
      /// </summary>
      public Action<LedEventArgs> DisEnableAllLedEvent;

      /// <summary>
      /// 指示灯刷新后都会执行的操作
      /// </summary>
      public Action<LedEventArgs> LedStatusUpdateEvent;
      #endregion


      #region 其他委托
      public Func<int,int,int,DataGridView> DataGridViewShowEvent;
      public Func<int,int,int,string> IndicatorTitleShowEvent;
      public Func<int,int,int,string> ToolTipShowEvent;
      #endregion

      #region 构造函数
      public IndicatorGrid(int rows, int columns)
      {
          this.DoubleBuffered = true;
          this.SetStyle(ControlStyles.DoubleBuffer |
                        ControlStyles.OptimizedDoubleBuffer |
                        ControlStyles.UserPaint |
                        ControlStyles.AllPaintingInWmPaint,
                        true);
          this.UpdateStyles();
          Rows = rows;
          Columns = columns;
          indicators = new IndicatorState[Rows, Columns];
          InitializeComponent();        
          InitializeControl();
         

      }
      public IndicatorGrid(int rows, int columns,Font font)
      {
          this.DoubleBuffered = true;
          this.SetStyle(ControlStyles.DoubleBuffer |
                        ControlStyles.OptimizedDoubleBuffer |
                        ControlStyles.UserPaint |
                        ControlStyles.AllPaintingInWmPaint,
                        true);
          this.UpdateStyles();
          NumberFont = font;
          Rows = rows;
          Columns = columns;
          indicators = new IndicatorState[Rows, Columns];
          InitializeComponent();
          InitializeControl();
      }
      public IndicatorGrid()
      {
          this.DoubleBuffered = true;
          this.SetStyle(ControlStyles.DoubleBuffer |
                        ControlStyles.OptimizedDoubleBuffer |
                        ControlStyles.UserPaint |
                        ControlStyles.AllPaintingInWmPaint,
                        true);
          this.UpdateStyles();
          indicators = new IndicatorState[Rows, Columns];
          InitializeComponent();
          InitializeControl();
      }
      public IndicatorGrid(int rows, int columns, Color NumberColor, Color PassColor, Color IdlColor, Color ExceptionColor, Color DisabledColor, Color BurnInColor)
      {
          this.DoubleBuffered = true;
          this.SetStyle(ControlStyles.DoubleBuffer |
                        ControlStyles.OptimizedDoubleBuffer |
                        ControlStyles.UserPaint |
                        ControlStyles.AllPaintingInWmPaint,
                        true);
          this.UpdateStyles();
          this.Rows = rows;
          this.Columns = columns;
          this.NumberColor = NumberColor;
          this.PassColor = PassColor;
          this.IdlColor = IdlColor;
          this.ExceptionColor = ExceptionColor;
          this.DisabledColor = DisabledColor;
          this.BurnInColor = BurnInColor;
          indicators = new IndicatorState[Rows, Columns];
          InitializeComponent();          
          InitializeControl();
      }
      #endregion

      #region 公共方法
      // 设置指示灯状态
      public void SetIndicator(int row, int column, IndicatorState status)
      {
          if (row >= 0 && row < Rows && column >= 0 && column < Columns)
          {
              if (indicators[row, column] != IndicatorState.Disable && indicators[row, column]!=IndicatorState.Hidden)
              {
                  if (indicators[row, column] != status)
                  {
                      indicators[row, column] = status;

                      int x = column * CellWidth;
                      int y = row * RowHeight;
                      this.Invalidate(new Rectangle(x, y, cellWidth, RowHeight)); // 重新绘制控件
                  }
              }
             
          }
      }
      /// <summary>
      /// 显示全部行
      /// </summary>       
      public void ShowIndicator(IndicatorState indicatorState = IndicatorState.Idl)
      {
          for (int row = 0; row < Rows; row++)
          {
              for (int col = 0; col < Columns; col++)
              {
                  indicators[row, col] = indicatorState;
                  int x = col * CellWidth;
                  int y = row * RowHeight;
                  this.Invalidate(new Rectangle(x, y, cellWidth, RowHeight)); // 重新绘制控件
              }
          }
      }
      /// <summary>
      /// 显示指定行
      /// </summary>
      /// <param name="row"></param>
      /// <param name="indicatorState"></param>
      public void ShowIndicator(int row, IndicatorState indicatorState = IndicatorState.Idl)
      {
          if (row >= 0 && row < Rows)
          {
              for (int col = 0; col < Columns; col++)
              {
                  indicators[row, col] = indicatorState;
                  int x = col * CellWidth;
                  int y = row * RowHeight;
                  this.Invalidate(new Rectangle(x, y, cellWidth, RowHeight)); // 重新绘制控件
              }              
          }
      }
      /// <summary>
      /// 显示指定行和列的指示灯,默认以空闲状态显示
      /// </summary>
      /// <param name="row"></param>
      /// <param name="column"></param>
      /// <param name="indicatorState"></param>
      public void ShowIndicator(int row,int column,IndicatorState indicatorState=IndicatorState.Idl)
      {
          if (row >= 0 && row < Rows && column >= 0 && column < Columns)
          {
              indicators[row, column] = indicatorState;
              int x = column * CellWidth;
              int y = row * RowHeight;
              this.Invalidate(new Rectangle(x, y, cellWidth, RowHeight)); // 重新绘制控件
          }
      }
      /// <summary>
      /// 隐藏全部行
      /// </summary>       
      public void HiddenIndicator()
      {
          for (int row = 0; row < Rows; row++)
          {
              for (int col = 0; col < Columns; col++)
              {
                  indicators[row, col] = IndicatorState.Hidden;
                  int x = col * CellWidth;
                  int y = row * RowHeight;
                  this.Invalidate(new Rectangle(x, y, cellWidth, RowHeight)); // 重新绘制控件
              }
          }
      }
      /// <summary>
      /// 隐藏指定行
      /// </summary>
      /// <param name="row"></param>
      /// <param name="indicatorState"></param>
      public void HiddenIndicator(int row)
      {
          if (row >= 0 && row < Rows)
          {
              for (int col = 0; col < Columns; col++)
              {
                  indicators[row, col] = IndicatorState.Hidden;
                  int x = col * CellWidth;
                  int y = row * RowHeight;
                  this.Invalidate(new Rectangle(x, y, cellWidth, RowHeight)); // 重新绘制控件
              }
          }
      }
      /// <summary>
      /// 隐藏指定行和列的指示灯
      /// </summary>
      /// <param name="row"></param>
      /// <param name="column"></param>
      /// <param name="indicatorState"></param>
      public void HiddenIndicator(int row, int column)
      {
          if (row >= 0 && row < Rows && column >= 0 && column < Columns)
          {
              indicators[row, column] = IndicatorState.Hidden;
              int x = column * CellWidth;
              int y = row * RowHeight;
              this.Invalidate(new Rectangle(x, y, cellWidth, RowHeight)); // 重新绘制控件
          }
      }
      #endregion

      #region 重写方法
      
      protected override void OnPaint(PaintEventArgs e)
      {
          base.OnPaint(e);
          var penBorder1 = new Pen(BorderColor, 2);
          var penBorder2 = new Pen(BorderColor, 1);
          var g = e.Graphics;
          {
              g.Clear(BackColor);

              for (int row = 0; row < Rows; row++)
              {
                  int y = row * rowHeight;

                  for (int col = 0; col < Columns; col++)
                  {
                      int x = col * cellWidth;
                      g.DrawLine(penBorder1, x, 0, x, this.Height);
                      g.DrawRectangle(penBorder1, 0, y, this.Width, rowHeight);
                      if (indicators[row, col] == IndicatorState.Hidden)
                      {
                          continue;
                      }
                      Rectangle cellRect = new Rectangle(x, y, cellWidth, rowHeight);
                      if (e.ClipRectangle.IntersectsWith(cellRect))
                      {

                          // 获取对应状态的画刷
                          Brush brush = GetIndicatorBrush(indicators[row, col]);
                          if (IsCircle)
                          {
                              // 计算指示灯的大小,确保不小于最小直径
                              int indicatorDiameter = Math.Max(MinIndicatorDiameter, Math.Min(cellWidth, cellHeight) / 2);
                              int indicatorX = x + (cellWidth - indicatorDiameter) / 2;
                              int indicatorY = y + (rowHeight - indicatorDiameter) / 2;
                              Rectangle indicatorRect = new Rectangle(indicatorX, indicatorY, indicatorDiameter, indicatorDiameter);
                              g.FillEllipse(brush, indicatorRect); // 绘制圆形指示灯
                              g.DrawEllipse(penBorder2, indicatorRect);
                          }
                          else
                          {
                              // 计算垂直偏移量以居中指示灯
                              int yOffset = (rowHeight - cellHeight) / 2;
                              // 绘制圆角矩形
                              GraphicsPath path = RoundedRectangle(x, y + yOffset, cellWidth, cellHeight, BorderRadius);
                              g.FillPath(brush, path);
                              g.DrawPath(penBorder2, path);
                          }

                          if (IsEnableNumber)
                          {
                              string numberText = (row * Columns + col + 1).ToString();
                              SizeF textSize = g.MeasureString(numberText, NumberFont);
                              PointF textLocation = new PointF(x + (cellWidth - textSize.Width) / 2, y + (rowHeight - textSize.Height) / 2);
                              g.DrawString(numberText, NumberFont, new SolidBrush(NumberColor), textLocation);
                          }
                      }
                  }
                  int lastX = Columns * cellWidth;
                  g.DrawLine(penBorder1, lastX, y, lastX, y + rowHeight);
              }
          }
      }

      protected override void OnMouseMove(MouseEventArgs e)
      {
          base.OnMouseMove(e);
          // 计算垂直偏移量
          int yOffset = (rowHeight - cellHeight) / 2;
          // 根据点击位置计算行列,考虑垂直偏移量
          int row = (e.Y - yOffset) / rowHeight;
          int col = e.X / cellWidth;        
          bool indicatorFound = CheckCenter(row, col, e.X,e.Y);           
          // 如果没有指示灯包含鼠标位置
          if (indicatorFound&&indicators[row, col] != IndicatorState.Hidden&&IsShowToolTip)
          {
              // 显示指示灯的提示信息
              int productId = (row == 0 ? (col + 1) : ((row * Columns + col + 1)));
              //通过调用委托来获取指定产品ID的显示数据
              string indicatorToolTip = ToolTipShowEvent?.Invoke(row, col, productId);
              if (string.IsNullOrEmpty(indicatorToolTip))
              {
                  indicatorToolTip = $"Indicator at Row {row + 1}, Column {col + 1}: {indicators[row, col]}";
              }
              toolTip.SetToolTip(this, indicatorToolTip);

          }
          else
          {
              toolTip.Hide(this);
          }
      }
   
      #endregion

      #region 私有方法
      private bool CheckCenter(int row,int col,int x,int y)
      {
          // 指示灯半径
          int indicatorDiameter = Math.Max(MinIndicatorDiameter, Math.Min(cellWidth, cellHeight) / 2);
          int indicatorRadius = indicatorDiameter / 2;
          // 计算指示灯中心的位置
          int centerX = col * cellWidth + cellWidth / 2;
          int centerY = row * rowHeight + rowHeight / 2;

          // 检查鼠标是否在指示灯内
          if (Math.Pow(x - centerX, 2) + Math.Pow(y - centerY, 2) <= Math.Pow(indicatorRadius, 2))
          {
              return true;
          }
          else
          {
              return false;
          }
      }
      private void InitializeControl()
      {
          toolTip = new ToolTip();
          toolTip.AutoPopDelay = 50000;
          toolTip.InitialDelay = 1000;
          toolTip.ReshowDelay = 1000;
          toolTip.ShowAlways = false;         
          this.Resize += IndicatorGrid_Resize;
          if (!IsDefaultWidthHeight)
          {
              this.Size = new Size(Columns * 30, Rows * 30); // 根据行列数设置控件大小
              cellWidth = this.Width / Columns;
              cellHeight = this.Height / Rows;              
          }
          else
          {
              if (CellHeight==0)
              {
                  CellHeight = 40;
              }
              if (CellWidth==0)
              {
                  CellWidth = 30;
              }
          }
          this.ContextMenuScript = new ContextMenuStrip();
          if (IsSecondLevelMenu)
          {
              ToolStripMenuItem toolStripMenuItem = new ToolStripMenuItem("指示灯操作");
              GetToolStripItemCollection(toolStripMenuItem.DropDownItems);
              this.contextMenuStrip.Items.Add(toolStripMenuItem);
          }
          else
          {
              GetToolStripItemCollection(this.ContextMenuScript.Items);            
          }
          
          this.MouseClick += IndicatorGrid_MouseClick;         
      }

      private void IndicatorGrid_Resize(object sender, EventArgs e)
      {
          if (IsAutoHeight)
          {
              CellHeight = this.Height / Rows;
          }
          if (IsAutoWidth)
          {
              CellWidth = this.Width / Columns;
          }
          if (IsAutoRowHeight)
          {
              RowHeight = this.Height / Rows;
          }
          if (IsAutoWidth||IsAutoHeight||IsAutoRowHeight)
          {
              this.Invalidate();
          }                      
         
      }

      private GraphicsPath RoundedRectangle(int x, int y, int width, int height, int radius)
      {
          GraphicsPath path = new GraphicsPath();
          path.StartFigure();
          path.AddArc(x, y, radius * 2, radius * 2, 180, 90);
          path.AddLine(x + radius, y, x + width - radius, y);
          path.AddArc(x + width - radius * 2, y, radius * 2, radius * 2, 270, 90);
          path.AddLine(x + width, y + radius, x + width, y + height - radius);
          path.AddArc(x + width - radius * 2, y + height - radius * 2, radius * 2, radius * 2, 0, 90);
          path.AddLine(x + width - radius, y + height, x + radius, y + height);
          path.AddArc(x, y + height - radius * 2, radius * 2, radius * 2, 90, 90);
          path.AddLine(x, y + height - radius, x, y + radius);
          path.CloseFigure();
          return path;
      }

      private Brush GetIndicatorBrush(IndicatorState state)
      {
          switch (state)
          {
              case IndicatorState.BurnIn:
                  return new SolidBrush(BurnInColor);
              case IndicatorState.Pass:
                  return new SolidBrush(PassColor);
              case IndicatorState.Disable:
                  return new SolidBrush(DisabledColor);
              case IndicatorState.Exception:
                  return new SolidBrush(ExceptionColor);
              case IndicatorState.Idl:
                  return new SolidBrush(IdlColor);
              default:
                  return null;
          }
      }
      private void GetToolStripItemCollection(ToolStripItemCollection toolStripItemCollection)
      {
          toolStripItemCollection.Add("启用", null, EnableIndicatorMenuItem_Click);
          toolStripItemCollection.Add("禁用", null, DisableIndicatorMenuItem_Click);
          toolStripItemCollection.Add("启用行", null, EnableRowMenuItem_Click);
          toolStripItemCollection.Add("禁用行", null, DisableRowMenuItem_Click);
          toolStripItemCollection.Add("启用列", null, EnableColumnMenuItem_Click);
          toolStripItemCollection.Add("禁用列", null, DisableColumnMenuItem_Click);
          toolStripItemCollection.Add("启用全部", null, EnableAllMenuItem_Click);
          toolStripItemCollection.Add("禁用全部", null, DisableAllMenuItem_Click);
      }
      /// <summary>
      /// 鼠标点击时记录鼠标点击的位置
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>
      private void IndicatorGrid_MouseClick(object sender, MouseEventArgs e)
      {
          if (e.Button == MouseButtons.Right)
          {
              // 计算垂直偏移量
              int yOffset = (rowHeight - cellHeight) / 2;
              // 根据点击位置计算行列,考虑垂直偏移量
              int row = (e.Y - yOffset) / rowHeight;
              int column = e.X / cellWidth;
              if (row >= 0 && row < Rows && column >= 0 && column < Columns)
              {
                  selectedRow = row;
                  selectedColumn = column;
                  contextMenuStrip.Show(this, e.Location);
              }
          }           
          else if (e.Button==MouseButtons.Left)
          {                
              // 计算垂直偏移量
              int yOffset = (rowHeight - cellHeight) / 2;
              // 根据点击位置计算行列,考虑垂直偏移量
              int row = (e.Y - yOffset) / rowHeight;
              int column = e.X / cellWidth;
              bool indicatorFound = CheckCenter(row, column, e.X, e.Y);
              if (indicatorFound && indicators[row,column]!=IndicatorState.Hidden)
              {
                  if (row >= 0 && row < Rows && column >= 0 && column < Columns)
                  {
                      int productId = (row == 0 ? (column + 1) : ((row * Columns + column + 1)));
                      DataGridView dataGridView = DataGridViewShowEvent?.Invoke(row,column,productId);
                      string indicatorTitle = IndicatorTitleShowEvent?.Invoke(row, column, productId);
                      ShowPopupWindowAtIndicator(row, column, indicatorTitle, dataGridView);
                  }
              }               
          }
      }

      private void ShowPopupWindowAtIndicator(int row, int col,string indicatorTitle,DataGridView dataGridView)
      {
          // 计算指示灯中心的相对坐标
          int centerX = col * cellWidth + cellWidth / 2;
          int centerY = row * rowHeight + rowHeight / 2;
          Point indicatorCenter = new Point(centerX, centerY);
          // 将相对坐标转换为屏幕坐标
          Point screenLocation = this.PointToScreen(indicatorCenter);
          // 创建并显示弹出窗口
          Form popupWindowForm = new Form();
          popupWindowForm.Text = IsRowColumn ? $"第{row + 1}层 第{col + 1}个":
              $" 位置编号{ (row==0?(col+1):((row*Columns+col+1)))}";
          popupWindowForm.MaximizeBox = false;
          popupWindowForm.Width = PopWidth;
          popupWindowForm.Height = PopHeight;
          isAllowClosePopForm = true;
          popupWindowForm.MouseLeave += PopupWindowForm_MouseLeave;
          popupWindowForm.MouseMove += PopupWindowForm_MouseMove;
          popupWindowForm.Load += PopupWindowForm_Load;
          popupWindowForm.StartPosition = FormStartPosition.Manual;
          //获取屏幕工作区域的大小
          Rectangle screen = Screen.FromControl(this).WorkingArea;
          if (screenLocation.X+popupWindowForm.Width>screen.Right)
          {
              //将窗口左移
              screenLocation.X = screenLocation.X - popupWindowForm.Width;
          }
          if (screenLocation.Y+popupWindowForm.Height>screen.Bottom)
          {
              //将窗口上移
              screenLocation.Y = screenLocation.Y - popupWindowForm.Height;
          }

          if (!string.IsNullOrEmpty(indicatorTitle))
          {
              if (PopFormTitleMode)
              {
                  popupWindowForm.Text = indicatorTitle;
              }
              else
              {
                  popupWindowForm.Text = popupWindowForm.Text + " " + indicatorTitle;
              }
          }
          popupWindowForm.Controls.Add(dataGridView);
          popupWindowForm.Location = screenLocation;
          popupWindowForm.ShowDialog();
      }

      private void PopupWindowForm_MouseMove(object sender, MouseEventArgs e)
      {
          if (IsFormCloseDelay)
          {
              isAllowClosePopForm = false;
          }
      }

      private async void PopupWindowForm_Load(object sender, EventArgs e)
      {
          if (IsFormCloseDelay)
          {
              await Task.Delay(1000 * FormCloseDelay);
              if (isAllowClosePopForm)
              {
                  try
                  {
                      Form form = sender as Form;
                      if (form != null)
                      {
                          form.Close();
                      }
                  }
                  catch (Exception)
                  {

                      
                  }
              }
              isAllowClosePopForm = true;
            
          }
      }

      private void PopupWindowForm_MouseLeave(object sender, EventArgs e)
      {
          Form form = sender as Form;
          if (form!=null)
          {
              isAllowClosePopForm = false;
              form.Close();
          }
      }

      /// <summary>
      /// 启用
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>
      private void EnableIndicatorMenuItem_Click(object sender, EventArgs e)
      {
          if (selectedRow >= 0 && selectedRow < Rows && selectedColumn >= 0 && selectedColumn < Columns)
          {
              if (indicators[selectedRow, selectedColumn] == IndicatorState.Hidden)
              {
                  return;
              }
              indicators[selectedRow, selectedColumn] = IsRun ? IndicatorState.BurnIn : IndicatorState.Idl;
              int x = selectedColumn * CellWidth;
              int y = selectedRow * RowHeight;
              this.Invalidate(new Rectangle(x, y, cellWidth, RowHeight)); // 重新绘制控件
              if (LedStatusUpdateEvent!=null)
              {
                  LedStatusUpdateEvent.Invoke(new LedEventArgs() { rowIndex = selectedRow, columnIndex = selectedColumn, state = indicators[selectedRow, selectedColumn] });
              }
              if (EnableLedEvent!=null)
              {
                  EnableLedEvent.Invoke(new LedEventArgs() { rowIndex=selectedRow,columnIndex=selectedColumn,state = indicators[selectedRow, selectedColumn] });
              }
          }
      }
      /// <summary>
      /// 禁用
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>
      private void DisableIndicatorMenuItem_Click(object sender, EventArgs e)
      {
          if (selectedRow >= 0 && selectedRow < Rows && selectedColumn >= 0 && selectedColumn < Columns)
          {
              if (indicators[selectedRow, selectedColumn] == IndicatorState.Hidden)
              {
                  return;
              }
              indicators[selectedRow, selectedColumn] = IndicatorState.Disable;
              int x = selectedColumn * CellWidth;
              int y = selectedRow * RowHeight;
              this.Invalidate(new Rectangle(x, y, cellWidth, RowHeight)); // 重新绘制控件
              if (LedStatusUpdateEvent != null)
              {
                  LedStatusUpdateEvent.Invoke(new LedEventArgs() { rowIndex = selectedRow, columnIndex = selectedColumn, state = indicators[selectedRow, selectedColumn] });
              }  
              if (DisEnableLedEvent != null)
              {
                  DisEnableLedEvent.Invoke(new LedEventArgs() { rowIndex = selectedRow, columnIndex = selectedColumn, state = indicators[selectedRow, selectedColumn] });
              }
          }
      }
      /// <summary>
      /// 启用行
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>
      private void EnableRowMenuItem_Click(object sender, EventArgs e)
      {
          if (selectedRow >= 0 && selectedRow < Rows)
          {
              for (int col = 0; col < Columns; col++)
              {
                  if (indicators[selectedRow, col] == IndicatorState.Hidden)
                  {
                      continue;
                  }
                  indicators[selectedRow, col] = IsRun ? IndicatorState.BurnIn : IndicatorState.Idl;
                  int x = col * CellWidth;
                  int y = selectedRow * RowHeight;
                  this.Invalidate(new Rectangle(x, y, cellWidth, RowHeight)); // 重新绘制控件
              }
              if (LedStatusUpdateEvent != null)
              {
                  LedStatusUpdateEvent.Invoke(new LedEventArgs() { rowIndex = selectedRow, isOnlyRow =true,columnIndex = selectedColumn, state = indicators[selectedRow, selectedColumn] });
              } 
              if (EnableRowLedEvent != null)
              {
                  EnableRowLedEvent.Invoke(new LedEventArgs() { rowIndex = selectedRow, isOnlyRow = true, columnIndex = selectedColumn, state = indicators[selectedRow, selectedColumn] });
              }
          }
      }
      /// <summary>
      /// 禁用行
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>
      private void DisableRowMenuItem_Click(object sender, EventArgs e)
      {
          if (selectedRow >= 0 && selectedRow < Rows)
          {
              for (int col = 0; col < Columns; col++)
              {
                  if (indicators[selectedRow, col] == IndicatorState.Hidden)
                  {
                      continue;
                  }
                  indicators[selectedRow, col] = IndicatorState.Disable;
                  int x = col * CellWidth;
                  int y = selectedRow * RowHeight;
                  this.Invalidate(new Rectangle(x, y, cellWidth, RowHeight)); // 重新绘制控件
              }
              if (LedStatusUpdateEvent != null)
              {
                  LedStatusUpdateEvent.Invoke(new LedEventArgs() { rowIndex = selectedRow, isOnlyRow = true, columnIndex = selectedColumn, state = indicators[selectedRow, selectedColumn] });
              }
              if (DisEnableRowLedEvent != null)
              {
                  DisEnableRowLedEvent.Invoke(new LedEventArgs() { rowIndex = selectedRow, isOnlyRow = true, columnIndex = selectedColumn, state = indicators[selectedRow, selectedColumn] });
              }
          }
      }
      /// <summary>
      /// 禁用全部
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>
      private void DisableAllMenuItem_Click(object sender, EventArgs e)
      {
          for (int row = 0; row < Rows; row++)
          {
              for (int col = 0; col < Columns; col++)
              {
                  if (indicators[row, col] == IndicatorState.Hidden)
                  {
                      continue;
                  }
                  indicators[row, col] = IndicatorState.Disable;
                  int x = col * CellWidth;
                  int y = row * RowHeight;
                  this.Invalidate(new Rectangle(x, y, cellWidth, RowHeight)); // 重新绘制控件
              }
          }
          if (LedStatusUpdateEvent != null)
          {
              LedStatusUpdateEvent.Invoke(new LedEventArgs() { rowIndex = selectedRow,isOnlyColumn=true, isOnlyRow = true, columnIndex = selectedColumn, state = indicators[selectedRow, selectedColumn] });
          }    
          if (DisEnableAllLedEvent != null)
          {
              DisEnableAllLedEvent.Invoke(new LedEventArgs() { rowIndex = selectedRow, isOnlyColumn = true, isOnlyRow = true, columnIndex = selectedColumn, state = indicators[selectedRow, selectedColumn] });
          }
          //this.Invalidate();
      }
      /// <summary>
      /// 启用全部
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>
      private void EnableAllMenuItem_Click(object sender, EventArgs e)
      {
          for (int row = 0; row < Rows; row++)
          {
              for (int col = 0; col < Columns; col++)
              {
                  if (indicators[row, col] == IndicatorState.Hidden)
                  {
                      continue;
                  }
                  indicators[row, col] = IsRun ? IndicatorState.BurnIn : IndicatorState.Idl;
                  int x = col * CellWidth;
                  int y = row * RowHeight;
                  this.Invalidate(new Rectangle(x, y, cellWidth, RowHeight)); // 重新绘制控件
              }
          }
          if (LedStatusUpdateEvent != null)
          {
              LedStatusUpdateEvent.Invoke(new LedEventArgs() { rowIndex = selectedRow, isOnlyColumn = true, isOnlyRow = true, columnIndex = selectedColumn, state = indicators[selectedRow, selectedColumn] });
          } 
          if (EnableAllLedEvent != null)
          {
              EnableAllLedEvent.Invoke(new LedEventArgs() { rowIndex = selectedRow, isOnlyColumn = true, isOnlyRow = true, columnIndex = selectedColumn, state = indicators[selectedRow, selectedColumn] });
          }
          //this.Invalidate();

      }
      /// <summary>
      /// 禁用列
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>
      private void DisableColumnMenuItem_Click(object sender, EventArgs e)
      {
          if (selectedColumn >= 0 && selectedColumn < Columns)
          {
              for (int row = 0; row < Rows; row++)
              {
                  if (indicators[row, selectedColumn] == IndicatorState.Hidden)
                  {
                      continue;
                  }
                  indicators[row, selectedColumn] = IndicatorState.Disable;
                  int x = selectedColumn * CellWidth;
                  int y = row * RowHeight;
                  this.Invalidate(new Rectangle(x, y, cellWidth, RowHeight)); // 重新绘制控件
              }             
              if (LedStatusUpdateEvent != null)
              {
                  LedStatusUpdateEvent.Invoke(new LedEventArgs() { rowIndex = selectedRow, isOnlyColumn = true, columnIndex = selectedColumn, state = indicators[selectedRow, selectedColumn] });
              } 
              if (DisEnableColumnLedEvent != null)
              {
                  DisEnableColumnLedEvent.Invoke(new LedEventArgs() { rowIndex = selectedRow, isOnlyColumn = true, columnIndex = selectedColumn, state = indicators[selectedRow, selectedColumn] });
              }
          }
      }
      /// <summary>
      /// 启用列
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>
      private void EnableColumnMenuItem_Click(object sender, EventArgs e)
      {
          if (selectedColumn >= 0 && selectedColumn < Columns)
          {
              for (int row = 0; row < Rows; row++)
              {
                  if (indicators[row, selectedColumn] == IndicatorState.Hidden)
                  {
                      continue;
                  }
                  indicators[row, selectedColumn] = IsRun ? IndicatorState.BurnIn : IndicatorState.Idl;
                  int x = selectedColumn * CellWidth;
                  int y = row * RowHeight;
                  this.Invalidate(new Rectangle(x, y, cellWidth, RowHeight)); // 重新绘制控件
              }
              if (LedStatusUpdateEvent != null)
              {
                  LedStatusUpdateEvent.Invoke(new LedEventArgs() { rowIndex = selectedRow, isOnlyColumn = true, columnIndex = selectedColumn, state = indicators[selectedRow, selectedColumn] });
              }
              if (EnableColumnLedEvent != null)
              {
                  EnableColumnLedEvent.Invoke(new LedEventArgs() { rowIndex = selectedRow, isOnlyColumn = true, columnIndex = selectedColumn, state = indicators[selectedRow, selectedColumn] });
              }
          }
      }
      #endregion

  }

Comments | 0 评论
消息盒子

# 暂无消息 #

只显示最新10条未读和已读信息