根据传入的窗口标题、提示信息(题干)、列表(选项)生成一个带ListBox的窗口(向导界面)。
AHK v1代码
if(A_ScriptFullPath=A_LineFile)
MsgBox % ListBox("窗口标题", "这是一个生成listbox的Demo", "a|b|c|d|",3)
return
;-------------------------------------------------------------------------------
ListBox(Title := "", Prompt := "", List := "", Select := 0) {
;-------------------------------------------------------------------------------
; show a custom input box with a ListBox control
; return the text of the selected item
;
; Title is the title for the GUI
; Prompt is the text to display
; List is a pipe delimited list of choices
; Select (if present) is the index of the preselected item
;---------------------------------------------------------------------------
static Result ; used as a GUI control variable
; create GUI
Gui, ListBox: New, +LastFound, %Title%
Gui, -MinimizeBox
Gui, Margin, 30, 18
Gui, Add, Text,, %Prompt%
Gui, Add, ListBox, w190 r10 vResult Choose%Select%, %List%
Gui, Add, Button, w80 Default, &OK
Gui, Add, Button, x+m wp, &Cancel
; main loop
Gui, Show
WinWaitClose
Return, Result
;-----------------------------------
; event handlers
;-----------------------------------
ListBoxButtonOK: ; "OK" button, {Enter} pressed
Gui, Submit ; get Result from GUI
Gui, Destroy
Return
ListBoxButtonCancel: ; "Cancel" button
ListBoxGuiClose: ; {Alt+F4} pressed, [X] clicked
ListBoxGuiEscape: ; {Esc} pressed
Result := "ListBoxCancel"
Gui, Destroy
Return
}
AHK v2代码
#Requires AutoHotkey v2.0
if (A_ScriptFullPath = A_LineFile)
MsgBox(ListBox("窗口标题", "这是一个生成listbox的Demo", "a|b|c|d|", 3))
;-------------------------------------------------------------------------------
ListBox(Title := "", Prompt := "", List := "", Select := 0) {
;-------------------------------------------------------------------------------
; show a custom input box with a ListBox control
; return the text of the selected item
;
; Title is the title for the GUI
; Prompt is the text to display
; List is a pipe delimited list of choices
; Select (if present) is the index of the preselected item
;---------------------------------------------------------------------------
Result := "" ; used as a GUI control variable
; create GUI
ListBoxGui := Gui("+LastFound", Title)
ListBoxGui.Opt("-MinimizeBox")
ListBoxGui.MarginX:=30
ListBoxGui.Marginy:=18
ListBoxGui.Add("Text",, Prompt)
LB := ListBoxGui.Add("ListBox", "w190 r10 Choose" . Select, StrSplit(List, "|"))
ListBoxGui.Add("Button", "w80 Default", "&OK").OnEvent("Click", (*) => Submit())
ListBoxGui.Add("Button", "x+m wp", "&Cancel").OnEvent("Click", (*) => Cancel())
ListBoxGui.OnEvent("Close", (*) => Cancel())
ListBoxGui.OnEvent("Escape", (*) => Cancel())
; main loop
ListBoxGui.Show()
WinWaitClose("ahk_id " . ListBoxGui.Hwnd)
return Result
;-----------------------------------
; event handlers
;-----------------------------------
; "OK" button, {Enter} pressed
Submit(*) {
Result := LB.Text
ListBoxGui.Destroy()
}
; "Cancel" button
; {Alt+F4} pressed, [X]
; {Esc} pressed
Cancel(*) {
Result := "ListBoxCancel"
ListBoxGui.Destroy()
}
}