C#自定義事件模擬風(fēng)吹草搖擺效果
這是一個(gè)自定義事件的例子。C#、WinForm、Visual Studio 2017。
在HoverTreeForm中畫一塊草地,上面有許多草(模擬)。
HewenqiTianyi類模擬天氣,會引發(fā)“風(fēng)”事件(HoverTreeWindEvent),風(fēng)有東風(fēng)或西風(fēng),或靜止。
當(dāng)吹東風(fēng),草往西邊倒,吹西風(fēng)則往東邊到。靜止則草不會東歪西倒。
草地上每一顆草都監(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用戶控件。
效果圖:
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(); } } }
源碼下載:http://xiazai.jb51.net/201707/yuanma/TianYiHeWenQi.rar
總結(jié)
以上所述是小編給大家介紹的C#自定義事件模擬風(fēng)吹草搖擺效果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
DevExpress實(shí)現(xiàn)自定義GridControl中按鈕文字內(nèi)容的方法
這篇文章主要介紹了DevExpress實(shí)現(xiàn)自定義GridControl中按鈕文字內(nèi)容的方法,需要的朋友可以參考下2014-08-08基于C#解決庫存扣減及訂單創(chuàng)建時(shí)防止并發(fā)死鎖的問題
這篇文章主要介紹了基于C#解決庫存扣減及訂單創(chuàng)建時(shí)防止并發(fā)死鎖的問題,很多開發(fā)人員對于這個(gè)問題的排查起來是比較困難的,而生產(chǎn)生的原因多種多樣,很多人認(rèn)是因?yàn)楸碇械臄?shù)據(jù)太多了同時(shí)操作的人多人才會產(chǎn)生這種錯誤,下面我們來還原一下死鎖的過程2022-05-05C#實(shí)現(xiàn)類似jQuery的方法連綴功能
這篇文章主要介紹了C#實(shí)現(xiàn)類似jQuery的方法連綴功能,可以簡化語句,使代碼變得清晰簡單,感興趣的小伙伴們可以參考一下2015-11-11Unity的BuildPlayerProcessor實(shí)用案例深入解析
這篇文章主要為大家介紹了Unity的BuildPlayerProcessor實(shí)用案例深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05