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.

842 lines
29 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Text.RegularExpressions;
using Mesnac.Controls.Base;
namespace Mesnac.Controls.Compressor
{
[ToolboxBitmap(typeof(System.Windows.Forms.TextBox))]
public partial class PlcTextBox :Mesnac.Controls.Default.TextBox, IBaseControl
{
public enum RegexType
{
,
,
,
,
Email,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
线,
Url,
QQ,
,
IP,
,
,
,
,
,
,
Am,
}
#region 命令
private const int WM_ERASEBKGND = 0x0014;
private const int WM_PAINT = 0xF;
private const uint WM_NCPAINT = 0x0085;
#endregion
#region 字段
private Color _borderColor = Color.FromArgb(107, 191, 111);
private string _waterMarkText;
private Color _waterMarkTextColor = Color.DarkGray;
private float _borderWeight = 1;
private RegexType _controlType = RegexType.;
private string _controlTypeText = "默认";
private int _maxValue;
private int _minValue;
private bool _isEmpty = true;//允许
private bool _isValid = true;//保存有效性
private bool _mcVisible = true; //保存可见性
private bool _mcEnabled = true; //保存可用性
private Color r = Color.FromArgb(107, 191, 111);
#endregion
#region 属性
[Description("水印文字"), Category("外观")]
public string WaterMarkText
{
get { return _waterMarkText; }
set
{
_waterMarkText = value;
base.Invalidate();
}
}
[DefaultValue(typeof(Color), "DarkGray"), Description("水印文字颜色")]
public Color WaterMarkTextColor
{
get { return _waterMarkTextColor; }
set
{
_waterMarkTextColor = value;
base.Invalidate();
}
}
[DefaultValue(typeof(Color), "107, 191, 111"), Description("边框颜色"), Category("外观")]
public Color BorderColor
{
get { return _borderColor; }
set
{
_borderColor = value;
base.Invalidate();
}
}
[DefaultValue(typeof(float), "1"), Description("边框粗细"), Category("外观")]
public float BorderWeight
{
get { return _borderWeight; }
set
{
_borderWeight = value;
base.Invalidate();
}
}
[DefaultValue(RegexType.默认), Description("文本框类型"), Category("外观")]
public RegexType ControlType
{
get { return _controlType; }
set
{
_controlType = value;
this.ShowDescription(value);
base.Invalidate();
}
}
[DefaultValue("默认"), Description("控件验证描述"), Category("行为")]
public string ControlTypeText
{
get { return _controlTypeText; }
}
[DefaultValue(null), Description("最大值"), Category("数据")]
public int MaxValue
{
get { return _maxValue; }
set { _maxValue = value; }
}
[DefaultValue(null), Description("最小值"), Category("数据")]
public int MinValue
{
get { return _minValue; }
set { _minValue = value; }
}
[DefaultValue(true), Description("验证是否通过"), Category("行为")]
public bool InvalidPass
{
get { return _isValid; }
}
[Description("是否允许为空"), Category("行为")]
public bool IsEmpty
{
get { return _isEmpty; }
set { _isEmpty = value; }
}
#endregion
public PlcTextBox()
{
InitializeComponent();
this._maxValue = 65535;
this._minValue = -65535;
}
public PlcTextBox(IContainer container)
{
container.Add(this);
InitializeComponent();
}
protected override Control.ControlCollection CreateControlsInstance()
{
r = this.BorderColor;
this.BorderStyle = BorderStyle.FixedSingle;
return base.CreateControlsInstance();
}
#region 事件
private List<DesignAction> _keyDownActionList = new List<DesignAction>();
public List<DesignAction> KeyDownActionList
{
get { return _keyDownActionList; }
set { _keyDownActionList = value; }
}
private List<DesignAction> _clickActionList = new List<DesignAction>();
public List<DesignAction> ClickActionList
{
get { return _clickActionList; }
set { _clickActionList = value; }
}
private List<DesignAction> _textChangeList = new List<DesignAction>();
public List<DesignAction> TextChangeList
{
get { return _textChangeList; }
set { _textChangeList = value; }
}
#endregion
#region 验证
private void ShowDescription(RegexType value)
{
switch (value)
{
case RegexType.:
this._controlTypeText = "默认";
break;
case RegexType.:
this._controlTypeText = "数字";
break;
case RegexType.:
this._controlTypeText = "汉字";
break;
case RegexType.:
this._controlTypeText = "邮政编码";
break;
case RegexType.Email:
this._controlTypeText = "电子邮件";
break;
case RegexType.:
this._controlTypeText = "座机电话号码";
break;
case RegexType.:
this._controlTypeText = "中国电话号码";
break;
case RegexType.:
this._controlTypeText = "手机号码";
break;
case RegexType.:
this._controlTypeText = "整数";
break;
case RegexType.:
this._controlTypeText = "负整数";
break;
case RegexType.:
this._controlTypeText = "浮点数";
break;
case RegexType.:
this._controlTypeText = "非负浮点数";
break;
case RegexType.:
this._controlTypeText = "正浮点数";
break;
case RegexType.:
this._controlTypeText = "非正浮点数";
break;
case RegexType.:
this._controlTypeText = "负浮点数";
break;
case RegexType.:
this._controlTypeText = "英文字符";
break;
case RegexType.:
this._controlTypeText = "大写英文字符";
break;
case RegexType.:
this._controlTypeText = "小写英文字符";
break;
case RegexType.:
this._controlTypeText = "数字和英文字母";
break;
case RegexType.线:
this._controlTypeText = "数字、英文字母或下划线";
break;
case RegexType.Url:
this._controlTypeText = "URL";
break;
case RegexType.QQ:
this._controlTypeText = "QQ";
break;
case RegexType.:
this._controlTypeText = "身份证";
break;
case RegexType.IP:
this._controlTypeText = "IP";
break;
case RegexType.:
this._controlTypeText = "2000-2-28 23:29:59";
break;
case RegexType.:
this._controlTypeText = "2000-2-28";
break;
case RegexType.:
this._controlTypeText = "年份";
break;
case RegexType.:
this._controlTypeText = "月份";
break;
case RegexType.:
this._controlTypeText = "日";
break;
case RegexType.:
this._controlTypeText = "23:29:59";
break;
case RegexType.Am:
this._controlTypeText = "2000-02-29 10:29:39 pm";
break;
case RegexType.:
this._controlTypeText = "2009年2月28日";
break;
}
}
protected override void OnTextChanged(System.EventArgs e)
{
bool b = Invalid(this.ControlType, this.Text);
if (!b)
{
this.BorderColor = Color.Red;
this.errorProvider1.SetIconAlignment((Control)this, ErrorIconAlignment.MiddleRight);
this.errorProvider1.SetError((Control)this, _waterMarkText == null ? _controlTypeText : _waterMarkText);
this._isValid = false;
//this.errorProvider1.Tag = false;
}
else
{
this.BorderColor = r;
errorProvider1.SetError((Control)this, "");
this._isValid = true;
//this.errorProvider1.Tag = true;
base.Invalidate();
}
base.Invalidate();
}
protected override void OnLostFocus(EventArgs e)
{
if (!this._isValid)
{
return;
}
//WmPaint();
if (!_isEmpty)
{
if (this.Text == string.Empty)
{
this.BorderColor = Color.Red;
using (Graphics g = Graphics.FromHwnd(this.Handle))
{
using (Pen pen = new Pen(this.BorderColor, this.BorderWeight))
{
g.DrawRectangle(pen, 0, 0, Size.Width - 1, Size.Height - 1);
}
}
this._isValid = false;
errorProvider1.SetIconAlignment((Control)this, ErrorIconAlignment.MiddleRight);
errorProvider1.SetError((Control)this, "不允许为空!");
return;
}
else
{
errorProvider1.SetIconAlignment((Control)this, ErrorIconAlignment.MiddleRight);
errorProvider1.SetError((Control)this, "");
this._isValid = true;
}
}
if (!Invalid(this.ControlType))
{
this.BorderColor = Color.Red;
this._isValid = false;
errorProvider1.SetIconAlignment((Control)this, ErrorIconAlignment.MiddleRight);
errorProvider1.SetError((Control)this, "数值超出指定范围!");
}
else
{
this.BorderColor = r;
this._isValid = true;
errorProvider1.SetError((Control)this, "");
base.Invalidate();
}
base.Invalidate();
base.OnLostFocus(e);
}
private bool Invalid(RegexType value)
{
if (this._isEmpty && String.IsNullOrEmpty(this.Text))
{
return true;
}
bool b = true;
switch (value)
{
case RegexType.:
if (Convert.ToInt32(this._maxValue) >= Convert.ToInt32(this.Text) && Convert.ToInt32(this._minValue) <= Convert.ToInt32(this.Text))
b = true;
else
b = false;
break;
case RegexType.:
if (Convert.ToInt32(this._maxValue) >= Convert.ToInt32(this.Text) && Convert.ToInt32(this._minValue) <= Convert.ToInt32(this.Text))
b = true;
else
b = false;
break;
case RegexType.:
if (Convert.ToDecimal(this._maxValue) >= Convert.ToDecimal(this.Text) && Convert.ToDecimal(this._minValue) <= Convert.ToDecimal(this.Text))
b = true;
else
b = false;
break;
case RegexType.:
if (Convert.ToDecimal(this._maxValue) >= Convert.ToDecimal(this.Text) && Convert.ToDecimal(this._minValue) <= Convert.ToDecimal(this.Text))
b = true;
else
b = false;
break;
case RegexType.:
if (Convert.ToDecimal(this._maxValue) >= Convert.ToDecimal(this.Text) && Convert.ToDecimal(this._minValue) <= Convert.ToDecimal(this.Text))
b = true;
else
b = false;
break;
case RegexType.:
if (Convert.ToDecimal(this._maxValue) >= Convert.ToDecimal(this.Text) && Convert.ToDecimal(this._minValue) <= Convert.ToDecimal(this.Text))
b = true;
else
b = false;
break;
default:
break;
}
return b;
}
private bool Invalid(RegexType value, string text)
{
if (_isEmpty)
{
if (this.Text == string.Empty)
return true;
}
bool b = false;
switch (value)
{
case RegexType.:
b = true;
break;
case RegexType.:
b = Validation(text, @"^\d+$");
break;
case RegexType.:
b = Validation(text, @"^[\u4e00-\u9fa5]$");
break;
case RegexType.:
b = Validation(text, @"^[1-9]\d{5}$");
break;
case RegexType.Email:
b = Validation(text, @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
break;
case RegexType.:
b = Validation(text, @"^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$");
break;
case RegexType.:
b = Validation(text, @"^\d{3}-\d{8}|\d{4}-\d{7}$");
break;
case RegexType.:
b = Validation(text, @"^((\(\d{2,3}\))|(\d{3}\-))?13\d{9}$");
break;
case RegexType.:
b = Validation(text, @"^-?\d+$");
break;
case RegexType.:
b = Validation(text, @"^-[0-9]*[1-9][0-9]*$");
break;
case RegexType.:
b = Validation(text, @"^(-?\d+)(\.\d+)?$");
break;
case RegexType.:
b = Validation(text, @"^\d+(\.\d+)?$");
break;
case RegexType.:
b = Validation(text, @"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$");
break;
case RegexType.:
b = Validation(text, @"^((-\d+(\.\d+)?)|(0+(\.0+)?))$");
break;
case RegexType.:
b = Validation(text, @"^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$");
break;
case RegexType.:
b = Validation(text, @"^[A-Za-z]+$");
break;
case RegexType.:
b = Validation(text, @"^[A-Z]+$");
break;
case RegexType.:
b = Validation(text, @"^[a-z]+$");
break;
case RegexType.:
b = Validation(text, @"^[A-Za-z0-9]+$");
break;
case RegexType.线:
b = Validation(text, @"^\w+$");
break;
case RegexType.Url:
b = Validation(text, @"^(?:https?|ftp)\:\/\/(?:(?:[a-z0-9\-\._~\!\$\&\'\(\)\*\+\,\;\=:]|%[0-9a-f]{2,2})*\@)?(?:((?:(?:[a-z0-9][a-z0-9\-]*[a-z0-9]|[a-z0-9])\.)*(?:[a-z][a-z0-9\-]*[a-z0-9]|[a-z])|(?:\[[^\]]*\]))(?:\:[0-9]*)?)(?:\/(?:[a-z0-9\-\._~\!\$\&\'\(\)\*\+\,\;\=\:\@]|%[0-9a-f]{2,2})*)*(?:\?(?:[a-z0-9\-\._~\!\$\&\'\(\)\*\+\,\;\=\:\@\/\?]|%[0-9a-f]{2,2})*)?(?:\#(?:[a-z0-9\-\._~\!\$\&\'\(\)\*\+\,\;\=\:\@\/\?]|%[0-9a-f]{2,2})*)?$");
break;
case RegexType.QQ:
b = Validation(text, @"^[1-9][0-9]{4,}$");
break;
case RegexType.:
b = Validation(text, @"^((1[1-5])|(2[1-3])|(3[1-7])|(4[1-6])|(5[0-4])|(6[1-5])|71|(8[12])|91)\d{4}((19\d{2}(0[13-9]|1[012])(0[1-9]|[12]\d|30))|(19\d{2}(0[13578]|1[02])31)|(19\d{2}02(0[1-9]|1\d|2[0-8]))|(19([13579][26]|[2468][048]|0[48])0229))\d{3}(\d|X|x)?$");
break;
case RegexType.IP:
b = Validation(text, @"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$");
break;
case RegexType.:
b = Validation(text, @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$");
break;
case RegexType.:
b = Validation(text, @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$");
break;
case RegexType.:
b = Validation(text, @"^((1[6-9]|[2-9]\d)\d{2})$");
break;
case RegexType.:
b = Validation(text, @"^(0?[123456789]|1[012])$");
break;
case RegexType.:
b = Validation(text, @"^(0?[1-9]|[12]\d|3[01])$");
break;
case RegexType.:
b = Validation(text, @"^(20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$");
break;
case RegexType.Am:
b = Validation(text, @"^((\d{2}(([02468][048])|([13579][26]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|([1-2][0-9])))))|(\d{2}(([02468][1235679])|([13579][01345789]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\s(((0?[1-9])|(1[0-2]))\:([0-5][0-9])((\s)|(\:([0-5][0-9])\s))([AM|PM|am|pm]{2,2})))?$");
break;
case RegexType.:
b = Validation(text, @"^((((1[6-9]|[2-9]\d)\d{2})年(0?[13578]|1[02])月(0?[1-9]|[12]\d|3[01])日)|(((1[6-9]|[2-9]\d)\d{2})年(0?[13456789]|1[012])月(0?[1-9]|[12]\d|30)日)|(((1[6-9]|[2-9]\d)\d{2})年0?2月(0?[1-9]|1\d|2[0-8])日)|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))年0?2月29日))$");
break;
}
return b;
}
private bool Validation(string validValue, string regularExpression)
{
Regex regex;
try
{
regex = new Regex(regularExpression);
}
catch
{
return false;
}
if (regex.IsMatch(validValue))
{
return true;
}
else
{
return false;
}
}
protected override void OnPaint(PaintEventArgs pe)
{
this.BorderStyle = BorderStyle.FixedSingle;
//if (m.Msg == WM_PAINT || m.Msg == WM_NCPAINT)
//{
if (this.BorderWeight % 2 == 0)
{
this.BorderWeight -= 1;
}
using (Graphics g = Graphics.FromHwnd(this.Handle))
{
using (Pen pen = new Pen(this.BorderColor, this.BorderWeight))
{
g.DrawRectangle(pen, 0, 0, Size.Width - 1, Size.Height - 1);
}
}
WmPaint();
//}
base.OnPaint(pe);
}
private void WmPaint()
{
using (Graphics graphics = Graphics.FromHwnd(base.Handle))
{
if (Text.Length == 0 && !string.IsNullOrEmpty(_waterMarkText) && !Focused)
{
TextFormatFlags format = TextFormatFlags.EndEllipsis | TextFormatFlags.VerticalCenter;
if (RightToLeft == RightToLeft.Yes)
{
format |= TextFormatFlags.RightToLeft | TextFormatFlags.Right;
}
TextRenderer.DrawText(graphics, _waterMarkText, Font, base.ClientRectangle, _waterMarkTextColor, format);
}
}
}
#endregion
#region 接口实现
public string MCKey
{
get;
set;
}
public object MCValue
{
get
{
return null;
}
set
{
if (value == null)
return;
try
{
if (_controlType == RegexType.)
{
var intvalue = Convert.ToInt32(value);
this.Text = intvalue.ToString();
//var objectarray = (object[])value;
//if (objectarray.Length > 0)
//{
// var intvalue = Convert.ToInt32(objectarray[0]);
// this.Text = intvalue.ToString();
//}
}
else if (_controlType == RegexType. || _controlType == RegexType.)
{
var objectarray = (object[])value;
if (objectarray.Length > 1)
{
float flvalue = convertTofloat(objectarray);
this.Text = flvalue.ToString();
}
}
}
catch(Exception e)
{
ICSharpCode.Core.LoggingService.Debug(e.ToString());
}
}
}
public IBaseControl MCRoot
{
get
{
return null;
}
set
{
;
}
}
[TypeConverter(typeof(DataSourceConverter))]
[Description("数据连接"), Category("数据")]
public string MCDataSourceID
{
get;
set;
}
public MCDataSource MCDataSource
{
get;
set;
}
[Description("是否为数据库控件"), Category("数据")]
public bool IsDbControl
{
get;
set;
}
[Description("初始化SQL"), Category("数据")]
public string InitDataSource
{
get;
set;
}
[Description("执行SQL"), Category("数据")]
public string ActionDataSource
{
get;
set;
}
[Description("绑定数据源"), Category("数据")]
public object BindDataSource
{
get;
set;
}
[Description("操作类型"), Category("数据")]
public DbOptionTypes DbOptionType
{
get;
set;
}
[Description("是否可见"), Category("行为")]
public bool MCVisible
{
get
{
return this._mcVisible;
}
set
{
this._mcVisible = value == null ? true : value;
if (this.Site.DesignMode)
{
this.Visible = true;
}
else
{
this.Visible = this._mcVisible;
}
}
}
[Description("是否可用"), Category("行为")]
public bool MCEnabled
{
get
{
return this._mcEnabled;
}
set
{
this._mcEnabled = value == null ? true : value;
if (this.Site.DesignMode)
{
this.Enabled = true;
}
else
{
this.Enabled = this._mcEnabled;
}
}
}
[Description("是否控制权限"), Category("行为")]
public bool MCPurview
{
get;
set;
}
public bool IsValid
{
get { return _isValid; }
set { _isValid = value; }
}
#endregion
private float convertTofloat(object[] buff)
{
int flag = 1;
int b = Convert.ToInt32(buff[0]);
int a = Convert.ToInt32(buff[1]);
if (a >= 32768)
{
a = a - 32768;
flag = -1;
}
byte[] low = System.BitConverter.GetBytes(b);
byte[] high = System.BitConverter.GetBytes(a);
byte[] abc = new byte[4];
for (int i = 0; i < 2; i++)
{
abc[i] = low[i];
}
for (int i = 0; i < 2; i++)
{
abc[i + 2] = high[i];
}
float result = BitConverter.ToSingle(abc, 0);
return result * flag;
}
private Object[] floatToObject(float ff)
{
//正负符号默认为0
byte flag = 0;
if (ff < 0)
{
flag = 128;
ff = ff * -1;
}
byte[] floatByte = BitConverter.GetBytes(ff);
byte[] lowhByte = new byte[4];
lowhByte[0] = floatByte[0];
lowhByte[1] = floatByte[1];
lowhByte[2] = 0;
lowhByte[3] = 0;
byte[] highByte = new byte[4];
highByte[0] = floatByte[2];
highByte[1] = floatByte[3];
highByte[2] = 0;
highByte[3] = 0;
highByte[1] = (byte)(highByte[1] + flag);
UInt32 low = BitConverter.ToUInt32(lowhByte, 0);
int high = BitConverter.ToInt16(highByte, 0);
object[] buff = new object[2];
buff[0] = low;
buff[1] = high;
return buff;
}
}
}