溫馨提示×

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

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C#自定義事件模擬風(fēng)吹草搖擺效果的示例分析

發(fā)布時(shí)間:2021-06-24 09:39:07 來源:億速云 閱讀:160 作者:小新 欄目:編程語言

這篇文章給大家分享的是有關(guān)C#自定義事件模擬風(fēng)吹草搖擺效果的示例分析的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

這是一個(gè)自定義事件的例子。C#、WinForm、Visual Studio 2017。

在HoverTreeForm中畫一塊草地,上面有許多草(模擬)。

HewenqiTianyi類模擬天氣,會(huì)引發(fā)“風(fēng)”事件(HoverTreeWindEvent),風(fēng)有東風(fēng)或西風(fēng),或靜止。

當(dāng)吹東風(fēng),草往西邊倒,吹西風(fēng)則往東邊到。靜止則草不會(huì)東歪西倒。

草地上每一顆草都監(jiān)聽HoverTreeWindEvent事件,根據(jù)風(fēng)向(WindDdirection)調(diào)整姿態(tài)。

HewenqiTianyi中有定時(shí)器,每隔一段時(shí)間觸發(fā)調(diào)整風(fēng)向的事件。

由于監(jiān)聽到的“風(fēng)”事件不是WinForm中的線程,要改變WinForm中“草”的姿態(tài),

使用了BeginInvoke方法和委托,在WinForm線程外訪問控件。具體看HoverTreeGrass用戶控件。

效果圖:

C#自定義事件模擬風(fēng)吹草搖擺效果的示例分析

HewenqiTianyi類代碼:

using System;
using System.Timers;
namespace TianYiHeWenQi
{
  class HewenqiTianyi
  {
    public static event ActionEventHandler HoverTreeWindEvent;
    WindEventArgs _arg = new WindEventArgs();
    public HewenqiTianyi()
    {
      Timer h_timer = new Timer();
      h_timer.Interval = 3000;
      h_timer.Elapsed += H_timer_Elapsed;
      h_timer.Start();
    }
    Random _HoverClock=new Random ();
    private void H_timer_Elapsed(object sender, ElapsedEventArgs e)
    {
      _arg.WindType = (WindDdirection)(_HoverClock.Next(3));
      OnAction(_arg);
    }
    protected void OnAction(WindEventArgs ev)
    {
      HoverTreeWindEvent?.Invoke(ev);
      //相當(dāng)于以下代碼
      //if (HoverTreeWindEvent != null)
      //{
      //  HoverTreeWindEvent(ev);
      //}
    }
  }
  class WindEventArgs : EventArgs
  {
    public WindDdirection WindType { get; set; }
  }
  enum WindDdirection
  {
    East,
    West,
    Static
  }
  delegate void ActionEventHandler(WindEventArgs ev);
}

自定義用戶控件代碼:

using System;
using System.Windows.Forms;
namespace TianYiHeWenQi
{
  public partial class HoverTreeGrass : UserControl
  {
    delegate void MySetText(string text);
    public HoverTreeGrass()
    {
      InitializeComponent();
      HewenqiTianyi.HoverTreeWindEvent += HewenqiTianyi_HoverTreeWindEvent;
    }
    private void UpdateLabel(WindDdirection wd)
    {
      if (label_grass.InvokeRequired)
      {
        // 當(dāng)一個(gè)控件的InvokeRequired屬性值為真時(shí),說明有一個(gè)創(chuàng)建它以外的線程想訪問它
        Action<WindDdirection> actionDelegate = (x) => {
          switch (x)
          {
            case WindDdirection.East:
              label_grass.Location = new System.Drawing.Point(40 - 9, label_grass.Location.Y);
              label_grass.Text="\\";
              break;
            case WindDdirection.West:
              label_grass.Location = new System.Drawing.Point(40+9, label_grass.Location.Y);
              label_grass.Text = "/";
              break;
            case WindDdirection.Static:
              label_grass.Location = new System.Drawing.Point(40, label_grass.Location.Y);
              label_grass.Text = "|";
              break;
          }
        };
        // 或者
        // Action<string> actionDelegate = delegate(string txt) { this.label_grass.Text = txt; };
        this.label_grass.BeginInvoke(actionDelegate, wd);
      }
      else
      {
        switch (wd)
        {
          case WindDdirection.East:
            label_grass.Text = "\\";
            break;
          case WindDdirection.West:
            label_grass.Text = "/";
            break;
          case WindDdirection.Static:
            label_grass.Text = "|";
            break;
        }
      }
    }
    private void HewenqiTianyi_HoverTreeWindEvent(WindEventArgs ev)
    {
      UpdateLabel(ev.WindType);
    }
  }
}

HoverTreeForm窗體代碼:

using System.Windows.Forms;
namespace TianYiHeWenQi
{
  public partial class HoverTreeForm : Form
  {
    public HoverTreeForm()
    {
      InitializeComponent();
      for (int i = 0; i < tableLayoutPanel_hovertree.ColumnCount; i++) {
        for (int j = 0; j < tableLayoutPanel_hovertree.RowCount; j++) {
          tableLayoutPanel_hovertree.Controls.Add(new HoverTreeGrass(), i, j);
        }
      }
      HewenqiTianyi h_ty = new HewenqiTianyi();
    }
  }
}

感謝各位的閱讀!關(guān)于“C#自定義事件模擬風(fēng)吹草搖擺效果的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI