入力フォームなどに使えるGUIを作成/操作する
下記のサブコマンドがある。
説明の量が多いので項を分ける。
ウィンドウはAddで最初にコントロールが配置されるときに作られる。
ウィンドウのクラス名は「AutoHotkeyGUI」。
Gui,Showでタイトルを指定して表示する前のタイトルは、スクリプトのファイル名部分。
スクリプトは最大で99のウィンドウを持つことが出来る。
各ウィンドウには最大11000個のコントロールを配置できる。
デフォルト以外のウィンドウを操作するには、サブコマンドの前に「2:」のようにウィンドウ番号とコロンを追加する。
Gui, 2:Add, Text,, Text for about box. Gui, 2:Show
基本的にデフォルトのウィンドウ番号は「1」だが、2番目以降のウィンドウ上のGUI操作イベント(コントロールの操作時やメニューバーのアイテムなど)で実行されたスレッドでは、そのウィンドウがデフォルトウィンドウとなる。
デフォルトウィンドウは、Gui,2:Defaultのようにして変更できる。
Fontサブコマンドでのフォントの設定は、ウィンドウごとに保持される。
GuiEscape、GuiCloseなどの特別な名前のサブルーチンラベルが存在すると、ウィンドウに対して特定の操作が行われたときそのサブルーチンが実行されるようになる。
現在用意されている特殊サブルーチンには以下のものがある。
2番目以降のウィンドウのイベントは、「2GuiEscape」のように前に数字を追加した名前のラベルで定義する。
Gui,Cancelと同じ動作)
Gui,-E0x00000010」で拡張スタイルWS_EX_ACCEPTFILESを解除すると、一時的にドラッグ&ドロップ受付を無効にできる。Gui,+E0x00000010」で再び有効にできる。
下記のようにサブルーチンの前に複数のラベルを書けば、複数のイベントにひとつの動作を割り当てられる。
GuiEscape: GuiClose: ButtonCancel: Gui, Cancel ; All of the above labels will execute this subroutine. return
GUIコマンドのテストをするときは、下記の割り当てがお勧め。
普通にウィンドウを閉じただけではスクリプトが終了しないので、ウィンドウを閉じたときにスクリプトが終了するようにしてやる。
GuiClose: ExitApp
Buttonコントロールでは、「G」オプションでサブルーチンを指定しなくても、自動的にデフォルトのラベルが割り当てられる。
たとえば、Text引数が「Text」のボタンでは、「ButtonText」というラベルに割り当てられる。(該当するラベルが存在すれば)
ただし、Text引数に半角スペースや「&」、改行文字などが含まれる場合、それらの文字を除いた文字列が使用される。
2番目以降のウィンドウのボタンについては、「2ButtonText」のような最初にウィンドウ番号をつけた名前のラベルに割り当てられる。
GUI,Add,Button,,Exit ButtonExit: ExitApp
作成したウィンドウは、一般的なウィンドウのようにTabキーでフォーカスを別のコントロールに移動することが出来る。
移動する順序は、コントロールがウィンドウに追加された順番。
ウィンドウが表示されるときは、最初のNoTabオプションを指定されていないフォーカスをもてるコントロールにフォーカスがセットされる。
一部のコントロールは、テキストに「&A」のように&+英数字を入れることで、アクセスキーを設定できる。
アクセスキーは「A」のようにアンダーライン付きの文字で表示され、Alt+文字キーでそのコントロールをクリックしたときの動作が行われる。
TextやGroupBoxでは、次に追加したコントロールにフォーカスが移る。
複数のコントロールに同じアクセスキーが割り当てられている場合、アクセスキーを押すごとに次の該当するコントロールにフォーカスが移動する。
「&&」と書くことで、テキスト中に「&」を表示することが出来る。
コントロールを追加する際、X/Yオプションで座標を指定しなかった場合、コントロールの位置は自動的に決定される。
最初に追加されるコントロールは、ウィンドウの左上に配置され、2つ目以降のコントロールは、直前のコントロールの下側にほかのコントロールと重ならないように配置される。
X/Yのうち片方だけ指定した場合、もうひとつの座標だけが自動的に決定される。
コントロールのサイズも、コントロールの種類やテキストの内容などによって自動的に決定される。
自動レイアウト時には、ほかのコントロールから一定の間隔をあけて配置される。
デフォルトでは、上下の間隔は選択されているフォントの高さの1.25倍、左右の間隔は0.75倍になる。
マージンの幅は、Gui,Margin」で変更できる。
「Gui,Show」でウィンドウが最初に表示されるときにも、幅や高さが指定されない限り、このマージンを考慮してサイズが決定される。
ウィンドウのアイコンには、ウィンドウが作成されるときにタスクトレイアイコンでして使用されていたアイコンが使用される。
タスクトレイアイコンは「Menu, Tray, Icon」で設定できる。
ウィンドウコントロールの操作によって新規スレッドでサブルーチンが呼び出される際には、LastFoundWindowとしてコントロールを持つウィンドウが記憶される。
これにより、各種ウィンドウ操作コマンドで当該ウィンドウを操作したいとき、ウィンドウの指定を省略することが出来る。
GUIコマンドを含んでいるスクリプトは、常駐状態かつ#SingleInstance(複数実行不可)になる。
作成したGUI上のコントロールを操作するには、GuiControl、GuiControlGetコマンドを使用する。
GuiControl, GuiControlGet, Menu, Control, ControlGet, SplashImage, MsgBox, FileSelectFile, FileSelectFolder
Gui, add, text,, First name: Gui, add, text,, Last name: Gui, add, edit, vFirstName ym ; The ym option starts a new column of controls. Gui, add, edit, vLastName Gui, add, button, default, OK ; The label ButtonOK (if it exists) will be run when the button is pressed. Gui, show,, Simple Input Example return ; End of auto-execute section. The script is idle until the user does something. GuiClose: ButtonOK: Gui, submit ; Save the input from the user to each control's associated variable. MsgBox You entered "%FirstName% %LastName%". ExitApp
Gui, add, tab,, First Tab|Second Tab|Third Tab Gui, add, checkbox, vMyCheckbox, Sample checkbox Gui, tab, 2 Gui, add, radio, vMyRadio, Sample radio1 Gui, add, radio,, Sample radio2 Gui, tab, 3 Gui, add, edit, vMyEdit r5 ; r5 means 5 rows tall. Gui, tab ; i.e. subsequently-added controls will not belong to the tab control. Gui, add, button, default xm, OK ; xm puts it at the bottom left corner. Gui, show return ButtonOK: GuiClose: GuiEscape: Gui, submit MsgBox You entered:`n%MyCheckbox%`n%MyRadio%`n%MyEdit% ExitApp
Gui, Add, Text,, Pick a file to launch from the list below.`nTo cancel, press ESCAPE or close this window.
Gui, Add, ListBox, vMyListBox gMyListBox w640 r10
Gui, Add, Button, Default, OK
Loop, C:\*.* ; Change this folder and wildcard pattern to suit your preferences.
{
GuiControl,, MyListBox, %A_LoopFileFullPath%
}
Gui, Show
return
MyListBox:
if A_GuiControlEvent <> DoubleClick
return
; Otherwise, the user double-clicked a list item, so treat that the same as pressing OK.
; So fall through to the next label.
ButtonOK:
GuiControlGet, MyListBox ; Retrieve the ListBox's current selection.
MsgBox, 4,, Would you you like to launch the file or document below?`n`n%MyListBox%
IfMsgBox, No
return
; Otherwise, try to launch it:
Run, %MyListBox%,, UseErrorLevel
if ErrorLevel = ERROR
MsgBox Could not launch the specified file. Perhaps it is not associated with anything.
return
GuiClose:
GuiEscape:
ExitApp
CustomColor = EEAA99 ; Can be any RGB color (it will be made transparent below). Gui, Color, %CustomColor% Gui, Font, s24 Gui, Add, Text, vMyText cLime, XXXXX YYYYY ; XX & YY serve to auto-size the window. Gui, +AlwaysOnTop +LastFound ; Make it always-on-top and make it the last found window. ; Make all pixels of this color transparent and make the text itself translucent (150): WinSet, TransColor, %CustomColor% 150 Gui, -Caption ; Remove the borders. Due to a quirk in Windows, this must be done after transparency. SetTimer, UpdateOSD, 500 Gosub, UpdateOSD ; Make the first update immediate rather than waiting for the timer. Gui, Show, x0 y400 return UpdateOSD: MouseGetPos, MouseX, MouseY GuiControl,, MyText, X%MouseX%, Y%MouseY% return
Gui, +Resize
Gui, Add, Button, default, &Load New Image
Gui, Add, Radio, ym+5 x+10 vRadio checked, Load &actual size
Gui, Add, Radio, ym+5 x+10, Load to &fit screen
Gui, Add, Pic, xm vPic
Gui, Show
return
ButtonLoadNewImage:
FileSelectFile, file,,, Select an image:, Images (*.gif; *.jpg; *.bmp; *.png; *.tif; *.ico; *.cur; *.ani; *.exe; *.dll)
if file =
return
Gui, Submit, NoHide ; Save the values of the radio buttons.
if Radio = 1 ; Display image at its actual size.
{
width = 0
height = 0
}
else ; Second radio is selected: Resize the image to fit the screen.
{
width = %A_ScreenWidth%
Width -= 28 ; Allow room for borders and margins inside.
Height = -1 ; Keep aspect ratio seems best.
}
GuiControl,, Pic, *w%width% *h%height% %file%
Gui, Show, xCenter y0 AutoSize, %file% ; Resize the window to match the picture size.
return
GuiClose:
ExitApp
; Create the sub-menus for the menu bar:
Menu, FileMenu, Add, &New, FileNew
Menu, FileMenu, Add, &Open, FileOpen
Menu, FileMenu, Add, &Save, FileSave
Menu, FileMenu, Add, Save &As, FileSaveAs
Menu, FileMenu, Add ; Separator line.
Menu, FileMenu, Add, E&xit, FileExit
Menu, HelpMenu, Add, &About, HelpAbout
; Create the menu bar by attaching the sub-menus to it:
Menu, MyMenuBar, Add, &File, :FileMenu
Menu, MyMenuBar, Add, &Help, :HelpMenu
; Attach the menu bar to the window:
Gui, Menu, MyMenuBar
; Create the main Edit control and display the window:
Gui, +Resize ; Make the window resizable.
Gui, Add, Edit, vMainEdit W600 R20
Gui, Show,, Untitled
CurrentFileName = ; Indicate that there is no current file.
return
FileNew:
GuiControl,, MainEdit ; Clear the Edit control.
return
FileOpen:
FileSelectFile, SelectedFileName, 3,, Open File, Text Documents (*.txt)
if SelectedFileName = ; No file selected.
return
Gosub FileRead
return
FileRead: ; Caller has set the variable SelectedFileName for us.
FileRead, MainEdit, %SelectedFileName% ; Read the file's contents into the variable.
if ErrorLevel <> 0
{
MsgBox Could not open "%SelectedFileName%".
return
}
GuiControl,, MainEdit, %MainEdit% ; Put the text into the control.
CurrentFileName = %SelectedFileName%
Gui, Show,, %CurrentFileName% ; Show file name in title bar.
return
FileSave:
if CurrentFileName = ; No filename selected yet, so do Save-As instead.
Goto FileSaveAs
Gosub SaveCurrentFile
return
FileSaveAs:
FileSelectFile, SelectedFileName, S16,, Save File, Text Documents (*.txt)
if SelectedFileName = ; No file selected.
return
CurrentFileName = %SelectedFileName%
Gosub SaveCurrentFile
return
SaveCurrentFile: ; Caller has ensured that CurrentFileName is not blank.
IfExist %CurrentFileName%
{
FileDelete %CurrentFileName%
if ErrorLevel <> 0
{
MsgBox The attempt to overwrite "%CurrentFileName%" failed.
return
}
}
GuiControlGet, MainEdit ; Retrieve the contents of the Edit control.
FileAppend, %MainEdit%, %CurrentFileName% ; Save the contents to the file.
; Upon success, Show file name in title bar (in case we were called by FileSaveAs):
Gui, Show,, %CurrentFileName%
return
HelpAbout:
MsgBox A very simple text editor.
return
GuiDropFiles: ; Support drag & drop.
Loop, parse, A_GuiControlEvent, `n
{
SelectedFileName = %A_LoopField% ; Get the first file only (in case there's more than one).
break
}
Gosub FileRead
return
GuiSize:
if ErrorLevel = 1 ; The window has been minimized. No action needed.
return
; Otherwise, the window has been resized or maximized. Resize the Edit control to match.
NewWidth := A_GuiWidth - 20
NewHeight := A_GuiHeight - 20
GuiControl, Move, MainEdit, W%NewWidth% H%NewHeight%
return
FileExit: ; User chose "Exit" from the File menu.
GuiClose: ; User closed the window.
ExitApp
Gui, +AlwaysOnTop +Disabled -SysMenu Gui, Add, Text,, Some text to display. Gui, Show, NoActivate, My Window ; NoActivate avoids deactivating the currently active window.