Пример программы:
Set wso = WScript.CreateObject("Scripting.WindowSystemObject") wso.EnableVisualStyles = true Set form = wso.CreateForm(0,0,0,0) form.ClientWidth = 150 form.ClientHeight = 100 form.CenterControl() Set button = form.CreateButton(10,10,75,25,"Close") button.OnClick = GetRef("ButtonClick") Sub ButtonClick(this) form.Close() End Sub form.Show() wso.Run()
Set o = WScript.CreateObject("Scripting.WindowSystemObject")
Обработчик события является функцией вида:
Function Имя(Sender,дополнительные параметры) Операторы End Function
или
Sub Имя (Sender,дополнительные параметры) Операторы End Sub
В VBScript для задания обработчика требуется использовать оператор GetRef, например:
Function MyEventHandler(Sender) Sender.Form.MessageBox "My Event Handler" End Function Button.OnClick = GetRef("MyEventHandler") Button.AddEventHandler "OnClick",GetRef("MyEventHandler")
При этом VBScript в отличие от JScript жестко следит за количеством параметров у функций.
Примеры на языке VBScript находятся в каталоге WSO в подкаталоге Demo\VBScript.
Один из примеров:
'WindowSystemObject (WSO) example 'Copyright (C) Veretennikov A. B. 2004 Set o = WScript.CreateObject("Scripting.WindowSystemObject") Set f = o.CreateForm(0,0,650,400) f.CenterControl() Sub MouseMove(this,x,y,flags) this.Form.StatusBar.item(0).Text = CStr(x)+" x "+CStr(y) End Sub Sub MouseExit(this) this.Form.StatusBar.item(0).Text = "No Mouse In Control" End Sub Sub MouseUp(this,x,y,Button,Flags) this.Form.MessageBox "MouseUp "+CStr(x)+" x "+CStr(y)+", Button = "+CStr(Button) End Sub Sub KeyDown(this,Key,Flags) If ((Key <> 27) And (Key <> 112)) Then this.Form.MessageBox "KeyDown "+CStr(Key) End Sub Sub ButtonClick(this) this.Form.MessageBox "Button "+this.Text+": OnClick" End Sub int CanCloseVar Sub OKResult(this) CanCloseVar = 1 this.Form.Close() End Sub Function CanClose(Sender,Result) CanCloseVar = 0 Set CanCloseDialog = f.CreateDialogForm(0,0,510,100) CanCloseDialog.CenterControl() CanCloseDialog.TextOut 10,10,"Do you really want to quit this program?" Set YesButton = CanCloseDialog.CreateButton(10,30,75,25,"Yes") YesButton.OnClick = GetRef("OKResult") YesButton.Default = true Set NoButton = CanCloseDialog.CreateButton(90,30,75,25,"No") NoButton.OnClick = GetRef("CloseFormHandler") NoButton.Cancel = true Set HelpButton = CanCloseDialog.CreateButton(170,30,75,25,"Help") HelpButton.OnClick = GetRef("HelpAbout") CanCloseDialog.HelpButton = HelpButton CanCloseDialog.Color = &H00FF00FF CanCloseDialog.ShowModal() CanCloseDialog.Destroy() If CanCloseVar = 1 Then Result.Put(true) Else Result.Put(false) End Function Set Button = f.CreateButton(10,10,75,25,"Demo") Button.AddEventHandler "OnClick",GetRef("ButtonClick") Set CancelButton = f.CreateButton(100,10,75,25,"Close") CancelButton.OnClick = GetRef("CloseFormHandler") f.CreateStatusBar().Name = "StatusBar" f.StatusBar.Add(100).AutoSize = true f.OnMouseMove = GetRef("MouseMove") f.OnMouseLeave = GetRef("MouseExit") f.OnMouseUp = GetRef("MouseUp") f.OnKeyDown = GetRef("KeyDown") f.OnCloseQuery = GetRef("CanClose") Set Text1 = f.TextOut(10,60,"WindowSystemObject") Text1.Font.Size = 16 Text1.Color = 255 Text1.Font.Bold = true f.TextOut 10,100,"This sample shows how to handle events from WindowsSystemObject using VBScript."+vbCr+vbCr+"Clicking any mouse button invokes OnMouseUp event handler."+vbCr+vbCr+"Moving mouse inside this window invokes OnMouseMove event handler."+vbCr+vbCr+"(The current mouse position is displayed in the status bar)"+vbCr+vbCr+"Moving mouse outside this window invokes OnMouseLeave event handler."+vbCr+vbCr+"Clicking Demo or Close buttons invokes OnClick event handler."+vbCr+vbCr+"Pressing any key invokes OnKeyDown event handler."+vbCr+vbCr+"Try to close this window to invoke OnCloseQuery event." Set FileMenu = f.Menu.Add("File") FileMenu.Add("Exit",27).OnExecute = GetRef("CloseFormHandler") Set HelpMenu = f.Menu.Add("Help") HelpMenu.Add("About","F1").OnExecute = GetRef("HelpAbout") f.Show() o.Run() Sub HelpAbout(Sender) o.About() End Sub Function StartupDir() Dim s s = WScript.ScriptFullName s = Left(s,InStrRev(s,"\")) StartupDir = s End Function Sub WSOHelp(Sender) Set shell = WScript.CreateObject("WScript.Shell") shell.Run """"+StartupDir() + "..\..\wso.chm"+"""" End Sub Sub AboutWSO_OnHitTest(Sender,x,y,ResultPtr) ResultPtr.put(o.Translate("HTCAPTION")) End Sub Sub CloseFormHandler(Sender) Sender.Form.Close() End Sub