溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C#中Winform 實現Ajax效果自定義按鈕

發(fā)布時間:2020-09-13 19:44:38 來源:腳本之家 閱讀:269 作者:數據酷軟件 欄目:編程語言

技術看點

  1.  WinForm自定義控件的使用
  2. 自定義控件gif動畫的播放

需求及效果

又來一波 C# GDI自定義控件show 。這個控件已經使用幾年了,最近找出來重構一下。原來是沒有邊框的,那么導致導航的功能不是很突出。本來想加個效果:在執(zhí)行單擊時顯示Loading動畫,在執(zhí)行完單擊事件后恢復原樣。這就是網頁里見到的局部刷新,Ajax常用的場景。需求來自幾年前一個智能儲物柜項目,人機界面有個美工設計好的效果圖,為了省事和通用,需要一個透明的按鈕來實現導航的任務。就是控件只是設計時可見,運行時不可見。

C#中Winform 實現Ajax效果自定義按鈕

C#中Winform 實現Ajax效果自定義按鈕

 C#中Winform 實現Ajax效果自定義按鈕

關鍵點說明

1)、GraphicsPath實現矩形的圓角羽化處理

 using (GraphicsPath path = new GraphicsPath())
   {
    #region 羽化,圓角處理
    path.StartFigure();
    path.AddArc(new Rectangle(new Point(rect.X, rect.Y), new Size(2 * Radius, 2 * Radius)), 180, 90);
    path.AddLine(new Point(rect.X + Radius, rect.Y), new Point(rect.Right - Radius, rect.Y));
    path.AddArc(new Rectangle(new Point(rect.Right - 2 * Radius, rect.Y), new Size(2 * Radius, 2 * Radius)), 270, 90);
    path.AddLine(new Point(rect.Right, rect.Y + Radius), new Point(rect.Right, rect.Bottom - Radius));
    path.AddArc(new Rectangle(new Point(rect.Right - 2 * Radius, rect.Bottom - 2 * Radius), new Size(2 * Radius, 2 * Radius)), 0, 90);
    path.AddLine(new Point(rect.Right - Radius, rect.Bottom), new Point(rect.X + Radius, rect.Bottom));
    path.AddArc(new Rectangle(new Point(rect.X, rect.Bottom - 2 * Radius), new Size(2 * Radius, 2 * Radius)), 90, 90);
    path.AddLine(new Point(rect.X, rect.Bottom - Radius), new Point(rect.X, rect.Y + Radius));
    path.CloseFigure();
    #endregion 
    
要點就是畫幾段弧線和矩形連接起來。透明就是用了Color.FromArgb加上透明度,然后填充GraphicsPath形成透明區(qū)域。
g.FillPath(new SolidBrush(Color.FromArgb(153, BackColor)), path);
2)、單窗體應用如何模塊化 

窗體只有一個,但操作界面好多個,由于是無人值守的應用。那么老是切換窗體操作是非常不方便的。工作區(qū)域是一個容器Panel,把每個操作界面定義成一個Panel作為只容器。

 public partial class DepositBizPanel : UserControl
 {
  private BackgroundStyle backgroundStyle = BackgroundStyle.Green;
  /// <summary>
  /// 主題風格
  /// </summary>
  public BackgroundStyle BackgroundStyle
  {
   get { return backgroundStyle; }
   set
   {
    backgroundStyle = value;
    switch (value)
    {
     case GreenlandExpressBox.BackgroundStyle.Blue:
      BackgroundImage = Properties.Resources.jbblue;
      break;
     case GreenlandExpressBox.BackgroundStyle.Orange:
      BackgroundImage = Properties.Resources.jborange;
      break;
     case GreenlandExpressBox.BackgroundStyle.Green:
      BackgroundImage = Properties.Resources.jbgreen;
      break;
    }
    Invalidate();
   }
  }

  public Panel ParentPanel
  {
   get;
   set;
  }

  public Bitmap QR_Barcode
  {
   get { return (Bitmap)pbxBarcode.Image; }
   set { pbxBarcode.Image = value; }
  }

  public DialogResult PanelDiagResult
  {
   get;
   set;
  }

  public DepositBizPanel(Panel parent, Bitmap barcode, BackgroundStyle style)
  {
   InitializeComponent();
   DoubleBuffered = true;
   ParentPanel = parent;
   QR_Barcode = barcode;
   BackgroundStyle = style;
  }  

  private void btnback_Click(object sender, EventArgs e)
  {
   foreach (Control panel in ParentPanel.Controls)
   {
    if (panel is DepositBizPanel)
    {
     ParentPanel.Controls.Remove(panel);
     PanelDiagResult = DialogResult.Cancel;
     break;
    }
   }
  }

  private void btnprocessnext_Click(object sender, EventArgs e)
  {
   foreach (Control panel in ParentPanel.Controls)
   {
    if (panel is DepositBizPanel)
    {
     ParentPanel.Controls.Remove(panel);
     PanelDiagResult = DialogResult.OK;
     break;
    }
   }
  }
 }
