C\#사용자 정의 이벤트 시 뮬 레이 션 바람 풀 흔 들 림 효과

이것 은 사용자 정의 이벤트 의 예 입 니 다.C#、WinForm、Visual Studio 2017。
Hover TreeForm 에 잔디 를 그 렸 는데 그 위 에 많은 풀(시 뮬 레이 션)이 있 습 니 다.
HewenqiTianyi 류 는 날 씨 를 모 의 하면'바람'사건(HoverTreeWindEvent)을 일 으 키 고 바람 이 동풍 이나 서풍 이 있 거나 정지 된다.
동풍 이 불면 풀이 서쪽 으로 넘 어 지고 서풍 이 불면 동쪽 으로 간다.멈 추 면 풀이 이리 저리 쓰 러 지지 않 는 다.
잔디 위의 모든 풀 은 HoverTreeWindEvent 사건 을 감청 하고 풍향(WindD direction)에 따라 자 세 를 조절 한다.
HewenqiTianyi 에는 일정 시간 마다 풍향 을 조정 하 는 이벤트 가 발생 하 는 타이머 가 있 습 니 다.
감 청 된'바람'사건 은 WinForm 의 스 레 드 가 아니 기 때문에 WinForm 의'풀'자 세 를 바 꿔 야 합 니 다.
BeginInvoke 방법 과 의뢰 를 사용 하여 WinForm 스 레 드 밖에서 컨트롤 에 접근 합 니 다.구체 적 으로 Hover TreeGrass 사용자 컨트롤 을 보십시오.
효과 그림:

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\#사용자 정의 이벤트 시 뮬 레이 션 바람 풀 흔 들 림 효과 입 니 다.도움 이 되 셨 으 면 좋 겠 습 니 다.궁금 한 점 이 있 으 시 면 메 시 지 를 남 겨 주세요.편집장 님 께 서 바로 답 해 드 리 겠 습 니 다.여기 서도 저희 사이트 에 대한 여러분 의 지지 에 감 사 드 립 니 다!

좋은 웹페이지 즐겨찾기