這是一個自定義事件的例子。C#、WinForm、Visual Studio 2017。
在HoverTreeForm中畫一塊草地,上面有許多草(模擬)。
HewenqiTianyi類模擬天氣,會引發(fā)“風”事件(HoverTreeWindEvent),風有東風或西風,或靜止。
當吹東風,草往西邊倒,吹西風則往東邊到。靜止則草不會東歪西倒。
草地上每一顆草都監(jiān)聽HoverTreeWindEvent事件,根據(jù)風向(WindDdirection)調整姿態(tài)。
HewenqiTianyi中有定時器,每隔一段時間觸發(fā)調整風向的事件。
由于監(jiān)聽到的“風”事件不是WinForm中的線程,要改變WinForm中“草”的姿態(tài),
使用了BeginInvoke方法和委托,在WinForm線程外訪問控件。具體看HoverTreeGrass用戶控件。
效果圖:

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);      //相當于以下代碼      //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)      {        // 當一個控件的InvokeRequired屬性值為真時,說明有一個創(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="http://";              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 = "http://";            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();    }  }}源碼下載:http://xiazai.VeVB.COm/201707/yuanma/TianYiHeWenQi.rar
總結
以上所述是小編給大家介紹的C#自定義事件模擬風吹草搖擺效果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網(wǎng)網(wǎng)站的支持!
新聞熱點
疑難解答