人機操作界面例子

 3)、控件播放gif動畫

private void BeginAnimate()
  {
   if (m_AnimateImage == null)
    return;
   if (ImageAnimator.CanAnimate(m_AnimateImage))
   {
    //當gif動畫每隔一定時間后,都會變換一幀,那么就會觸發(fā)一事件, 
    //該方法就是將當前image每變換一幀時,都會調用當前這個委托所關聯的方法。 
    ImageAnimator.Animate(m_AnimateImage, m_evthdlAnimator);
   }
  }
  private void StopAnimate()
  {
   if (m_AnimateImage == null)
    return;
   try
   {
    if (ImageAnimator.CanAnimate(m_AnimateImage))
    {
     ImageAnimator.StopAnimate(m_AnimateImage, m_evthdlAnimator);
    }
   }
   finally
   {
    m_IsExecuted = false;
   }
  }
  private void UpdateImage()
  {
   if (m_AnimateImage == null)
    return;
   if (ImageAnimator.CanAnimate(m_AnimateImage))
   {
    //獲得當前gif動畫的下一步需要渲染的幀,當下一步任何對當前gif動畫的操作都是對該幀進行操作) 
    ImageAnimator.UpdateFrames(m_AnimateImage);
   }
  }
  private void OnImageAnimate(Object sender, EventArgs e)
  {
   Invalidate();
  }
  protected override void OnLoad(EventArgs e)
  {
   base.OnLoad(e);
   string s1 = @"R0lGODlhIAAgALMAAP///7Ozs/v7+9bW1uHh5fLy8rq6uoGBgTQ0NAEBARsbG8TExJeXl/39/VRUVAAAACH/C05FVFNDQVBFMi4wAwEAAAAh+QQFBQAAACwAAAAAIAAgAAAE5xDISSlLrOrNp0pKNRCdFhxVolJLEJQUoSgOpSYT4RowNSsvyW1icA16k8MMMRkCBjskBTFDAZyuAEkqCfxIQ2hgQRFvAQEEIjNxVDW6XNE4YagRjuBCwe60smQUDnd4Rz1ZAQZnFAGDd0hihh22CEE9kjAEVlycXIg7BAsMB6SlnJ87paqbSKiKoqusnbMdmDC2tXQlkUhziYtyWTxIfy6BE8WJt5YEvpJivxNaGmLHT0VnOgGYf0dZXS7APdpB309RnHOG5gDqXGLDaC457D1zZ/V/nmOM82XiHQjYKhKP1oZmAD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BQUAAAAsCAAOABgAEgAABGwQyEkrCDgbYvvMoOF5ILaNaIoGKroch9hacD3MFMHUBzMHiBtgwJMBFolDB4GoGGBCACKRcAAUWAmzOWJQExysQsJgWj0KqvKalTiYPhp1LBFTtp10Is6mT5gdVFx1bRN8FTsVCAqDOB9+KhEAIfkEBQUAAAAsAgASAB0ADgAABHgQyEmrBePS4bQdQZBdR5IcHmWEgUFQgWKaKbWwwSIhc4LonsXhBSCsQoOSScGQDJiWwOHQnAxWBIYJNXEoFCiEWDI9jCzESey7GwMM5doEwW4jJoypQQ743u1WcTV0CgFzbhJ5XClfHYd/EwZnHoYVDgiOfHKQNREAIfkEBQUAAAAsAAAPABkAEQAABGeQqUQruDjrW3vaYCZ5X2ie6EkcKaooTAsi7ytnTq046BBsNcTvItz4AotMwKZBIC6H6CVAJaCcT0CUBTgaTg5nTCu9GKiDEMPJg5YBBOpwlnVzLwtqyKnZagZWahoMB2M3GgsHSRsRACH5BAUFAAAALAEACAARABgAAARcMKR0gL34npkUyyCAcAmyhBijkGi2UW02VHFt33iu7yiDIDaD4/erEYGDlu/nuBAOJ9Dvc2EcDgFAYIuaXS3bbOh7MIC5IAP5Eh6fk2exC4tpgwZyiyFgvhEMBBEAIfkEBQUAAAAsAAACAA4AHQAABHMQyAnYoViSlFDGXBJ808Ep5KRwV8qEg+pRCOeoioKMwJK0Ekcu54h9AoghKgXIMZgAApQZcCCu2Ax2O6NUud2pmJcyHA4L0uDM/ljYDCnGfGakJQE5YH0wUBYBAUYfBIFkHwaBgxkDgX5lgXpHAXcpBIsRADs=";
   byte[] buffer = Convert.FromBase64String(s1);
   MemoryStream ms = new MemoryStream(buffer);
   var srcImg = Image.FromStream(ms);
   m_AnimateImage = srcImg;
  }
