这篇文章给大家分享的是有关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#自定义事件模拟风吹草摇摆效果的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。