这是一个自定义事件的例子。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);}@H_301_28@
自定义用户控件代码:
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); } }}@H_301_28@
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(); } }}@H_301_28@
源码下载:http://xiazai.jb51.net/201707/yuanma/TianYiHeWenQi.rar
总结
以上所述是小编给大家介绍的C#自定义事件模拟风吹草摇摆效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!
总结以上是内存溢出为你收集整理的C#自定义事件模拟风吹草摇摆效果全部内容,希望文章能够帮你解决C#自定义事件模拟风吹草摇摆效果所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)