OnLoad執(zhí)行的操作是從base64字符串里反序列化圖片,就是效果圖中的Loading的gif圖片。這里遇到一個問題:在關閉了MemoryStream之后,會出現“gdi+ 中發(fā)生一般性錯誤”,于是改為不關閉了,控件銷毀之后占用的內存就會釋放吧。這是一點隱憂,如果有好的辦法,希望留言告知。

透明按鈕自定義控件全部代碼

第一版自定義按鈕:

/// <summary>
 /// Cool透明自定義按鈕
 /// </summary>
 public partial class CoolTransparentButton : UserControl
 {
  private Size iconSize = new Size(32, 32);
  public Size IconSize
  {
   get
   {
    return iconSize;
   }
   set
   {
    iconSize = value;
    Invalidate();
   }
  }
  private string _ButtonText;
  public string ButtonText
  {
   get { return _ButtonText; }
   set
   {
    _ButtonText = value;
    Invalidate();
   }
  }
  protected Image _IconImage;
  public Image IconImage
  {
   get
   {
    return _IconImage;
   }
   set
   {
    _IconImage = value;
    Invalidate();
   }
  }
  private bool _FocseActived = false;
  private Color _BorderColor = Color.White;
  public Color BorderColor
  {
   get
   {
    return _BorderColor;
   }
   set
   {
    _BorderColor = value;
    Invalidate();
   }
  }
  private int _Radius = 12;
  public int Radius
  {
   get
   {
    return _Radius;
   }
   set
   {
    _Radius = value;
    Invalidate();
   }
  }
  private bool ifDrawBorderWhenLostFocse = true;
  /// <summary>
  /// 失去焦點是否畫邊框
  /// </summary>
  public bool IfDrawBorderWhenLostFocse
  {
   get
   {
    return ifDrawBorderWhenLostFocse;
   }
   set
   {
    ifDrawBorderWhenLostFocse = value;
    Invalidate();
   }
  }
  /// <summary>
  /// 是否處于激活狀態(tài)(焦點)
  /// </summary>
  public bool FocseActived
  {
   get { return _FocseActived; }
   set
   {
    _FocseActived = value;
    Invalidate();
   }
  }   
  public CoolTransparentButton()
  {
   DoubleBuffered = true;
   BackColor = Color.Transparent;
   SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true);
   SetStyle(ControlStyles.Opaque, false);
   UpdateStyles(); 
  }
  protected override void OnPaint(PaintEventArgs e)
  {
   var rect = ClientRectangle;
   rect.Inflate(-1, -1);
   Graphics g = e.Graphics;
   g.SmoothingMode = SmoothingMode.HighQuality;
   using (GraphicsPath path = new GraphicsPath())
   {
    #region 羽化,圓角處理
    path.StartFigure();
    path.AddArc(new Rectangle(new Point(rect.X, rect.Y), new Size(2 * Radius, 2 * Radius)), 180, 90);
    path.AddLine(new Point(rect.X + Radius, rect.Y), new Point(rect.Right - Radius, rect.Y));
    path.AddArc(new Rectangle(new Point(rect.Right - 2 * Radius, rect.Y), new Size(2 * Radius, 2 * Radius)), 270, 90);
    path.AddLine(new Point(rect.Right, rect.Y + Radius), new Point(rect.Right, rect.Bottom - Radius));
    path.AddArc(new Rectangle(new Point(rect.Right - 2 * Radius, rect.Bottom - 2 * Radius), new Size(2 * Radius, 2 * Radius)), 0, 90);
    path.AddLine(new Point(rect.Right - Radius, rect.Bottom), new Point(rect.X + Radius, rect.Bottom));
    path.AddArc(new Rectangle(new Point(rect.X, rect.Bottom - 2 * Radius), new Size(2 * Radius, 2 * Radius)), 90, 90);
    path.AddLine(new Point(rect.X, rect.Bottom - Radius), new Point(rect.X, rect.Y + Radius));
    path.CloseFigure();
    #endregion 
    if (!FocseActived)
    {
     if (ifDrawBorderWhenLostFocse)
      g.DrawPath(new Pen(Color.Gray, 1), path);
     g.FillPath(new SolidBrush(Color.FromArgb(66, BackColor)), path);
    }
    else
    {
     g.DrawPath(new Pen(BorderColor, 1), path);
     rect.Inflate(-1, -1);
     g.FillPath(new SolidBrush(Color.FromArgb(153, BackColor)), path);
    }
    #region 畫文本
    g.SmoothingMode = SmoothingMode.AntiAlias;
    if (IconImage != null)
    {
     Rectangle rc = new Rectangle((Width - 32) / 2, 16, IconSize.Width, IconSize.Height);
     g.DrawImage(IconImage, rc);
    }
    if (!string.IsNullOrEmpty(ButtonText))
    {
     using (StringFormat f = new StringFormat())
     {
      Rectangle rectTxt = new Rectangle(0, (Height - 18) / 2, Width, 36);
      f.Alignment = StringAlignment.Center;// 水平居中對齊 
      f.LineAlignment = StringAlignment.Center; // 垂直居中對齊 
      f.FormatFlags = StringFormatFlags.NoWrap;// 設置為單行文本 
      SolidBrush fb = new SolidBrush(this.ForeColor); // 繪制文本 
      e.Graphics.DrawString(ButtonText, new Font("微軟雅黑", 16F, FontStyle.Bold), fb, rectTxt, f);
     }
    }
    #endregion 
   }
  }
  protected override void OnMouseHover(EventArgs e)
  {
   FocseActived = true;
  }
  protected override void OnMouseLeave(EventArgs e)
  {
   FocseActived = false;
  }
  protected override void OnEnter(EventArgs e)
  {
   FocseActived = true;
  }
  protected override void OnLeave(EventArgs e)
  {
   FocseActived = false;
  } 
 }
