Menu/MenuBarオブジェクト

Provides an interface to create and modify a menu or menu bar, add and modify menu items, and retrieve information about the menu or menu bar.

class Menu extends Object

メニューオブジェクトは、ポップアップメニューの定義、変更、表示に使用します。Menu()MenuFromHandleおよびA_TrayMenuは、この型のオブジェクトを返す。

class MenuBar extends Menu

MenuBarオブジェクトは、Gui.MenuBarで使用するメニューバーの定義や修正に使用します。MenuBar()で作成します。MenuFromHandleは、メニューバーハンドルが与えられた場合、このタイプのオブジェクトを返します。

「Menu」はクラスそのものなので、以下ではあらゆるMenuオブジェクトのプレースホルダとして 「MyMenu」を使用します。

メニューオブジェクトは、Objectから継承されるメソッドとプロパティに加えて、以下の定義済みのメソッドとプロパティを持ちます。

目次

静的メソッド

Call

新しい Menu または MenuBar オブジェクトを作成します。

MyMenu := Menu()
MyMenuBar := MenuBar()
MyMenu := Menu.Call()
MyMenuBar := MenuBar.Call()

メソッド

Add

メニュー項目を追加または変更します。

MyMenu.Add(MenuItemName, CallbackOrSubmenu, Options)

パラメータ

MenuItemName

型:文字列

メニュー項目に表示するテキスト、または修正する既存項目の位置を指定します。MenuItemName」を参照。

CallbackOrSubmenu

型:機能オブジェクトまたはMenu

The function to call as a new thread when the menu item is selected, or a reference to a Menu object to use as a submenu.

This parameter is required when creating a new item, but optional when updating the options of an existing item.

コールバックは3つのパラメーターを受け取り、以下のように定義できる:

