Collabora Office 21.06 帮助
对话框、文档、表单或图形控件引发的事件可以链接到宏, 这称为「事件驱动编程」。将事件与宏关联的最常见方法是工具 – 自定义菜单中的 选项卡,以及「工具 - 宏 – 组织对话框...」菜单中的「对话框编辑器」控件属性窗格。
Graphical artifacts, keyboard inputs, mouse moves and other man/machine interactions can be controlled using UNO listeners that watch for the user’s behavior. Listeners are dynamic program code alternatives to macro assignments. One may create as many UNO listeners as events to watch for. A single listener can also handle multiple user interface controls.
监听器附加到对话框中的控件, 以及文档或表单事件。在创建运行时对话框或向对话框中动态添加控件时, 也会使用监听器。
本示例为 标准 库中的对话框 Dialog1 的Button1 控件创建监听器。
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import uno, unohelper
from com.sun.star.awt import XActionListener
from com.sun.star.awt import ActionEvent
from com.sun.star.lang import EventObject
from com.sun.star.ui.dialogs.ExecutableDialogResults \
import OK, CANCEL
import msgbox as util
_MY_BUTTON = "Button1"
_MY_LABEL = 'Python 监听中..'
_DLG_PROVIDER = "com.sun.star.awt.DialogProvider"
def Main(*args):
ui = createUnoDialog("Standard.Dialog1", embedded=True)
ui.Title = "Python X[any]Listener"
ctl = ui.getControl(_MY_BUTTON)
ctl.Model.Label = _MY_LABEL
act = ActionListener()
ctl.addActionListener(act)
rc = ui.execute()
if rc == OK:
MsgBox("用户确认了对话框。")
elif rc == CANCEL:
MsgBox("用户取消了对话框。")
ui.dispose() # ui.endExecute
ctl.removeActionListener(act)
def createUnoDialog(libr_dlg: str, embedded=False):
""" 在对话框位置创建对话框 """
smgr = XSCRIPTCONTEXT.getComponentContext().ServiceManager
if embedded:
model = XSCRIPTCONTEXT.getDocument()
dp = smgr.createInstanceWithArguments(_DLG_PROVIDER, (model,))
location = "?location=document"
else:
dp = smgr.createInstanceWithContext(_DLG_PROVIDER, ctx)
location = "?location=application"
dlg = dp.createDialog("vnd.sun.star.script:"+libr_dlg+location)
return dlg
class ActionListener(unohelper.Base, XActionListener):
""" 监听 & 统计按钮点击 """
def __init__(self):
self.count = 0
def actionPerformed(self, evt: ActionEvent):
self.count = self.count + 1
#mri(evt)
if evt.Source.Model.Name == _MY_BUTTON:
evt.Source.Model.Label = _MY_LABEL+ str( self.count )
return
def disposing(self, evt: EventObject): # 必要的例行程序
pass
def MsgBox(txt: str):
mb = util.MsgBox(uno.getComponentContext())
mb.addButton("Ok")
mb.show(txt, 0, "Python")
g_exportedScripts = (Main,)
「msgbox.py」 (位于「{installation}/program/」目录) 有一些按钮监听器的示例。
Option Explicit
Const MY_LIBRARY = "Standard", MY_DIALOG = "Dialog1", MY_BUTTON = "Button1"
Const MY_LABEL = "Basic 正在监听.."
Dim count As Integer
Sub Main
Dim libr As Object ' com.sun.star.script.XLibraryContainer
Dim dlg As Object
Dim ui As Object ' stardiv.Toolkit.UnoDialogControl
Dim ctl As Object ' stardiv.Toolkit.UnoButtonControl
Dim act As Object ' com.sun.star.awt.XActionListener
Dim rc As Object : rc = com.sun.star.ui.dialogs.ExecutableDialogResults
BasicLibraries.LoadLibrary(MY_LIBRARY)
libr = DialogLibraries.GetByName(MY_LIBRARY)
dlg = libr.GetByName(MY_DIALOG)
ui = CreateUnoDialog(dlg)
ui.Title = "Basic X[any]Listener example"
count = 0
ctl = ui.GetControl(MY_BUTTON)
ctl.Model.Label = MY_LABEL
act = CreateUnoListener("awt_", "com.sun.star.awt.XActionListener")
ctl.addActionListener(act)
Select Case ui.Execute
Case rc.OK : MsgBox "用户确认了对话框。",, "Basic"
Case rc.CANCEL : MsgBox "用户取消了对话框。",, "Basic"
End Select
ui.dispose ' ui.endExecute()
ctl.removeActionListener(act)
End Sub
Private Sub awt_actionPerformed(evt As com.sun.star.awt.ActionEvent)
''' 监听 & 统计按钮点击 '''
With evt.Source.Model
If .Name = MY_BUTTON Then
count = count + 1
.Label = MY_LABEL+Cstr(count)
End If
End With
End Sub ' awt_actionPerformed
Private Sub awt_disposing(evt As com.sun.star.lang.EventObject) ' mandatory Sub
' 您的代码放在这里
End Sub ' awt_disposing
监听器通常与对话框打开代码一起编写。可以创建许多不同的监听器,例如用于对话框的事件处理程序,或用于文档或表单的事件监视器。