第二版自定義按鈕:
/// <summary>
 /// 自定義透明自定義按鈕,模仿實現了網頁元素的Ajax效果
 /// </summary>
 public partial class AjaxTransparentButton : UserControl
 {
  private Size iconSize = new Size(32, 32);
  public Size IconSize
  {
   get
   {
    return iconSize;
   }
   set
   {
    iconSize = value;
    Invalidate();
   }
  }
  private string _ButtonText;
  public string ButtonText
  {
   get { return _ButtonText; }
   set
   {
    _ButtonText = value;
    Invalidate();
   }
  }
  protected Image _IconImage;
  public Image IconImage
  {
   get
   {
    return _IconImage;
   }
   set
   {
    _IconImage = value;
    Invalidate();
   }
  }
  private bool _FocseActived = false;
  private Color _BorderColor = Color.White;
  public Color BorderColor
  {
   get
   {
    return _BorderColor;
   }
   set
   {
    _BorderColor = value;
    Invalidate();
   }
  }
  private int _Radius = 12;
  public int Radius
  {
   get
   {
    return _Radius;
   }
   set
   {
    _Radius = value;
    Invalidate();
   }
  }
  private bool ifDrawBorderWhenLostFocse = true;
  /// <summary>
  /// 失去焦點是否畫邊框
  /// </summary>
  public bool IfDrawBorderWhenLostFocse
  {
   get
   {
    return ifDrawBorderWhenLostFocse;
   }
   set
   {
    ifDrawBorderWhenLostFocse = value;
    Invalidate();
   }
  }
  /// <summary>
  /// 是否處于激活狀態(tài)(焦點)
  /// </summary>
  public bool FocseActived
  {
   get { return _FocseActived; }
   set
   {
    _FocseActived = value;
    Invalidate();
   }
  }
  private Image m_AnimateImage = null;
  private EventHandler m_evthdlAnimator = null;
  private bool m_IsExecuted = false;
  public AjaxTransparentButton()
  {
   BackColor = Color.Transparent;
   SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.UserPaint, true);
   SetStyle(ControlStyles.Opaque, false);
   UpdateStyles();
   m_evthdlAnimator = new EventHandler(OnImageAnimate);
  }
  protected override void OnPaint(PaintEventArgs e)
  {
   var rect = ClientRectangle;
   rect.Inflate(-1, -1);
   Graphics g = e.Graphics;
   g.SmoothingMode = SmoothingMode.HighQuality;
   using (GraphicsPath path = new GraphicsPath())
   {
    #region 羽化,圓角處理
    path.StartFigure();
    path.AddArc(new Rectangle(new Point(rect.X, rect.Y), new Size(2 * Radius, 2 * Radius)), 180, 90);
    path.AddLine(new Point(rect.X + Radius, rect.Y), new Point(rect.Right - Radius, rect.Y));
    path.AddArc(new Rectangle(new Point(rect.Right - 2 * Radius, rect.Y), new Size(2 * Radius, 2 * Radius)), 270, 90);
    path.AddLine(new Point(rect.Right, rect.Y + Radius), new Point(rect.Right, rect.Bottom - Radius));
    path.AddArc(new Rectangle(new Point(rect.Right - 2 * Radius, rect.Bottom - 2 * Radius), new Size(2 * Radius, 2 * Radius)), 0, 90);
    path.AddLine(new Point(rect.Right - Radius, rect.Bottom), new Point(rect.X + Radius, rect.Bottom));
    path.AddArc(new Rectangle(new Point(rect.X, rect.Bottom - 2 * Radius), new Size(2 * Radius, 2 * Radius)), 90, 90);
    path.AddLine(new Point(rect.X, rect.Bottom - Radius), new Point(rect.X, rect.Y + Radius));
    path.CloseFigure();
    #endregion 
    if (!FocseActived)
    {
     if (ifDrawBorderWhenLostFocse)
      g.DrawPath(new Pen(Color.Gray, 1), path);
     g.FillPath(new SolidBrush(Color.FromArgb(66, BackColor)), path);
    }
    else
    {
     g.DrawPath(new Pen(BorderColor, 1), path);
     rect.Inflate(-1, -1);
     g.FillPath(new SolidBrush(Color.FromArgb(153, BackColor)), path);
    }
    #region 畫文本
    g.SmoothingMode = SmoothingMode.AntiAlias;
    if (IconImage != null)
    {
     Rectangle rc = new Rectangle((Width - 32) / 2, 16, IconSize.Width, IconSize.Height);
     g.DrawImage(IconImage, rc);
    }
    if (!string.IsNullOrEmpty(ButtonText))
    {
     using (StringFormat f = new StringFormat())
     {
      Rectangle rectTxt = new Rectangle(0, (Height - 18) / 2, Width, 36);
      f.Alignment = StringAlignment.Center;// 水平居中對齊 
      f.LineAlignment = StringAlignment.Center; // 垂直居中對齊 
      f.FormatFlags = StringFormatFlags.NoWrap;// 設置為單行文本 
      SolidBrush fb = new SolidBrush(this.ForeColor); // 繪制文本 
      e.Graphics.DrawString(ButtonText, new Font("微軟雅黑", 16F, FontStyle.Bold), fb, rectTxt, f);
     }
    }
    if (m_AnimateImage != null)
    {
     Rectangle rectGif = new Rectangle((Width - 24) / 2, (Height - 16) / 2 - 8, 32, 32);
     if (m_IsExecuted)
     {
      UpdateImage();
      e.Graphics.DrawImage(m_AnimateImage, rectGif);
     }
     else
     {
      e.Graphics.FillRectangle(new SolidBrush(Color.Transparent), rectGif);
     }
    }
    #endregion 
   }
  }
  protected override void OnMouseHover(EventArgs e)
  {
   FocseActived = true;
  }
  protected override void OnMouseLeave(EventArgs e)
  {
   FocseActived = false;
  }
  protected override void OnEnter(EventArgs e)
  {
   FocseActived = true;
  }
  protected override void OnLeave(EventArgs e)
  {
   FocseActived = false;
  }
  private void BeginAnimate()
  {
   if (m_AnimateImage == null)
    return;
   if (ImageAnimator.CanAnimate(m_AnimateImage))
   {
    //當gif動畫每隔一定時間后,都會變換一幀,那么就會觸發(fā)一事件, 
    //該方法就是將當前image每變換一幀時,都會調用當前這個委托所關聯的方法。 
    ImageAnimator.Animate(m_AnimateImage, m_evthdlAnimator);
   }
  }
  private void StopAnimate()
  {
   if (m_AnimateImage == null)
    return;
   try
   {
    if (ImageAnimator.CanAnimate(m_AnimateImage))
    {
     ImageAnimator.StopAnimate(m_AnimateImage, m_evthdlAnimator);
    }
   }
   finally
   {
    m_IsExecuted = false;
   }
  }
  private void UpdateImage()
  {
   if (m_AnimateImage == null)
    return;
   if (ImageAnimator.CanAnimate(m_AnimateImage))
   {
    //獲得當前gif動畫的下一步需要渲染的幀,當下一步任何對當前gif動畫的操作都是對該幀進行操作) 
    ImageAnimator.UpdateFrames(m_AnimateImage);
   }
  }
  private void OnImageAnimate(Object sender, EventArgs e)
  {
   Invalidate();
  }
  protected override void OnLoad(EventArgs e)
  {
   base.OnLoad(e);
   string s1 = @"R0lGODlhIAAgALMAAP///7Ozs/v7+9bW1uHh5fLy8rq6uoGBgTQ0NAEBARsbG8TExJeXl/39/VRUVAAAACH/C05FVFNDQVBFMi4wAwEAAAAh+QQFBQAAACwAAAAAIAAgAAAE5xDISSlLrOrNp0pKNRCdFhxVolJLEJQUoSgOpSYT4RowNSsvyW1icA16k8MMMRkCBjskBTFDAZyuAEkqCfxIQ2hgQRFvAQEEIjNxVDW6XNE4YagRjuBCwe60smQUDnd4Rz1ZAQZnFAGDd0hihh22CEE9kjAEVlycXIg7BAsMB6SlnJ87paqbSKiKoqusnbMdmDC2tXQlkUhziYtyWTxIfy6BE8WJt5YEvpJivxNaGmLHT0VnOgGYf0dZXS7APdpB309RnHOG5gDqXGLDaC457D1zZ/V/nmOM82XiHQjYKhKP1oZmADdEAAAh+QQFBQAAACwAAAAAGAAXAAAEchDISasKNeuJFKoHs4mUYlJIkmjIV54Soypsa0wmLSnqoTEtBw52mG0AjhYpBxioEqRNy8V0qFzNw+GGwlJki4lBqx1IBgjMkRIghwjrzcDti2/Gh7D9qN774wQGAYOEfwCChIV/gYmDho+QkZKTR3p7EQAh+QQFBQAAACwBAAAAHQAOAAAEchDISWdANesNHHJZwE2DUSEo5SjKKB2HOKGYFLD1CB/DnEoIlkti2PlyuKGEATMBaAACSyGbEDYD4zN1YIEmh0SCQQgYehNmTNNaKsQJXmBuuEYPi9ECAU/UFnNzeUp9VBQEBoFOLmFxWHNoQw6RWEocEQAh+QQFBQAAACwHAAAAGQARAAAEaRDICdZZNOvNDsvfBhBDdpwZgohBgE3nQaki0AYEjEqOGmqDlkEnAzBUjhrA0CoB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CcT0CUBTgaTg5nTCu9GKiDEMPJg5YBBOpwlnVzLwtqyKnZagZWahoMB2M3GgsHSRsRACH5BAUFAAAALAEACAARABgAAARcMKR0gL34npkUyyCAcAmyhBijkGi2UW02VHFt33iu7yiDIDaD4/erEYGDlu/nuBAOJ9Dvc2EcDgFAYIuaXS3bbOh7MIC5IAP5Eh6fk2exC4tpgwZyiyFgvhEMBBEAIfkEBQUAAAAsAAACAA4AHQAABHMQyAnYoViSlFDGXBJ808Ep5KRwV8qEg+pRCOeoioKMwJK0Ekcu54h9AoghKgXIMZgAApQZcCCu2Ax2O6NUud2pmJcyHA4L0uDM/ljYDCnGfGakJQE5YH0wUBYBAUYfBIFkHwaBgxkDgX5lgXpHAXcpBIsRADs=";
   byte[] buffer = Convert.FromBase64String(s1);
   MemoryStream ms = new MemoryStream(buffer);
   var srcImg = Image.FromStream(ms);
   m_AnimateImage = srcImg;
  }
  protected override void OnClick(EventArgs e)
  {
   if (m_IsExecuted)
    return;
   Action clickTask = () =>
   {
    m_IsExecuted = true;
    BeginAnimate();
    base.OnClick(e);
    Invalidate();
   };
   //異步執(zhí)行單擊事件
   clickTask.BeginInvoke((result) =>
   {
    clickTask.EndInvoke(result);
    m_IsExecuted = false;
    StopAnimate();
   }, null);
  }
  protected override void Dispose(bool disposing)
  {
   base.Dispose(disposing);
   if (m_AnimateImage != null)
   {
    try
    {
     StopAnimate();
    }
    finally
    {
     m_AnimateImage.Dispose();
     m_evthdlAnimator = null;
    }
   }
  }
  protected override void OnKeyDown(KeyEventArgs e)
  {
   base.OnKeyDown(e);
   if (e.KeyCode == Keys.Enter)
   {
    OnClick(e);
   }
  }
 }
注釋不是很多,源碼如有需要拿走不謝
向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI