WinCC画面图层的显示和隐藏
最近在逛西门子论坛时,发现一个有趣的功能,可以通过脚本对画面的图层进行隐藏和显示,不过该帖子只显示了 简单的脚本代码,本着实践是学习的最好方法。我进行了实际操作,并增加了新的功能。下面是我实际操作的步骤和总结
将整个画面作为对象,然后修改对象的属性方法,实现画面图层的隐藏显示
如何将整个画面作为对象
画面中图层号和脚本中索引值的关系
画面图层随机显示
条目 | 描述 |
---|---|
软件版本 | wincc7.5 SP1 |
创建项目
打开wincc 7.5 Advance软件,创建对应的项目,项目名称为LayerVisible。
创建变量.先创建变量组Function,然后创建两个变量(红色框框),其他变量为其他画面用到的,不用于此项目画面。
创建画面,画面名称为Layer01,切记此画面名称,后面创建画面对象时,需要用到。
按钮列表
对象 | 对象名称 | 数量 | 描述 |
---|---|---|---|
图形对象控件 | 默认 | 1 | 显示图片 |
静态文本 | 默认 | 2 | 显示画面名称和图层颜色介绍 |
按钮 | 默认 | 9 | 控制图层的隐藏/显示 |
矩形 | 默认 | 6 | 6个矩形处于画面的6个图层,通过颜色区分 |
按钮描述
按钮名称 | 作用 |
---|---|
图层1 | 点击此按钮,隐藏其他五个矩形,只显示红色矩形 |
图层2 | 点击此按钮,隐藏其他五个矩形,只显示蓝色矩形 |
图层3 | 点击此按钮,隐藏其他五个矩形,只显示黄色矩形 |
图层4 | 点击此按钮,隐藏其他五个矩形,只显示绿色矩形 |
图层5 | 点击此按钮,隐藏其他五个矩形,只显示紫色矩形 |
图层6 | 点击此按钮,隐藏其他五个矩形,只显示白色矩形 |
红绿黄 | 点击此按钮,隐藏其他三个矩形,显示红绿黄三个颜色的矩形 |
蓝白紫 | 点击此按钮,隐藏其他三个矩形,只显示蓝白紫三个颜色的矩形 |
随机 | 点击此按钮,隐藏其他五个矩形,显示一个随机颜色的矩形 |
按钮脚本
图层1
Dim Sobj
Dim VisibleOn
Set Sobj = hmiruntime.Screens('Layer01') '获取画面对象,
Set VisibleOn = HMIRuntime.Tags('VisibleOn') '获取变量
VisibleOn.Write 1 '变量值写1
If VisibleOn.Value = 1 Then '逻辑判断
Sobj.Layers(1).visible = True '显示图层0
Sobj.Layers(2).visible = False '隐藏图层1
Sobj.Layers(3).visible = False
Sobj.Layers(4).visible = False
Sobj.Layers(5).visible = False
Sobj.Layers(6).visible = False
Else
Sobj.Layers(1).visible = False
End If
图层2
Dim Sobj
Dim VisibleOn
Set Sobj = hmiruntime.Screens('Layer01')
Set VisibleOn = HMIRuntime.Tags('VisibleOn')
VisibleOn.Write 2
If VisibleOn.Value = 2 Then
Sobj.Layers(1).visible = False
Sobj.Layers(2).visible = True
Sobj.Layers(3).visible = False
Sobj.Layers(4).visible = False
Sobj.Layers(5).visible = False
Sobj.Layers(6).visible = False
Else
Sobj.Layers(2).visible = False
End If
图层3
Dim Sobj
Dim VisibleOn
Set Sobj = hmiruntime.Screens('Layer01')
Set VisibleOn = HMIRuntime.Tags('VisibleOn')
VisibleOn.Write 3
If VisibleOn.Value = 3 Then
Sobj.Layers(1).visible = False
Sobj.Layers(2).visible = False
Sobj.Layers(3).visible = True
Sobj.Layers(4).visible = False
Sobj.Layers(5).visible = False
Sobj.Layers(6).visible = False
Else
Sobj.Layers(3).visible = False
End If
图层4
Dim Sobj
Dim VisibleOn
Set Sobj = hmiruntime.Screens('Layer01')
Set VisibleOn = HMIRuntime.Tags('VisibleOn')
VisibleOn.Write 4
If VisibleOn.Value = 4 Then
Sobj.Layers(1).visible = False
Sobj.Layers(2).visible = False
Sobj.Layers(3).visible = False
Sobj.Layers(4).visible = True
Sobj.Layers(5).visible = False
Sobj.Layers(6).visible = False
Else
Sobj.Layers(4).visible = False
End If
图层5
Dim Sobj
Dim VisibleOn
Set Sobj = hmiruntime.Screens('Layer01')
Set VisibleOn = HMIRuntime.Tags('VisibleOn')
VisibleOn.Write 5
If VisibleOn.Value = 5 Then
Sobj.Layers(1).visible = False
Sobj.Layers(2).visible = False
Sobj.Layers(3).visible = False
Sobj.Layers(4).visible = False
Sobj.Layers(5).visible = True
Sobj.Layers(6).visible = False
Else
Sobj.Layers(5).visible = False
End If
图层6
Dim Sobj
Dim VisibleOn
Set Sobj = hmiruntime.Screens('Layer01')
Set VisibleOn = HMIRuntime.Tags('VisibleOn')
VisibleOn.Write 6
If VisibleOn.Value = 6 Then
Sobj.Layers(1).visible = False
Sobj.Layers(2).visible = False
Sobj.Layers(3).visible = False
Sobj.Layers(4).visible = False
Sobj.Layers(5).visible = False
Sobj.Layers(6).visible = True
Else
Sobj.Layers(6).visible = False
End If
红绿黄
Dim Sobj
Dim VisibleOn
Set Sobj = hmiruntime.Screens('Layer01')
Set VisibleOn = HMIRuntime.Tags('VisibleOn')
VisibleOn.Write 7
If VisibleOn.Value = 7 Then
Sobj.Layers(1).visible = True
Sobj.Layers(2).visible = False
Sobj.Layers(3).visible = True
Sobj.Layers(4).visible = True
Sobj.Layers(5).visible = False
Sobj.Layers(6).visible = False
Else
Sobj.Layers(1).visible = False
Sobj.Layers(3).visible = False
Sobj.Layers(4).visible = False
End If
蓝白紫
Dim Sobj
Dim VisibleOn
Set Sobj = hmiruntime.Screens('Layer01')
Set VisibleOn = HMIRuntime.Tags('VisibleOn')
VisibleOn.Write 8
If VisibleOn.Value = 8 Then
Sobj.Layers(1).visible = False
Sobj.Layers(2).visible = True
Sobj.Layers(3).visible = False
Sobj.Layers(4).visible = False
Sobj.Layers(5).visible = True
Sobj.Layers(6).visible = True
Else
Sobj.Layers(2).visible = False
Sobj.Layers(5).visible = False
Sobj.Layers(6).visible = False
End If
随机
Dim Sobj
Set Sobj = HMIRuntime.Screens('Layer01')
HMIRuntime.Tags('ColorNumber').Write MyRnd(1,6)
Msgbox HMIRuntime.Tags('ColorNumber').Read
If HMIRuntime.Tags('ColorNumber').Read = 1 Then
Sobj.Layers(1).visible = True
Sobj.Layers(2).visible = False
Sobj.Layers(3).visible = False
Sobj.Layers(4).visible = False
Sobj.Layers(5).visible = False
Sobj.Layers(6).visible = False
Elseif HMIRuntime.Tags('ColorNumber').Read = 2 Then
Sobj.Layers(1).visible = False
Sobj.Layers(2).visible = True
Sobj.Layers(3).visible = False
Sobj.Layers(4).visible = False
Sobj.Layers(5).visible = False
Sobj.Layers(6).visible = False
Elseif HMIRuntime.Tags('ColorNumber').Read = 3 Then
Sobj.Layers(1).visible = False
Sobj.Layers(2).visible = False
Sobj.Layers(3).visible = True
Sobj.Layers(4).visible = False
Sobj.Layers(5).visible = False
Sobj.Layers(6).visible = False
Elseif HMIRuntime.Tags('ColorNumber').Read = 4 Then
Sobj.Layers(1).visible = False
Sobj.Layers(2).visible = False
Sobj.Layers(3).visible = False
Sobj.Layers(4).visible = True
Sobj.Layers(5).visible = False
Sobj.Layers(6).visible = False
Elseif HMIRuntime.Tags('ColorNumber').Read = 5 Then
Sobj.Layers(1).visible = False
Sobj.Layers(2).visible = False
Sobj.Layers(3).visible = False
Sobj.Layers(4).visible = False
Sobj.Layers(5).visible = True
Sobj.Layers(6).visible = False
Elseif HMIRuntime.Tags('ColorNumber').Read = 6 Then
Sobj.Layers(1).visible = False
Sobj.Layers(2).visible = False
Sobj.Layers(3).visible = False
Sobj.Layers(4).visible = False
Sobj.Layers(5).visible = False
Sobj.Layers(6).visible = True
Else
Sobj.Layers(1).visible = False
Sobj.Layers(2).visible = False
Sobj.Layers(3).visible = False
Sobj.Layers(4).visible = False
Sobj.Layers(5).visible = False
Sobj.Layers(6).visible = False
End If
在使用随机按钮时,需要先创建项目函数MyRnd(min,max),此函数会出现一个随机函数,min代表随机数的下限,max为上限,产生的随机数为int型。
赞 (0)