发布于 2005-10-10 17:22:30
0楼
问题基本得到解决
参考了无数次帮助 终于差不多可以实现了
主要是想有很多控件要添加而且比较有规律 因此想用VBA实现比较有重复利用的价值
代码如下(比较简单 所以没有注释,请有需要的自行研究)
Public Sub addObject()
Dim objCircle As HMICircle
Dim objRectangle As HMIRectangle
Dim objEllipse As HMIEllipse
Dim objText As HMIStaticText
Dim objText_Dyn As HMIStaticText
Set objCircle = ActiveDocument.HMIObjects.AddHMIObject("sCircle", "HMICircle")
Set objRectangle = ActiveDocument.HMIObjects.AddHMIObject("sRectangle", "HMIRectangle")
Set objEllipse = ActiveDocument.HMIObjects.AddHMIObject("sEllipse", "HMIEllipse")
Set objText = ActiveDocument.HMIObjects.AddHMIObject("sText", "HMIStaticText")
Set objText_Dyn = ActiveDocument.HMIObjects.AddHMIObject("sTextDyn", "HMIStaticText")
With objCircle
.Top = 30: .Left = 0: .Width = 15: .Selected = True
End With
With objRectangle
.Top = 80: .Left = 42: .Width = 40: .Selected = True
End With
With objEllipse
.Top = 48: .Left = 162: .Width = 120: .Selected = True
End With
With objText
.Top = 200: .Left = 200: .Width = 100: .Height = 50
.BackColor = RGB(255, 255, 0): .Text = "test"
End With
With objText_Dyn
.Top = 300: .Left = 300: .Width = 100: .Height = 50
.BackColor = RGB(255, 255, 0): .Text = "DYNTest"
End With
Dim objDyn As HMIVariableTrigger
Dim t
t = "@LocalMachineName"
Set objDyn = objText_Dyn.Text.CreateDynamic(hmiDynamicCreationTypeVariableIndirect, "@LocalMachineName")
objDyn.CycleType = hmiVariableCycleTypeOnChange
Call AddDynamicAsVariableIndirectToProperty
End Sub
Sub AddDynamicAsVariableIndirectToProperty()
'VBA59
Dim objVariableTrigger As HMIVariableTrigger
Dim objCommand As HMIButton
Dim objCircle As HMICircle
Set objCircle = ActiveDocument.HMIObjects.AddHMIObject("Circle2", "HMICircle")
With objCircle
.Top = 400: .Left = 400: .Width = 50: .Selected = True
End With
Set objCommand = ActiveDocument.HMIObjects.AddHMIObject("command", "HMIButton")
With objCommand
.Top = 450: .Left = 450: .Width = 100: .Height = 50: .Text = "set"
End With
'Create dynamic on property "Left":
Set objVariableTrigger = objCircle.Left.CreateDynamic(hmiDynamicCreationTypeVariableDirect, "@RedundantServerState")
Dim objVBs cript As HMIs criptInfo
Dim txtVBS As String
Set objVBs cript = objCommand.Events(1).Actions.AddAction(hmiActionCreationTypeVBs cript)
txtVBS = "dim str" & vbCrLf & "set str=hmiruntime.tags(""LI"")" & vbCrLf & "str.write ""liwenqun333"""
objVBs cript.SourceCode = txtVBS
'Define cycle-time
With objVariableTrigger
.CycleType = hmiVariableCycleTypeOnChange
End With
End Sub
Private Sub Document_BeforeSave(Cancel As Boolean, CancelForwarding As Boolean)
Dim iMaxMembers As Integer
iMaxMembers = ActiveDocument.HMIObjects.Count
If iMaxMembers = 0 Then Call addObject
End Sub
怎么论坛改成这样了??