首次访问?欢迎登录zenon工业软件技术论坛!
0

如何在zenon运行时创建C#事件

我需在点击一个画面、画面元件或变量值或状态变化时自定义一个C#的功能,如何在zenon项目运行时可以实现?

匿名用户
提问于 2024-08-09 14:47:12 +0800
编辑 标识违禁 0 移除标识 关闭 合并 删除

评论

添加评论 显示更多评论
0

可以使用zenon的AddIn功能来实现创建自定义事件的需求,具体如下:

1、打开Visual Studio 2022

2、选择Project Service Extension创建程序

3、在public void Start(IProject context, IBehavior behavior)过程中创建你所需要的事件

4、在触发事件中定义你所需执行的功能

5、最后,不要忘记在public void Stop()中删除你所创建的事件

以下是一个触发事件的代码示例

C#

using System;

using Scada.AddIn.Contracts;

using Scada.AddIn.Contracts.Screen;

using System.Linq;

using System.Diagnostics;

namespace Samples

{

[AddInExtension("ScreenElement.ScreenElement.Move", "Moves the Button around", "Samples")]

public class EditorWizardExtension_ScreenElement_ScreenElement_Move :IProjectServiceExtension
{
IScreenCollection screenCollection = null;//we keep a reference of screencolltection so we don't loose the reference to our events
public void Start(IProject context, IBehavior behavior)
{
  screenCollection = context.ScreenCollection;

  //We look for the Screen named "SIMUL Information"(which may be create with projec wizard) 
  //and and simulate 
  if (!screenCollection.Any(item => item.Name == "Start"))
  {
    //we didn't find the screen return
    Debug.Print("Screen not found!");
    return;
  }
  screenCollection.ElementLeftButtonDoubleClick += ScreenCollection_ElementLeftButtonDoubleClick;
  screenCollection.ElementLeftButtonDown += ScreenCollection_ElementLeftButtonDown;
  screenCollection.ElementLeftButtonUp += ScreenCollection_ElementLeftButtonUp;
  screenCollection.ElementRightButtonDoubleClick += ScreenCollection_ElementRightButtonDoubleClick;
  screenCollection.ElementRightButtonDown += ScreenCollection_ElementRightButtonDown;
  screenCollection.ElementRightButtonUp += ScreenCollection_ElementRightButtonUp;
}

private void ScreenCollection_ElementRightButtonUp(object sender, ElementRightButtonUpEventArgs e)
{
  //if a element is RightButtonUp we move to upper right corner
  if (e.Element.Name == "MyButton")
  {
    e.Element.Move(1600, 0, 1700, 110);
  }
}

private void ScreenCollection_ElementRightButtonDown(object sender, ElementRightButtonDownEventArgs e)
{
  //if a element is RightButtonDown we move to lower right corner
  if (e.Element.Name == "MyButton")
  {
    e.Element.Move(1600, 800, 1200, 900);
  }
}

private void ScreenCollection_ElementRightButtonDoubleClick(object sender, ElementRightButtonDoubleClickEventArgs e)
{
  //if a element is right double clicked we move to middle right
  if (e.Element.Name == "MyButton")
  {
    e.Element.Move(1600, 400, 1700, 500);
  }
}

private void ScreenCollection_ElementLeftButtonUp(object sender, ElementLeftButtonUpEventArgs e)
{
  //on if a element is LeftButtonUp we move to upper left corner
  if (e.Element.Name == "MyButton")
  {
    e.Element.Move(0, 0, 100, 100);
  }
}

private void ScreenCollection_ElementLeftButtonDown(object sender, ElementLeftButtonDownEventArgs e)
{
  // on if a element is LeftButtonDown we move to lower left corner
  if (e.Element.Name == "MyButton")
  {
    e.Element.Move(0, 800, 100, 900);
  }
}

private void ScreenCollection_ElementLeftButtonDoubleClick(object sender, ElementLeftButtonDoubleClickEventArgs e)
{
  // on if a element is LeftDoubleClicke we move left side
  if (e.Element.Name == "MyButton")
  {
    e.Element.Move(0, 400, 100, 500);
  }
}

public void Stop()
{
  //Remove all eventhandler
  screenCollection.ElementLeftButtonDoubleClick -= ScreenCollection_ElementLeftButtonDoubleClick;
  screenCollection.ElementLeftButtonDown -= ScreenCollection_ElementLeftButtonDown;
  screenCollection.ElementLeftButtonUp -= ScreenCollection_ElementLeftButtonUp;
  screenCollection.ElementRightButtonDoubleClick -= ScreenCollection_ElementRightButtonDoubleClick;
  screenCollection.ElementRightButtonDown -= ScreenCollection_ElementRightButtonDown;
  screenCollection.ElementRightButtonUp -= ScreenCollection_ElementRightButtonUp;
}

}

}

administrator的头像
31
administrator
回答 2024-08-09 14:55:35 +0800, 已更新 2024-08-09 15:04:33 +0800
编辑 标识违禁 0 移除标识 删除 链接

评论

添加评论 显示更多评论