GUI
実行制御 | GUI表示 | 演算・変数 | メモリ・DLL操作 | 文字列操作 |
キーボード | マウス | シェル | ウィンドウ | ウィンドウグループ
ステータスバー | コントロール | サウンド | ファイル | INIファイル |
レジストリ | 環境変数 | AutoHotkey | その他 | 設定関係 | オブジェクト
GUI[編集]
入力フォームなどに使えるGUIを作成/操作する。
下記のサブコマンドがある。 説明の量が多いので項を分ける。
GUI, Add, ControlType [, Options, Text] | ウィンドウにコントロールを追加する。 追加できるコントロールには以下のものがある。 |
Text, Edit, UpDown, Hotkey, Picture, GroupBox, Button, Checkbox, Radio, DropDownList, ComboBox, ListBox, ListView, TreeView, Slider, Progress, DateTime, MonthCal, Tab2, ActiveX, Custom | |
GUI, Tab [, Item, ControlNo] | タブコントロールがあるとき、今後配置するコントロールをどのタブシートに配置するかを変更。 |
GUI, ListView, ListViewName | リストビュー操作系関数で対象となるリストビューを変更する。 |
GUI, TreeView, TreeViewName | ツリービュー操作系関数で対象となるツリービューを変更する。 |
GUI, Menu [, MenuName] | ウィンドウにメニューバーを追加/削除する。 |
GUI, Color [, WindowColor, ControlColor] | ウィンドウ及びコントロールの背景色を設定する。 |
GUI, Font [, Options, FontName] | これ以降に追加されるコントロールのフォントを変更する。 |
GUI, Margin [,X, Y] | コントロール位置の自動決定時にあけられるマージンを設定。 |
Gui, New [, Options, Title] | 新しいウィンドウを作成する。 |
GUI, Show [, Options, Title] | ウィンドウを表示/表示状態の変更。 |
GUI, Hide | ウィンドウを非表示にする。 |
GUI, Maximize | ウィンドウを最大化する。 |
GUI, Minimize | ウィンドウを最小化する。 |
GUI, Restore | ウィンドウを元に戻す。 |
GUI, Flash [,Off] | ウィンドウのタイトルバーとタスクバーの色を反転させる。 これを利用してタイトルバーを点滅させることができる。 |
GUI, Submit [, NoHide] | コントロールの状態や内容を指定された変数に反映させる。 |
GUI, Cancel | 変数に内容を反映させずに、ウィンドウを非表示にする。 |
GUI, Destroy | ウィンドウを完全に破棄し、メモリやシステムリソースを開放する。 |
GUI, +/-Option | ウィンドウのオプションを追加/削除する。 |
GUI, Default | デフォルトのウィンドウを変更する。 |
Remarks[編集]
ウィンドウの生成[編集]
ウィンドウは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サブコマンドでのフォントの設定は、ウィンドウごとに保持される。
[v1.1.03+]: Gui names must conform to the same rules as variable names. Any number which is either not between 1 and 99 or is longer than two characters (such as 0x01) must be the HWND of an existing Gui, or the command will fail. The number of windows that can be created is limited only by available system resources.
[v1.1.04+]: Any number of unnamed GUI windows can be created using Gui, New.
特殊サブルーチンラベル[編集]
GuiEscape、GuiCloseなどの特別な名前のサブルーチンラベルが存在すると、ウィンドウに対して特定の操作が行われたときそのサブルーチンが実行されるようになる。
現在用意されている特殊サブルーチンには以下のものがある。
2番目以降のウィンドウのイベントは、 2GuiEscape
のように前に数字を追加した名前のラベルで定義する。
GuiEscape | Escapeキーを押したときに実行される。 ラベルが存在しない場合、Escapeキーを押しても何も起こらない。 | |
GuiClose | ウィンドウを閉じようとしたときに実行される。 ラベルが存在しない場合、ウィンドウは単に非表示になる( Gui,Cancel と同じ動作)
| |
GuiSize | ウィンドウがリサイズ、最小化、最大化、復元されたときに実行される。 A_GuiWidth、A_GuiHeight変数にクライアント領域の新しい幅と高さが格納される。 また、A_EventInfo変数とErrorLevel変数に以下の値が格納される。 | |
0 | リサイズまたは最大化・最小化からの復元 | |
1 | 最小化 | |
2 | 最大化 | |
GuiDropFiles | ファイルやフォルダがドラッグ&ドロップされたときに実行される。 このイベントのスレッドが実行されている間は、ドラッグ&ドロップは無視される。 A_GuiEvent変数とA_GuiControlEvent変数にはドロップされたファイルのファイル名が改行区切りで格納される。 A_GuiControl変数にはドロップ先のコントロールに関連付けられた変数名か、それがなければコントロールのテキストが格納される。コントロールにドロップされたのでなければ、空になる。 A_EventInfo変数とErrorLevel変数には、ドロップされたファイルの数が格納される。 GuiXとGuiY変数には、ドロップが行われた座標がウィンドウの左上からの相対座標で格納される。 Gui,-E0x00000010 で拡張スタイルWS_EX_ACCEPTFILESを解除すると、一時的にドラッグ&ドロップ受付を無効にできる。Gui,+E0x00000010 で再び有効にできる。
| |
GuiContextMenu | ウィンドウのタイトルバーとメニューバーを除くクライアント部分を右クリックしたときに実行される。AppsキーやShift+F10を押したときにも実行される。 このサブルーチンは、すでにスレッドが実行中でも新たに実行開始される。 A_GuiEvent変数には、右クリックによるイベント発生なら RightClick が、キー操作による発生なら Normal が格納される。A_GuiControl変数にはクリックされたコントロールに関連付けられた変数名か、それがなければコントロールのテキストが格納される。コントロールがクリックされたのでなければ、空になる。 A_EventInfo変数には、リストボックスやリストビューのフォーカスを持つ行番号が格納される。(ただし、右クリックではフォーカスは移動しない)1行目は 1 になる。リストボックスやリストビューでなければ 0 になる。GuiXとGuiY変数には、イベントが発生したときのマウスカーソルの座標がウィンドウの左上からの相対座標で格納される。キー操作によって起動されたときにも同じように格納される。 |
下記のようにサブルーチンの前に複数のラベルを書けば、複数のイベントにひとつの動作を割り当てられる。
GuiEscape:
GuiClose:
ButtonCancel:
Gui, Cancel ; All of the above labels will execute this subroutine.
Return
GUIコマンドのテストをするときは、下記の割り当てがお勧め。
普通にウィンドウを閉じただけではスクリプトが終了しないので、ウィンドウを閉じたときにスクリプトが終了するようにしてやる。
GuiClose: ExitApp
Buttonコントロールのデフォルトサブルーチンラベル[編集]
Buttonコントロールでは、 G
オプションでサブルーチンを指定しなくても、自動的にデフォルトのラベルが割り当てられる。
たとえば、Text引数が Text
のボタンでは、 ButtonText
というラベルに割り当てられる。(該当するラベルが存在すれば)
ただし、Text引数に半角スペースや &
、改行文字などが含まれる場合、それらの文字を除いた文字列が使用される。
2番目以降のウィンドウのボタンについては、 2ButtonText
のような最初にウィンドウ番号をつけた名前のラベルに割り当てられる。
Gui,Add,Button,,Exit ButtonExit: ExitApp
Tabナビゲーション[編集]
作成したウィンドウは、一般的なウィンドウのようにTabキーでフォーカスを別のコントロールに移動することが出来る。
移動する順序は、コントロールがウィンドウに追加された順番。
ウィンドウが表示されるときは、最初のNoTabオプションを指定されていないフォーカスをもてるコントロールにフォーカスがセットされる。
アクセスキー[編集]
一部のコントロールは、テキストに &A
のように「& + 英数字」を入れることで、アクセスキーを設定できる。
アクセスキーは A
のようにアンダーライン付きの文字で表示され、「Alt + 文字キー」でそのコントロールをクリックしたときの動作が行われる。
TextやGroupBoxでは、次に追加したコントロールにフォーカスが移る。
複数のコントロールに同じアクセスキーが割り当てられている場合、アクセスキーを押すごとに次の該当するコントロールにフォーカスが移動する。
&&
と書くことで、テキスト中に &
を表示することが出来る。
自動レイアウト[編集]
コントロールを追加する際、X/Yオプションで座標を指定しなかった場合、コントロールの位置は自動的に決定される。
最初に追加されるコントロールは、ウィンドウの左上に配置され、2つ目以降のコントロールは、直前のコントロールの下側にほかのコントロールと重ならないように配置される。
X/Yのうち片方だけ指定した場合、もうひとつの座標だけが自動的に決定される。
コントロールのサイズも、コントロールの種類やテキストの内容などによって自動的に決定される。
自動レイアウト時には、ほかのコントロールから一定の間隔をあけて配置される。
デフォルトでは、上下の間隔は選択されているフォントの高さの1.25倍、左右の間隔は0.75倍になる。
マージンの幅は、Gui,Marginで変更できる。
Gui,Show
でウィンドウが最初に表示されるときにも、幅や高さが指定されない限り、このマージンを考慮してサイズが決定される。
ウィンドウのアイコン[編集]
ウィンドウのアイコンには、ウィンドウが作成されるときにタスクトレイアイコンでして使用されていたアイコンが使用される。
タスクトレイアイコンは Menu, Tray, Icon
で設定できる。
イベント呼び出し時のLastFoundWindow[編集]
ウィンドウコントロールの操作によって新規スレッドでサブルーチンが呼び出される際には、LastFoundWindowとしてコントロールを持つウィンドウが記憶される。
これにより、各種ウィンドウ操作コマンドで当該ウィンドウを操作したいとき、ウィンドウの指定を省略することが出来る。
常駐[編集]
GUIコマンドを含んでいるスクリプトは、常駐状態かつ#SingleInstance(複数実行不可)になる。
コントロールの操作[編集]
作成したGUI上のコントロールを操作するには、GuiControl、GuiControlGetコマンドを使用する。
Related[編集]
GuiControl, GuiControlGet, Menu, Control, ControlGet, SplashImage, MsgBox, FileSelectFile, FileSelectFolder
Example(s)[編集]
Example #1: Simple input-box[編集]
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
Example #2: Tab control[編集]
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
Example #3: ListBox containing files in a directory[編集]
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
Example #4: On-screen display (OSD)[編集]
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
Example #5: Simple image viewer.[編集]
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
Example: Achieve an effect similar to SplashTextOn.[編集]
Gui, +AlwaysOnTop +Disabled -SysMenu
Gui, Add, Text,, Some text to display.
Gui, Show, NoActivate, My Window ; NoActivate avoids deactivating the currently active window.