using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; namespace Dongke.WinForm.Controls { /// /// 标准按钮控件 /// [ToolboxBitmap(typeof(Button))] public class BtnButton : Button, IDKControl, IAsyncControl { #region 成员变量 /// /// 焦点是否进入控件 /// private bool _entered = false; /// /// 鼠标是否进入控件 /// private bool _mouseOver = false; #endregion #region 构造函数 /// /// 标准按钮控件 /// public BtnButton() { this.Size = new Size(85, 30); } #endregion #region 属性 /// /// 获取一个值,该值指示控件是否有输入焦点。 /// [DefaultValue(false)] [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] [EditorBrowsable(EditorBrowsableState.Advanced)] public virtual bool Entered { get { return this._entered; } protected set { this._entered = value; } } /// /// 获取一个值,该值指示鼠标是否在控件上方。 /// [DefaultValue(false)] [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] [EditorBrowsable(EditorBrowsableState.Advanced)] public virtual bool MouseOver { get { return this._mouseOver; } protected set { this._mouseOver = value; } } #endregion #region 重写属性 /// /// 获取或设置控件的前景色 /// [DefaultValue(typeof(Color), "White")] public override Color ForeColor { get { return Color.White; } //set //{ //} } /// /// 获取或设置控件的前景色 /// //[DefaultValue(typeof(Resources), "button_00_back")] [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] [EditorBrowsable(EditorBrowsableState.Never)] public override Image BackgroundImage { get { if (!this.Enabled) { return Properties.Resources.Button_Disable_00; } if (this._mouseOver) { return Properties.Resources.Button_Enter_00; } return Properties.Resources.Button_Back_00; } //set //{ //} } #endregion #region 重写事件 #region 焦点事件 /// /// 输入焦点进入控件 /// /// protected override void OnEnter(EventArgs e) { this.Entered = true; base.OnEnter(e); } /// /// 获得焦点 /// /// protected override void OnGotFocus(EventArgs e) { base.OnGotFocus(e); } /// /// 失去焦点 /// /// protected override void OnLostFocus(EventArgs e) { base.OnLostFocus(e); } /// /// 输入焦点离开控件 /// /// protected override void OnLeave(EventArgs e) { base.OnLeave(e); //this.Entered = false; } /// /// 验证事件 /// /// protected override void OnValidating(CancelEventArgs e) { base.OnValidating(e); if (!e.Cancel) { this.Entered = false; } } #endregion #region 鼠标事件 /// /// 鼠标进入控件 /// /// protected override void OnMouseEnter(EventArgs e) { this.MouseOver = true; base.OnMouseEnter(e); } /// /// 鼠标离开控件 /// /// protected override void OnMouseLeave(EventArgs e) { base.OnMouseLeave(e); this.MouseOver = false; } /// /// 鼠标移动 /// /// protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); } /// /// 鼠标按下 /// /// protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); } /// /// 鼠标抬起 /// /// protected override void OnMouseUp(MouseEventArgs e) { base.OnMouseUp(e); } #endregion #endregion #region IAsyncControl 成员 #region 成员变量 /// /// 异步处理开始时,控件状态 /// private bool _asyncBeginStatus = false; private bool _asyncBeginFocused = false; #endregion #region 公有方法 /// /// 开始异步处理 /// /// 是否处理焦点 public virtual void BeginAsync(ref bool doFocus) { this._asyncBeginFocused = false; if (doFocus && this.Focused) { this._asyncBeginFocused = true; doFocus = false; } this._asyncBeginStatus = this.Enabled; this.Enabled = false; } /// /// 结束异步处理 /// public virtual void EndAsync() { this.Enabled = this._asyncBeginStatus; if (this._asyncBeginFocused) { this.Focus(); } } #endregion #endregion } }