C#自定义事件模拟风吹草摇摆效果
程序员文章站
2023-12-05 20:01:52
这是一个自定义事件的例子。c#、winform、visual studio 2017。
在hovertreeform中画一块草地,上面有许多草(模拟)。
hewenqi...
这是一个自定义事件的例子。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(); } } }
源码下载:http://xiazai.jb51.net/201707/yuanma/tianyihewenqi.rar
总结
以上所述是小编给大家介绍的c#自定义事件模拟风吹草摇摆效果,希望对大家有所帮助
上一篇: Python单元测试_使用装饰器实现测试跳过和预期故障的方法
下一篇: jdk自带定时器使用方法详解
推荐阅读