您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關C#自定義事件模擬風吹草搖擺效果的示例分析的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
這是一個自定義事件的例子。C#、WinForm、Visual Studio 2017。
在HoverTreeForm中畫一塊草地,上面有許多草(模擬)。
HewenqiTianyi類模擬天氣,會引發“風”事件(HoverTreeWindEvent),風有東風或西風,或靜止。
當吹東風,草往西邊倒,吹西風則往東邊到。靜止則草不會東歪西倒。
草地上每一顆草都監聽HoverTreeWindEvent事件,根據風向(WindDdirection)調整姿態。
HewenqiTianyi中有定時器,每隔一段時間觸發調整風向的事件。
由于監聽到的“風”事件不是WinForm中的線程,要改變WinForm中“草”的姿態,
使用了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屬性值為真時,說明有一個創建它以外的線程想訪問它 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(); } } }
感謝各位的閱讀!關于“C#自定義事件模擬風吹草搖擺效果的示例分析”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。