You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

412 lines
13 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
namespace Mesnac.Controls.Feeding
{
[ToolboxBitmap(typeof(System.Windows.Forms.Label))]
public partial class AlarmLabel : Label
{
#region 字段定义
private string _dataName;
private DataTable _data;
private DataTable _cloneData;
private int _alarmWord1; //报警地址字1
private int _alarmWord2; //报警地址字2
private string _alarmName1; //报警变量1
private string _alarmName2; //报警变量2
private int _alarmValue1; //报警值1
private int _alarmValue2; //报警值2
private bool _isMuilt; //是否换行显示
private bool _isScroll = false; //是否滚动显示
private int _scrollStep = 20; //滚动步长
public string Msg1 = string.Empty;//第一块报警信息
public string Msg2 = string.Empty;//第二块报警信息
string split = ";"; //报警信息分割符
string enter = "\r\n"; //报警信息换行符
private string _controlAlarmValue; //控件报警信息
private string _controlAlarmName; //控件报警动画字段
#endregion
#region 构造方法
public AlarmLabel()
{
InitializeComponent();
this.AutoSize = true;
}
public AlarmLabel(IContainer container)
{
container.Add(this);
InitializeComponent();
}
#endregion
#region 属性
/// <summary>
/// 报警值1
/// </summary>
public int AlarmValue1
{
get { return _alarmValue1; }
set
{
if (this._alarmValue1 != value)
{
_alarmValue1 = value;
Msg1 = string.Empty;
int[] values = ParseBinaryValue(_alarmValue1, 16);
Msg1 = GetAlarmInfo1(values, 16, _alarmWord1);
//this.Text = Msg1 + Msg2;
string temp = String.Empty;
if (_isMuilt)
{
temp = String.IsNullOrEmpty(this._controlAlarmValue) ? String.Empty : this._controlAlarmValue.Replace(",", enter);
}
else
{
temp = String.IsNullOrEmpty(this._controlAlarmValue) ? String.Empty : this._controlAlarmValue.Replace(",", split);
}
this.Text = Msg1 + Msg2 + temp;
}
if (this._isScroll)
{
this.ProcessScroll();
}
}
}
/// <summary>
/// 报警值2
/// </summary>
public int AlarmValue2
{
get { return _alarmValue2; }
set
{
if (this._alarmValue2 != value)
{
_alarmValue2 = value;
Msg2 = string.Empty;
int[] values = ParseBinaryValue(_alarmValue2, 16);
Msg2 = GetAlarmInfo2(values, 16, _alarmWord2);
//this.Text = Msg1 + Msg2;
string temp = String.Empty;
if (_isMuilt)
{
temp = String.IsNullOrEmpty(this._controlAlarmValue) ? String.Empty : this._controlAlarmValue.Replace(",", enter);
}
else
{
temp = String.IsNullOrEmpty(this._controlAlarmValue) ? String.Empty : this._controlAlarmValue.Replace(",", split);
}
this.Text = Msg1 + Msg2 + temp;
}
if (this._isScroll)
{
this.ProcessScroll();
}
}
}
/// <summary>
/// 控件报警信息
/// </summary>
public string ControlAlarmValue
{
get { return _controlAlarmValue; }
set
{
if (_controlAlarmValue != value)
{
this._controlAlarmValue = value;
Msg2 = string.Empty;
int[] values = ParseBinaryValue(_alarmValue2, 16);
Msg2 = GetAlarmInfo2(values, 16, _alarmWord2);
//this.Text = Msg1 + Msg2;
string temp = String.Empty;
if (_isMuilt)
{
temp = String.IsNullOrEmpty(this._controlAlarmValue) ? String.Empty : this._controlAlarmValue.Replace(",", enter);
}
else
{
temp = String.IsNullOrEmpty(this._controlAlarmValue) ? String.Empty : this._controlAlarmValue.Replace(",", split);
}
this.Text = Msg1 + Msg2 + temp;
}
if (this._isScroll)
{
this.ProcessScroll();
}
}
}
/// <summary>
/// 报警地址字1
/// </summary>
public int AlarmWord1
{
get { return _alarmWord1; }
set { _alarmWord1 = value; }
}
/// <summary>
/// 报警地址字2
/// </summary>
public int AlarmWord2
{
get { return _alarmWord2; }
set { _alarmWord2 = value; }
}
/// <summary>
/// 是否滚动显示
/// </summary>
public bool IsScroll
{
get { return _isScroll; }
set { _isScroll = value; }
}
/// <summary>
/// 是否换行
/// </summary>
public bool IsMuilt
{
get { return _isMuilt; }
set { _isMuilt = value; }
}
/// <summary>
/// 滚动步长
/// </summary>
public int ScrollStep
{
get { return _scrollStep; }
set { _scrollStep = value; }
}
/// <summary>
/// 报警信息数据源
/// </summary>
public DataTable Data
{
get { return _data; }
set
{
_data = value;
//if (_data != null)
//{
// _cloneData = _data;
//}
if (this._cloneData == null || this._cloneData.TableName != this._data.TableName)
{
InitData();
}
}
}
#endregion
#region 动画属性
/// <summary>
/// 报警信息数据源
/// </summary>
public string DataName
{
get { return _dataName; }
set { _dataName = value; }
}
/// <summary>
/// 报警变量1
/// </summary>
public string AlarmName1
{
get { return _alarmName1; }
set { _alarmName1 = value; }
}
/// <summary>
/// 报警变量2
/// </summary>
public string AlarmName2
{
get { return _alarmName2; }
set { _alarmName2 = value; }
}
/// <summary>
/// 控件报警动画属性
/// </summary>
public string ControlAlarmName
{
get { return _controlAlarmName; }
set { _controlAlarmName = value; }
}
#endregion
#region 辅助方法
/// <summary>
/// 初始化数据
/// </summary>
public void InitData()
{
if (this._cloneData != null)
{
this._cloneData.Dispose();
this._cloneData = null;
}
if (this._data != null)
{
this._cloneData = this._data.Copy();
}
}
/// <summary>
/// 显示报警信息1
/// </summary>
/// <param name="values"></param>
/// <param name="WordLength"></param>
/// <param name="alarmBlock"></param>
private string GetAlarmInfo1(int[] values, int WordLength, int alarmBlock)
{
string tempMsg1 = String.Empty;
if (values == null)
return String.Empty;
for (int i = 0; i < WordLength; i++)
{
if (values[i].ToString() == "1")
{
string info = this.AlarmStr(alarmBlock, i);
if (_isMuilt)
{
//Msg1 = Msg1 + info + enter;
tempMsg1 += info + enter;
}
else
{
//Msg1 = Msg1 + this.AlarmStr(alarmBlock, i) + split;
tempMsg1 += info + split;
}
}
}
return tempMsg1;
//if (_isMuilt)
// this.Text = this.Text + Msg1;
//else
//{
// if (Msg1 != string.Empty)
// this.Text = this.Text + Msg1.Substring(0, Msg1.Length - 1);
// else
// this.Text = this.Text + Msg1;
//}
}
/// <summary>
/// 显示报警信息2
/// </summary>
/// <param name="values"></param>
/// <param name="WordLength"></param>
/// <param name="alarmBlock"></param>
private string GetAlarmInfo2(int[] values, int WordLength, int alarmBlock)
{
string tempMsg2 = String.Empty;
if (values == null)
return String.Empty;
for (int i = 0; i < WordLength; i++)
{
if (values[i].ToString() == "1")
{
string info = this.AlarmStr(alarmBlock, i);
if (_isMuilt)
{
//Msg2 = Msg2 + info + enter;
tempMsg2 += info + enter;
}
else
{
//Msg2 = Msg2 + this.AlarmStr(alarmBlock, i) + split;
tempMsg2 += info + split;
}
}
}
return tempMsg2;
//if (_isMuilt)
// this.Text = this.Text + Msg2;
//else
//{
// if (Msg2 != string.Empty)
// this.Text = this.Text + Msg2.Substring(0, Msg2.Length - 1);
// else
// this.Text = this.Text + Msg2;
//}
}
/// <summary>
/// 查找报警信息
/// </summary>
/// <param name="alarm_address"></param>
/// <param name="alarm_bit"></param>
/// <param name="logFlag"></param>
private string AlarmStr(int alarm_address, int alarm_bit)
{
if (_cloneData == null || _cloneData.Rows.Count == 0)
return string.Empty;
if (_cloneData.Select(string.Format("Alarm_address='{0}' and Alarm_bit='{1}'", alarm_address, alarm_bit)).Length==0)
{
return string.Empty;
}
DataRow[] rows = _cloneData.Select(string.Format("Alarm_address='{0}' and Alarm_bit='{1}'", alarm_address, alarm_bit));
return rows[0]["Alarm_Name"].ToString(); ;
}
/// <summary>
/// 解析二进制位
/// </summary>
/// <param name="x">要解析的变量</param>
/// <param name="len">解析的长度</param>
/// <returns>返回解析的二进制值数组</returns>
private int[] ParseBinaryValue(int x, int len)
{
int[] result = new int[len];
int b = 1;
for (int i = 0; i < len; i++)
{
result[i] = (x & b) == 0 ? 0 : 1;
b = b << 1;
}
return result;
}
/// <summary>
/// 处理滚动的方法
/// </summary>
private void ProcessScroll()
{
Control parent = this.Parent;
if (parent != null)
{
this.Left -= this._scrollStep;
if (this.Left < 10)
{
this.Left = parent.Width - 10;
}
}
}
#endregion
}
}