Gambas文档
编译和安装
错误消息
代码片段
翻译
废弃的组件
教程
开发环境文档
开发文档
名词解释
如何操作
说明
维基手册
维基搜索
维基许可协议
文档
应用程序仓库
语言概览
语言索引
主题
组件
gb
.Array.Bounds
.Stat.Perm
Application
Args
Array
Boolean[]
Byte[]
Class
Classes
Collection
Component
Components
Date[]
Enum
Env
Error
File
Float[]
gb
Integer[]
Jit
Long[]
Object
Attach
Call
Class
Count
Detach
GetProperty
Is
IsLocked
IsValid
Lock
New
Parent
SetProperty
SizeOf
Type
Unlock
Object[]
Observer
Param
Pointer[]
Process
Short[]
Single[]
Stat
Stream
String
String[]
System
Task
Timer
User
Variant[]
gb.args
gb.cairo
gb.chart
gb.clipper
gb.complex
gb.compress
gb.crypt
gb.data
gb.db
gb.db.form
gb.db.mysql
gb.db.odbc
gb.db.postgresql
gb.db.sqlite2
gb.db.sqlite3
gb.dbus
gb.dbus.trayicon
gb.debug
gb.desktop
gb.desktop.x11
gb.eval
gb.eval.highlight
gb.form
gb.form.dialog
gb.form.editor
gb.form.htmlview
gb.form.mdi
gb.form.print
gb.form.terminal
gb.gmp
gb.gsl
gb.gtk
gb.gtk3
gb.gtk3.opengl
gb.gtk3.webview
gb.gui
gb.gui.qt
gb.gui.qt.ext
gb.gui.trayicon
gb.gui.webview
gb.hash
gb.highlight
gb.image
gb.image.effect
gb.image.io
gb.inotify
gb.logging
gb.map
gb.media
gb.media.form
gb.mime
gb.mysql
gb.ncurses
gb.net
gb.net.curl
gb.net.pop3
gb.net.smtp
gb.opengl
gb.opengl.glsl
gb.opengl.glu
gb.opengl.sge
gb.openssl
gb.option
gb.pcre
gb.pdf
gb.poppler
gb.qt4
gb.qt4.ext
gb.qt4.opengl
gb.qt4.webkit
gb.qt4.webview
gb.qt5
gb.qt5.ext
gb.qt5.opengl
gb.qt5.webview
gb.qt6
gb.qt6.ext
gb.qt6.opengl
gb.qt6.webview
gb.report
gb.report2
gb.sdl
gb.sdl2
gb.sdl2.audio
gb.settings
gb.signal
gb.term
gb.test
gb.util
gb.util.web
gb.v4l
gb.vb
gb.web
gb.web.feed
gb.web.form
gb.web.gui
gb.xml
gb.xml.html
gb.xml.rpc
gb.xml.xslt
最近的修改

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.= 20 + 40 * (15 - i)
  pb.= 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

= (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