Object.Attach (gb)
Static Sub Attach ( Object As Object, Parent As Object, Name As String )
挂接对象到其母体。
Name 是用于在_Parent_(母体)对象中查找事件处理程序的名称。
对象发出的每个事件将被位于其母体中的事件处理程序管理。
如果母体是一个类,那么事件处理程序将是该类的静态方法。
接下来的代码:
hObject = NEW MyClass
Object.Attach(hObject, ME, "EventName")
等价于:
hObject = NEW MyClass AS "EventName"
Examples
PUBLIC Process1 AS Process
...
Process1 = SHELL "find /" FOR READ
Object.Attach(Process1, ME, "Process1")
...
PUBLIC SUB Process1_Read()
Message.Info("Got output from Process1!")
' and then read and do something with the output...
END
下面的示例将创建16个图片框,并且每次图片框的中的一个被单击(MouseUp),然后在iSwtch中的相应的数据位和
Picture被切换。
这里演示一个控件数组的元素怎样能接收一个信号,在这个示例中信号是"MouseUp"
PUBLIC pbSwtch AS Object[16]
PUBLIC iSwtch AS Integer ' 全部16个数据开关的状态
PUBLIC imgSwtchOff AS Picture ' 这个图片显示一个关闭的开关状态
PUBLIC imgSwtchOn AS Picture ' 这个图片显示一个打开的开关状态
PUBLIC SUB Form_Show()
DIM i AS Integer
DIM pb AS Object
imgSwtchOff = Picture["imgSwtchOff.png"]
imgSwtchOn = Picture["imgSwtchOn.png"]
FOR i = 0 TO 15
pb = NEW PictureBox(ME) ' 创建一个新的PictureBox,返回其句柄给pb
pb.X = 20 + 40 * (15 - i)
pb.Y = 60
pb.Width = 32
pb.Height = 32
pb.Picture = imgSwtchOff
pb.Name = "pbSwtch"
pbSwtch[i] = pb
Object.Attach(pbSwtch[i], ME, "pbSwtch")
NEXT
END
PUBLIC SUB pbSwtch_MouseUp()
DIM i AS Integer
DIM togglemask AS Integer
i = (Mouse.ScreenX - 20) / 40 ' 点击的是16个开关中的哪一个?
IF i >= 0 AND i < 16 THEN
i = 15 - i
togglemask = Shl(1, i)
iSwtch = iSwtch XOR togglemask
IF iSwtch AND togglemask THEN
pbSwtch[i].Picture = imgSwtchOn
ELSE
pbSwtch[i].Picture = imgSwtchOff
ENDIF
ENDIF
END