[AHK]动态创建带ListBox的窗口,答选择题的界面

根据传入的窗口标题、提示信息(题干)、列表(选项)生成一个带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()
    }
    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值