这是一个自定义事件的例子。c#、winform、visual studio 2017。
在hovertreeform中画一块草地,上面有许多草(模拟)。
hewenqitianyi类模拟天气,会引发“风”事件(hovertreewindevent),风有东风或西风,或静止。
当吹东风,草往西边倒,吹西风则往东边到。静止则草不会东歪西倒。
草地上每一颗草都监听hovertreewindevent事件,根据风向(windddirection)调整姿态。
hewenqitianyi中有定时器,每隔一段时间触发调整风向的事件。
由于监听到的“风”事件不是winform中的线程,要改变winform中“草”的姿态,
使用了begininvoke方法和委托,在winform线程外访问控件。具体看hovertreegrass用户控件。
效果图:
hewenqitianyi类代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
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); } |
自定义用户控件代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
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窗体代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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(); } } } |
源码下载:tianyihewenqi.rar
总结
以上所述是小编给大家介绍的c#自定义事件模拟风吹草摇摆效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:http://www.cnblogs.com/jihua/archive/2017/08/18/csharpevent.html