MyCallback(ItemName, ItemPos, MyMenu) { ...

パラメータに与える名前は重要ではないが、以下の値が順次割り当てられる:

  1. The name of the menu item.
  2. The position number of the menu item.
  3. The Menu object of the menu to which the menu item was added.

対応する情報が不要な場合、コールバックのパラメータリストの最後から1つ以上のパラメータを省略することができますが、この場合、MyCallback(Param1, *)のように、最後のパラメータとしてアスタリスクを指定する必要があります。

Options

型:文字列

If blank or omitted, it defaults to no options. Otherwise, specify one or more options from the list below (not case-sensitive). 各オプションと次のオプションはスペースまたはタブで区切る。オプションを削除する場合は、オプションの前にマイナス記号を付けます。オプションを追加する場合は、プラス記号を使用することができますが、必須ではありません。

Pn: Specify for n the menu item's thread priority, e.g. P1. メニュー項目を追加する際にこのオプションを省略した場合、優先順位は標準のデフォルトである0になります。メニュー項目を更新する際に省略した場合、その項目の優先順位は変更されません。優先順位として10進数(16進数ではない)を使用します。

Radio:項目にチェックが入っている場合は、チェックマークの代わりに箇条書きを使用します。

Right:メニューバー内で項目が右寄せになります。これはメニューバーだけに適用され、ポップアップメニューやサブメニューには適用されません。

Break:ポップアップメニューの新しい列を開始する項目です。

BarBreak:上記と同様ですが、列の間に仕切り線があります。

コールバックやサブメニューに影響を与えずに既存のアイテムのオプションを変更するには、CallbackOrSubmenuパラメータを省略するだけです。

備考

メニューの追加、新しいサブメニューやコールバックによる更新、通常のメニューからサブメニューへの変換(またはその逆)など、多目的に使えるメソッドです。MenuItemNameがまだ存在しない場合、メニューに追加されます。そうでない場合は、MenuItemNameは新しく指定されたCallbackOrSubmenuOptionsで更新される。

メニューの区切り線を追加する場合は、3つのパラメータをすべて省略します。

This method always adds new menu items at the bottom of the menu, while the Insert method can be used to insert an item before an existing custom menu item.

AddStandard

標準トレイのメニュー項目を追加します。

MyMenu.AddStandard()

この方法は、トレイメニューや他のメニューでも使用できます。

標準項目は、既存の項目の後に挿入されます。すでにある標準的なメニューは重複せず、足りないものは追加されます。空のメニューでAddStandardを呼び出した後の標準アイテムの名前と位置を下表に示します:

&Open10
&Help2
3
&Window Spy4
&Reload Script5
&Edit Script6
7
&Suspend Hotkeys81
&Pause Script92
E&xit103

コンパイルされたスクリプトには、デフォルトで最後の3つだけが含まれています。AddStandardを呼び出したときにA_AllowMainWindowが1の場合のみ、&Openが含まれます(その場合、3列目に示す位置に1を追加します)。トレイメニューに標準項目が含まれている場合、A_AllowMainWindowが変更されるたびに、&Openが挿入または削除されます。その他のメニューでは、A_AllowMainWindowが 0 の場合、&Openの効果はありません。

各標準項目は、その機能に対応する内部メニュー項目IDを持つが、それ以外は他のメニュー項目と同様に変更または削除することができます。AddStandardは、名前ではなくIDで既存アイテムを検出します。Addメソッドを使用して標準メニューに関連するコールバック関数を変更した場合、そのメニューには新しい一意のIDが割り当てられ、もはや標準メニューとはみなされない。

トレイメニューに&Open項目を追加すると、その項目がまだない場合はデフォルトの項目となります。

Check

Adds a visible checkmark in the menu next to a menu item (if there isn't one already).

MyMenu.Check(MenuItemName)

パラメータ

MenuItemName

型:文字列

メニュー項目の名称や位置。MenuItemName」を参照。

Delete

Deletes one or all menu items.

MyMenu.Delete(MenuItemName)

パラメータ

MenuItemName

型:文字列

If omitted, all menu items are deleted from the menu, leaving the menu empty. Otherwise, specify the name or position of a menu item. MenuItemName」を参照。

備考

空のメニューはまだ存在するので、それをサブメニューとして使用している他のメニューは、それらのサブメニューを保持します。

セパレータラインを削除するには、メニュー内の位置で識別します。例えば、セパレータの前に2つの項目がある場合は、MyMenu.Delete("3&")を使用します。

デフォルトのメニュー項目を削除した場合、MyMenu.Default := ""を設定した場合と同様の効果が得られます。

Disable

Grays out a menu item to indicate that the user cannot select it.

MyMenu.Disable(MenuItemName)

パラメータ

MenuItemName

型:文字列

メニュー項目の名称や位置。MenuItemName」を参照。

Enable

Allows the user to once again select a menu item if it was previously disabled (grayed out).

MyMenu.Enable(MenuItemName)

パラメータ

MenuItemName

型:文字列

メニュー項目の名称や位置。MenuItemName」を参照。

Insert

指定された項目の前に、新しい項目を挿入します。

MyMenu.Insert(MenuItemName, ItemToInsert, CallbackOrSubmenu, Options)

パラメータ

MenuItemName

型:文字列

If blank or omitted, ItemToInsert will be added at the bottom of the menu. Otherwise, specify the name or position of an existing custom menu item before which ItemToInsert should be inserted. MenuItemName」を参照。

ItemToInsert

型:文字列

The name of a new menu item to insert before MenuItemName. Unlike the Add method, a new item is always created, even if ItemToInsert matches the name of an existing item.

CallbackOrSubmenu

See the Add method's CallbackOrSubmenu parameter.

Options

See the Add method's Options parameter.

備考

To insert a menu separator line before an existing custom menu item, omit all parameters except MenuItemName. To add a menu separator line at the bottom of the menu, omit all parameters.

Rename

メニュー項目の名前を変更します。

MyMenu.Rename(MenuItemName , NewName)

パラメータ

MenuItemName

型:文字列

メニュー項目の名称や位置。MenuItemName」を参照。

NewName

型:文字列

If blank or omitted, MenuItemName will be converted into a separator line. Otherwise, specify the new name.

備考

メニューアイテムの現在のコールバックまたはサブメニューは変更されます。

A separator line can be converted to a normal item by specifying the position of the separator such as "1&" for MenuItemName and a non-blank name for NewName, and then using the Add method to give the item a callback or submenu.

SetColor

メニューの背景色を変更します。

MyMenu.SetColor(ColorValue, ApplyToSubmenus)

パラメータ

ColorValue

型:文字列または整数

If blank or omitted, it defaults to the word Default, which restores the default color of the menu. Otherwise, specify one of the 16 primary HTML color names, a hexadecimal RGB color string (the 0x prefix is optional), or a pure numeric RGB color value. 例値:"Silver""FFFFAA"0xFFFFAA"Default"

ApplyToSubmenus

型:論理値

省略したときは、デフォルトでTrueになります。

If true, the color will be applied to all of the menu's submenus.

If false, the color will be applied to the menu only.

SetIcon

メニュー項目の横に表示するアイコンを設定します。

MyMenu.SetIcon(MenuItemName, FileName , IconNumber, IconWidth)

パラメータ

MenuItemName

型:文字列

メニュー項目の名称や位置。MenuItemName」を参照。

FileName

型:文字列

The path to an icon or image file, or a bitmap or icon handle such as "HICON:" handle. 対応フォーマットの一覧は、ピクチャーコントロールをご覧ください。

Specify an empty string or "*" to remove the item's current icon.

IconNumber

型:整数

If omitted, it defaults to 1 (the first icon group). Otherwise, specify the number of the icon group to be used in the file. For example, MyMenu.SetIcon(MenuItemName, "Shell32.dll", 2) would use the default icon from the second icon group. If negative, its absolute value is assumed to be the resource ID of an icon within an executable file.

IconWidth

型:整数

If omitted, it defaults to the width of a small icon recommended by the OS (usually 16 pixels). If 0, the original width is used. Otherwise, specify the desired width of the icon, in pixels. IconNumberで示されるアイコングループに複数のアイコンサイズが含まれる場合、最も近いサイズのアイコンを使用し、指定されたサイズに拡大縮小されます。

備考

Currently it is necessary to specify the "actual size" when setting the icon to preserve transparency, e.g. MyMenu.SetIcon(MenuItemName, "Filename.png",, 0).

Show

メニューを表示します。

MyMenu.Show(X, Y)

パラメータ

X, Y

型:整数

If omitted, the menu will be shown near the mouse cursor. Otherwise, specify the X and Y coordinates at which to display the upper left corner of the menu. The coordinates are relative to the active window's client area unless overridden by using CoordMode or A_CoordModeMenu.

備考

Displaying the menu allows the user to select an item with arrow keys, menu shortcuts (underlined letters), or the mouse.

サブメニューやトレイメニューなど、あらゆるポップアップメニューを表示することが可能です。ただし、MyMenuがMenuBarオブジェクトである場合は例外が発生します。

ToggleCheck

チェックマークがない場合は追加し、それ以外の場合は削除します。

MyMenu.ToggleCheck(MenuItemName)

パラメータ

MenuItemName

型:文字列

メニュー項目の名称や位置。MenuItemName」を参照。

ToggleEnable

Disables a menu item if it was previously enabled; otherwise, enables it.

MyMenu.ToggleEnable(MenuItemName)

パラメータ

MenuItemName

型:文字列

メニュー項目の名称や位置。MenuItemName」を参照。

Uncheck

メニュー項目からチェックマーク(ある場合)を削除します。

MyMenu.Uncheck(MenuItemName)

パラメータ

MenuItemName

型:文字列

メニュー項目の名称や位置。MenuItemName」を参照。

プロパティ

ClickCount

Retrieves or sets how many times the tray icon must be clicked to select its default menu item.

CurrentCount := MyMenu.ClickCount
MyMenu.ClickCount := NewCount

CurrentCount is NewCount if assigned, otherwise 2 by default.

NewCount can be 1 to allow a single-click to select the tray menu's default menu item, or 2 to return to the default behavior (double-click). Any other value is invalid and throws an exception.

Default

デフォルトのメニュー項目を取得または設定します。

CurrentDefault := MyMenu.Default
MyMenu.Default := MenuItemName

CurrentDefault is the name of the default menu item, or an empty string if there is no default.

MenuItemName is the name or position of a menu item. MenuItemName」を参照。MenuItemNameが空文字列の場合、デフォルトは存在しない。

デフォルトの項目を設定すると、その項目のフォントが太くなります(トレイメニュー以外のメニューでデフォルトの項目を設定することは、現在のところ純粋な外観上の問題です)。When the user double-clicks the tray icon, its default menu item is selected (even if the item is disabled). デフォルトがない場合、ダブルクリックしても効果はありません。

トレイメニューのデフォルト項目は、存在する場合、最初は&Openです。AddStandardの呼び出しやA_AllowMainWindowの変更によってトレイメニューに&Openを追加すると、すでになかった場合はデフォルトのアイテムになることもあります。

デフォルトの項目が削除された場合、メニューには何もない状態になります。

Handle

Win32メニューのハンドル(HMENU型ハンドル)を、必要に応じて構築して返します。

Handle := MyMenu.Handle

返されたハンドルは、Win32メニューが破棄されるまで有効であり、通常Menuオブジェクトが解放されたときに発生する。メニューが破壊されると、オペレーティングシステムは、スクリプトまたは他のプログラムによってその後に作成されたすべてのメニューにハンドル値を再割り当てすることができます。

メニュー項目の名称や位置。このパラメータを使用するすべてのメソッドで、いくつかの共通のルールが適用されます:

メニュー名の1文字に下線を引くには、その文字の前にアンパサンド(&)を付けます。メニューが表示されているときに、キーボードの対応するキーを押すことで、その項目を選択することができます。文字通りのアンパサンドを表示するには、この例のように連続した2つのアンパサンドを指定します:"Save && Exit"

既存のメニューを参照する場合、名前の大文字と小文字は区別されませんが、アンパサンドは必ず含まれていなければなりません。事例:"&Open"

メニュー項目の名称は260文字まで可能です。

既存の項目をメニュー内の位置で識別するには、項目の位置の後にアンパサンドを記述します。例えば、"1&"は最初の項目を示します。

Win32メニュー

Windowsは、標準的な外観と動作を持つメニューを作成、変更、表示するための一連の関数と通知を提供します。これらの関数を用いて作成されたメニューをWin32メニューと呼ぶことにします。

メニューに項目が追加されたり、変更されたりすると、各項目の名前とその他のプロパティがMenuオブジェクトに保存されます。Win32メニューは、メニューまたはその親メニューがGUIに添付されるか、表示される最初の時に構築されます。メニューオブジェクトが削除されたとき(参照カウントがゼロになったとき)に、自動的に破棄されます。

Menu.Handleは、Win32メニューのハンドル(HMENU型ハンドル)を、必要に応じて構築して返します。

Win32の関数で直接メニューに加えられた修正は、スクリプトのMenuオブジェクトには反映されないので、ビルトインメソッドでアイテムが修正されると、失われる可能性があります。

各メニューには、最初にメニューに追加されるときにIDが割り当てられます。スクリプトは、アイテムが特定のIDを受け取ることに依存することはできませんが、例5で示すようにGetMenuItemIDを使用して、アイテムのIDを取得することができます。このIDはMenuオブジェクトでは使用できませんが、Win32の各種機能で使用することができます。

備考

メニューは通常このような形になっています:

Menu

MyMenu.Delete()などでメニューが完全に空になった場合は、表示することができません。トレイメニューが空になると、トレイアイコンを右クリックやダブルクリックしても効果がありません(このような場合は、通常#NoTrayIconを使用する方がよいでしょう)。

メニュー項目のコールバックがすでに実行されている状態で、ユーザーが同じメニューを再度選択した場合、その同じコールバックを実行するために新しいスレッドが作成され、前のスレッドを中断させます。このようなイベントを後でバッファリングするには、コールバックの最初の行にCriticalを使用します(ただし、この場合、ホットキーの押下など他のスレッドもバッファリング/ディファーされます)。

メニューから関数を呼び出すと、SendModeなどの設定値は常に初期値でスタートします。これらのデフォルトは、スクリプト起動時に変更することができます。

メニューの内容が常に同じとは限らない場合、そのようなメニューはすべて同じ関数を指定し、その関数のパラメータを参照して動作を決定する方法があります。また、関数オブジェクトクロージャファットアロー関数を使用して、1つ以上の値や変数をメニューアイテムのコールバック関数にバインドすることもできる。

GUIスレッドThreadCritical#NoTrayIcon関数ReturnSetTimer

トレイアイコンメニューの下部に新しいメニュー項目を追加します。

A_TrayMenu.Add()  ; セパレータラインを作成します。
A_TrayMenu.Add("Item1", MenuHandler)  ; 新しいメニュー項目を作成する。
Persistent

MenuHandler(ItemName, ItemPos, MyMenu) {
    MsgBox "You selected " ItemName " (position " ItemPos ")"
}

ユーザーがホットキーを押したときに表示されるポップアップメニューを作成します。

; ポップアップメニューにいくつかの項目を追加して作成します。
MyMenu := Menu()
MyMenu.Add("Item 1", MenuHandler)
MyMenu.Add("Item 2", MenuHandler)
MyMenu.Add()  ; 区切り線を追加する。

; 上のメニューのサブメニューとなるような別のメニューを作成する。
Submenu1 := Menu()
Submenu1.Add("Item A", MenuHandler)
Submenu1.Add("Item B", MenuHandler)

; 最初のメニューにサブメニューを作成する(右矢印の表示)。ユーザーが選択すると、2番目のメニューが表示される。
MyMenu.Add("My Submenu", Submenu1)

MyMenu.Add()  ; サブメニューの下に区切り線を追加します。
MyMenu.Add("Item 3", MenuHandler)  ; サブメニューの下に別のメニューを追加します。

MenuHandler(Item, *) {
    MsgBox("You selected " Item)
}

#z::MyMenu.Show()  ; つまり、Win-Zホットキーを押してメニューを表示する。

様々なメニューオブジェクトメンバーの一部を実演しています。

#SingleInstance
Persistent
Tray := A_TrayMenu ; 便宜上
Tray.Delete() ; 標準の項目を削除する。
Tray.Add() ; separator
Tray.Add("TestToggleCheck", TestToggleCheck)
Tray.Add("TestToggleEnable", TestToggleEnable)
Tray.Add("TestDefault", TestDefault)
Tray.Add("TestAddStandard", TestAddStandard)
Tray.Add("TestDelete", TestDelete)
Tray.Add("TestDeleteAll", TestDeleteAll)
Tray.Add("TestRename", TestRename)
Tray.Add("Test", Test)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

TestToggleCheck(*)
{
    Tray.ToggleCheck("TestToggleCheck")
    Tray.Enable("TestToggleEnable")  ; 自分自身の無効化を取り消すことができないので、次のテストも有効にします。
    Tray.Add("TestDelete", TestDelete) ; Similar to above.
}

TestToggleEnable(*)
{
    Tray.ToggleEnable("TestToggleEnable")
}

TestDefault(*)
{
    if Tray.Default = "TestDefault"
        Tray.Default := ""
    else
        Tray.Default := "TestDefault"
}

TestAddStandard(*)
{
    Tray.AddStandard()
}

TestDelete(*)
{
    Tray.Delete("TestDelete")
}

TestDeleteAll(*)
{
    Tray.Delete()
}

TestRename(*)
{
    static OldName := "", NewName := ""
    if NewName != "renamed"
    {
        OldName := "TestRename"
        NewName := "renamed"
    }
    else
    {
        OldName := "renamed"
        NewName := "TestRename"
    }
    Tray.Rename(OldName, NewName)
}

Test(Item, *)
{
    MsgBox("You selected " Item)
}

メニュー項目にアイコンを追加する方法を説明します。

FileMenu := Menu()
FileMenu.Add("Script Icon", MenuHandler)
FileMenu.Add("Suspend Icon", MenuHandler)
FileMenu.Add("Pause Icon", MenuHandler)
FileMenu.SetIcon("Script Icon", A_AhkPath, 2) ; 2nd icon group from the file
FileMenu.SetIcon("Suspend Icon", A_AhkPath, -206) ; icon with resource ID 206
FileMenu.SetIcon("Pause Icon", A_AhkPath, -207) ; icon with resource ID 207
MyMenuBar := MenuBar()
MyMenuBar.Add("&File", FileMenu)
MyGui := Gui()
MyGui.MenuBar := MyMenuBar
MyGui.Add("Button",, "Exit This Example").OnEvent("Click", (*) => WinClose())
MyGui.Show()

MenuHandler(*) {
    ; この例では、メニュー項目は何もしません。
}

メニューの項目数と最後の項目のIDを報告します。

MyMenu := Menu()
MyMenu.Add("Item 1", NoAction)
MyMenu.Add("Item 2", NoAction)
MyMenu.Add("Item B", NoAction)

; メニューの項目数を取得する。
item_count := DllCall("GetMenuItemCount", "ptr", MyMenu.Handle)

; 最後のアイテムのIDを取得します。
last_id := DllCall("GetMenuItemID", "ptr", MyMenu.Handle, "int", item_count-1)

MsgBox("MyMenu has " item_count " items, and its last item has ID " last_id)

NoAction(*) {
    ; 何もしません。
}