using System; using System.Collections.Generic; using System.Text; namespace Shared.Phone.UserCenter { /// /// Botton的最初原型(不建议修改和直接使用):标准字体和颜色,文字向左靠齐,14号字 /// public class ButtonBase : Button { #region ■ 变量声明___________________________ /// /// 设置能否触点击事件 /// private bool m_CanClick = true; /// /// 设置能否触点击事件 /// public bool CanClick { get { return m_CanClick; } set { m_CanClick = value; //能够点击,则显示没有点击过的状态 if (m_CanClick == true) { this.SetNotClickStatu(); } //不能点击,则显示已经点击了的状态 else { this.SetClickStatu(); } } } /// /// 圆角度 /// public int RadiusEx { set { this.Radius = (uint)Application.GetRealHeight(value); } } /// /// 声明此变量,旨在子线程也能够去获取一个控件的主键 /// public string MainKey = string.Empty; /// /// 点击的坐标 /// private System.Drawing.Point downPoint = new System.Drawing.Point(); /// /// 控件的点击事件(自定义封装事件,此事件被认可为执行按钮按下事件,受CanClick属性控制) /// public Action ButtonClickEvent = null; /// /// 控件的按下事件(自定义封装事件,此事件被认可为执行按钮按下事件,受CanClick属性控制) /// public Action ButtonDownClickEvent = null; /// /// 控件触发移动的事件(自身拥有算法,当移动多少像素后,触发事件,注意,该事件可能会频繁的触发) /// public Action ButtonHappenMoveEvent = null; #endregion #region ■ 重写彪哥的属性_____________________ /// /// 重写Text属性 /// public new string Text { //先这么弄先吧 get { return base.Text == null ? string.Empty : base.Text; } set { base.Text = value == null ? string.Empty : value; } } #endregion #region ■ 初始化_____________________________ /// /// Botton的最初原型(不建议修改和直接使用):标准字体和颜色,文字向左靠齐,14号字 /// public ButtonBase() { //测试,全体按钮为14号字 this.TextSize = 14; this.TextColor = UserCenterColor.Current.TextColor1; this.TextAlignment = TextAlignment.CenterLeft; //点击事件 this.MouseUpEventHandler += ButtonBase_MouseUpEventHandler; //按下事件 this.MouseDownEventHandler += ButtonBase_MouseDownEventHandler; //移动事件 this.MouseMoveEventHandler += ButtonBase_MouseMoveEventHandler; } /// /// 初始化控件大小(不以平均值进行真实数值计算) /// /// 宽度 /// 高度 /// 是否计算真实值 public void InitSize(int i_Width, int i_Height, bool real = true) { if (real == true) { i_Width = Application.GetRealWidth(i_Width); i_Height = Application.GetRealHeight(i_Height); } this.Height = i_Height; this.Width = i_Width; } /// /// 初始化控件大小(不以平均值进行真实数值计算) /// /// 宽度 /// 是否计算真实值 public void InitSize(int i_Width, bool real = false) { if (real == true) { i_Width = Application.GetRealWidth(i_Width); } this.Height = ControlCommonResourse.NormalControlHeight; this.Width = i_Width; } /// /// 初始化图标控件大小(以平均值进行真实数值计算) /// /// 宽度 /// 高度 /// 是否计算真实值 public void InitIconSize(int i_Width, int i_Height, bool real = true) { if (real == true) { i_Width = this.GetPictrueRealSize(i_Width); i_Height = this.GetPictrueRealSize(i_Height); } this.Height = i_Height; this.Width = i_Width; } /// /// 初始化图片控件大小 /// /// 宽度 /// 高度 /// 是否计算真实值 public void InitPictrueSize(int i_Width, int i_Height, bool real = true) { if (real == true) { i_Width = HdlControlLogic.Current.GetPictrueRealSize(i_Width); i_Height = HdlControlLogic.Current.GetPictrueRealSize(i_Height); } this.Height = i_Height; this.Width = i_Width; } #endregion #region ■ 点击事件___________________________ /// /// 点击事件 /// /// /// private void ButtonBase_MouseUpEventHandler(object sender, MouseEventArgs e) { if (ButtonClickEvent == null) { this.MouseUpEventHandler -= ButtonBase_MouseUpEventHandler; return; } //2020.05.14追加IsFormAdding:界面还在加载中,不能再点击 if (CanClick == true && ControlCommonResourse.IsFormAdding == false) { //Log出力 this.WriteLog(0); try { this.ButtonClickEvent(this, e); } catch (Exception ex) { //出现未知错误 var alert = new ShowMsgControl(ShowMsgType.Error, Language.StringByID(R.MyInternationalizationString.uUnKnownError)); alert.Show(); //Log出力 HdlLogLogic.Current.WriteLog(ex); } } } #endregion #region ■ 按下事件___________________________ /// /// 按下事件 /// /// /// private void ButtonBase_MouseDownEventHandler(object sender, MouseEventArgs e) { //记录起当前点击的坐标 downPoint.X = (int)e.X; downPoint.Y = (int)e.Y; if (CanClick == false || this.ButtonDownClickEvent == null) { //不能点击 return; } try { this.ButtonDownClickEvent(this, e); } catch (Exception ex) { //出现未知错误 var alert = new ShowMsgControl(ShowMsgType.Error, Language.StringByID(R.MyInternationalizationString.uUnKnownError)); alert.Show(); //Log出力 HdlLogLogic.Current.WriteLog(ex); } } #endregion #region ■ 移动事件___________________________ /// /// 移动事件 /// /// /// private void ButtonBase_MouseMoveEventHandler(object sender, MouseEventArgs e) { if (this.ButtonHappenMoveEvent == null) { this.MouseMoveEventHandler -= ButtonBase_MouseMoveEventHandler; return; } int value = (int)e.X - this.downPoint.X; if (value >= 30 || value <= -30) { //触发移动事件 this.ButtonHappenMoveEvent(); return; } value = (int)e.Y - this.downPoint.Y; if (value >= 30 || value <= -30) { //触发移动事件 this.ButtonHappenMoveEvent(); return; } } #endregion #region ■ 设置点击状态_______________________ /// /// 设置点击后的状态(此方法由各自控件进行重载) /// public virtual void SetClickStatu() { } /// /// 设置非点击后的状态(此方法由各自控件进行重载) /// public virtual void SetNotClickStatu() { } #endregion #region ■ 一般方法___________________________ /// /// 控件摧毁 /// public override void RemoveFromParent() { ButtonClickEvent = null; ButtonDownClickEvent = null; if (this.Parent != null) { base.RemoveFromParent(); } } /// /// Y轴重置(真实数值,没有父容器无效) /// /// 上下对齐方式 /// 上下两部分的间距 public void ReSetYaxis(UViewAlignment alignment, int Space = 0) { if (this.Parent == null) { return; } //Y轴重置 this.Y = HdlControlLogic.Current.GetControlChidrenYaxis(this.Parent.Height, this.Height, alignment, Space); } /// /// 根据文本,计算它实际的宽度 /// /// public int GetRealWidthByText() { if (string.IsNullOrEmpty(this.Text) == true) { return Application.GetRealWidth(25); } #if Android //需要增加一个误差值 return this.GetTextWidth() + Application.GetRealWidth(12); #endif #if iOS //需要增加一个误差值 return this.GetTextWidth() + Application.GetRealWidth(25); #endif } /// /// 计算图片的真实高宽度 /// /// /// public int GetPictrueRealSize(int i_size) { return HdlControlLogic.Current.GetPictrueRealSize(i_size); } #endregion #region ■ Log出力____________________________ /// /// 该控件所属的界面名字 /// public string formName = null; /// /// 控件名字 /// private string controlName = null; /// /// Log出力 /// private void WriteLog(int div) { if (formName == null) { formName = string.Empty; View myView = this.Parent; for (; ; ) { if (myView == null) { break; } else if (myView is CommonFormBase) { //这个控件所属的界面 formName = ((CommonFormBase)myView).FormID; break; } myView = myView.Parent; } if (string.IsNullOrEmpty(this.Text) == false) { //这个控件的文本 controlName = this.Text; } else { //如果没有文本的话,它应该是一张图片 controlName = this.UnSelectedImagePath; } } if (div == 0) { HdlLogLogic.Current.WriteLog(1, formName + "的[" + controlName + "]按键被点击"); } else { HdlLogLogic.Current.WriteLog(1, formName + "的[" + controlName + "]按键被长按"); } } #endregion } }