ListView

目次

導入と簡単な例

A ListView is one of the most elaborate controls provided by the operating system. 最も分かりやすい形としては、行と列の表形式で表示され、最も一般的な例としては、エクスプローラーのファイルやフォルダのリスト(詳細表示)があります。

ListViewは通常このような形をしています:

ListView

凝った作りになっていますが、ListViewの基本的な機能は簡単に使うことができます。ListViewを作成するための構文です:

LV := GuiObj.Add("ListView", Options, ["ColumnTitle1","ColumnTitle2","..."])

あるいは:

LV := GuiObj.AddListView(Options, ["ColumnTitle1","ColumnTitle2","..."])

ここでは、ユーザーの「My Documents」フォルダ内のファイルのリストを含むListViewを作成し、表示するスクリプトを紹介します:

; ウィンドウを作成します:
MyGui := Gui()

; NameとSizeの2つのカラムを持つListViewを作成します:
LV := MyGui.Add("ListView", "r20 w700", ["Name","Size (KB)"])

; ユーザーが行をダブルクリックするたびに、スクリプトに通知する:
LV.OnEvent("DoubleClick", LV_DoubleClick)

; フォルダからファイル名のリストを集め、ListViewに入れる:
Loop Files, A_MyDocuments "\*.*"
    LV.Add(, A_LoopFileName, A_LoopFileSizeKB)

LV.ModifyCol  ; Auto-size each column to fit its contents.
LV.ModifyCol(2, "Integer")  ; For sorting purposes, indicate that column 2 is an integer.

; Display the window:
MyGui.Show

LV_DoubleClick(LV, RowNumber)
{
    RowText := LV.GetText(RowNumber)  ; Get the text from the row's first field.
    ToolTip("You double-clicked row number " RowNumber ". Text:'" RowText "'")
}

Optionsパラメータのオプションとスタイル

Background: Background(背景):Background(背景)という語の直後に色名(カラーチャート参照)またはRGB値(0x接頭辞は任意)を指定。例:BackgroundSilver, BackgroundFFDD99. このオプションがない場合、ListViewの初期値はシステムのデフォルトの背景色になります。BackgroundDefaultまたは-Backgroundを指定すると、システムのデフォルトの背景色(通常は白)が適用される。For example, a ListView can be restored to the default color via LV.Opt("+BackgroundDefault").

C: Text color. 文字Cの直後に、色名(カラーチャート参照)またはRGB値(接頭辞0xは任意)を指定します。例:cRed, cFF2211, c0xFF2211, cDefault.

チェック付き:各行の左側にチェックボックスを提供します。行を追加する際、そのオプションにCheckという単語を指定すると、スタートするボックスがチェックされていない状態ではなく、チェックされた状態になります。ユーザーは、チェックボックスをクリックするか、スペースバーを押して、行のチェックを入れたり外したりすることができます。

Count: Specify the word Count followed immediately by the total number of rows that the ListView will ultimately contain. これは制限ではありません:を超える行を追加することができます。その代わり、このオプションはコントロールへのヒントとして機能し、行を追加するたびにメモリを確保するのではなく、一度だけメモリを確保することができます。To improve performance even more, use LV.Opt("-Redraw") prior to adding a large number of rows and LV.Opt("+Redraw") afterward. See Redraw for more details.

グリッド(Grid):行と列の境界を視覚的に示すための水平線と垂直線を提供します。

Hdr:ヘッダー(列のタイトルを含む特別な最上段)を省略する場合は、-Hdr(マイナスHdr)を指定します。後から見えるようにするには、LV.Opt("+Hdr")を使用します。

LV: Specify the string LV followed immediately by the number of an extended ListView style. これらのスタイルは、一般的な拡張スタイルとは全く別のものです。For example, specifying -E0x200 would remove the generic extended style WS_EX_CLIENTEDGE to eliminate the control's default border. By contrast, specifying -LV0x20 would remove LVS_EX_FULLROWSELECT.

LV0x10: Specify -LV0x10 to prevent the user from dragging column headers to the left or right to reorder them. しかし、列の物理的な並び替えは、スクリプトが見る列の順序に影響を与えないので、通常はこれを行う必要はありません。例えば、ユーザーが物理的に他の列の右に移動させたとしても、スクリプトから見て最初の列は常に列1です。

LV0x20: Specify -LV0x20 to require that a row be clicked at its first field to select it (normally, a click on any field will select it). この利点は、ユーザーが行のグループを選択するために矩形をドラッグすることが容易になることです。

Multi: -Multi(minus Multi)を指定すると、一度に複数の行を選択できないようにすることができます。

NoSortHdr:ヘッダーがクリックできないようにする。通常のボタンのような外観ではなく、平らな外観になります。他の多くのListViewスタイルと異なり、このスタイルはListViewの作成後に変更することができない。

NoSort:ユーザーがカラムヘッダをクリックしたときに発生する自動ソートをオフにします。However, the header will still behave visually like a button (unless the NoSortHdr option above has been specified). In addition, the ColClick event is still raised, so the script can respond with a custom sort or other action.

ReadOnly:を指定します。各行の1列目のテキストの編集を許可する場合は、-ReadOnly(マイナスリードオンリー)を指定します。行を編集するには、行を選択してからF2キーを押します(下記のWantF2オプションを参照してください)。また、行を一度クリックして選択し、半秒以上待ってから同じ行をもう一度クリックすると、編集することができます。

R: Rows of height (upon creation). Rの文字の後に、コントロール内のスペースを確保するための行数を指定します。例えば、R10とすると、コントロールは10列の高さになります。レポートビュー以外のビューモードでListViewを作成した場合、コントロールはテキストの列ではなく、アイコンの列に合うようにサイズ調整されます。注:ListViewの行にアイコンを追加すると、各行の高さが高くなるため、このオプションは不正確となります。

ソート:コントロールは、最初のカラムの内容に従ってアルファベット順にソートされた状態で保持されます。

SortDesc:降順以外は上記と同じ。

WantF2: Specify -WantF2 (minus WantF2) to prevent F2 from editing the currently focused row. この設定は、-ReadOnlyも有効でない限り無視される。

(無名数字スタイル):上記以外のスタイルはほとんど使用されないため、名称を付けない。一覧はListViewのスタイルの表を参照してください。

View Modes

ListViewには5つの表示モードがあり、その中で最も一般的なのはレポートビュー(これがデフォルト)です。他のビューを使用する場合は、オプションリストでその名前を指定します。また、ビューはコントロール作成後に変更することも可能です:LV.Opt("+IconSmall").

アイコン:大きなアイコンで表示されます。このビューおよびレポート以外のビューでは、最初の列以外の列のテキストは表示されません。このモードでアイコンを表示するには、ListViewに大きなアイコンのImageListが割り当てられている必要があります。

Tile: Shows a large-icon view but with ergonomic differences such as displaying each item's text to the right of the icon rather than underneath it. このビューでは、チェックボックスは機能しません。

IconSmall:小さなアイコンで表示します。

一覧表示:アイコンを列挙して表示するリスト形式のスモールアイコンビューを表示します。列の数は、コントロールの幅と、その中で最も幅の広いテキストアイテムの幅に依存します。

レポート初期設定のレポートビューに切り替わります。事例:LV.Opt("+Report").

ListViewの組み込みメソッド

GUIコントロールのデフォルトのメソッド/プロパティに加えて、ListViewコントロールは以下のメソッドを持ちます(Gui.ListViewクラスで定義)。

本ページで「行番号」と表記しているのは、ListView内の行の現在位置のことです。一番上の列が1、2番目の列が2、といった具合です。行が追加された後、他の行のソート、削除、挿入により、その行番号は変化する傾向があります。そのため、特定の行を内容から探すには、通常、ループ内でGetTextメソッドを使用するのが最適です。

列の方法:

Column methods:

検索方法:

その他の方法

Add

リストの一番下に新しい行を追加します。

RowNumber := LV.Add(Options, Col1, Col2, ...)

パラメータ

Options

型:文字列

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

Check: Shows a checkmark in the row (if the ListView has checkboxes). 後でチェックを外すには、LV.Modify(RowNumber, "-Check")を使用します。

Col: Specify the word Col followed immediately by the column number at which to begin applying the parameters Col1 and beyond. This is most commonly used with the Modify method to alter individual fields in a row without affecting those that lie to their left.

Focus: Sets keyboard focus to the row (often used in conjunction with the Select option below). To later de-focus it, use LV.Modify(RowNumber, "-Focus").

Icon: Specify the word Icon followed immediately by the number of this row's icon, which is displayed in the left side of the first column. このオプションがない場合、ImageListの最初のアイコンが使われます。空白のアイコンを表示する場合は、-1またはImageListのアイコンの数より大きい数を指定する。If the control lacks a small-icon ImageList, no icon is displayed nor is any space reserved for one in report view.

The Icon option accepts a one-based icon number, but this is internally translated to a zero-based index; therefore, Icon0 corresponds to the constant I_IMAGECALLBACK, which is normally defined as -1, and Icon-1 corresponds to I_IMAGENONE. その他の範囲外の値でも、アイコンがあるはずの場所に空白ができることがあります。

Select: Selects the row. 後で選択を解除するには、LV.Modify(RowNumber, "-Select")を使用します。行を選択する場合、少なくとも1つの行が常にフォーカスを持つようにしておくと、Appsキーがフォーカスを持つ行の近くにコンテキストメニューを表示できるようになるからです。Selectの後に、オプションとして、開始状態を示す0または1が続く場合がある。つまり、"Select""Select".VarContainingOneは同じです(ここで使われているピリオドは連結演算子です)。This technique also works with the Focus and Check options above.

Vis: Ensures that the specified row is completely visible by scrolling the ListView, if necessary. This has an effect only for LV.Modify; for example: LV.Modify(RowNumber, "Vis").

Col1, Col2, ...

型:文字列

新しい行の列で、テキストまたは数値(数値の結果を含む)を指定することができます。To make any field blank, specify "" or the equivalent. フィールドが少なすぎてすべての列を埋めることができない場合、末尾の列は空白になります。フィールドが多すぎる場合、末尾のフィールドは完全に無視されます。

戻り値

型:整数

This method returns the new row number, which is not necessarily the last row if the ListView has the Sort or SortDesc style.

Insert

指定された行番号に新しい行を挿入する。

RowNumber := LV.Insert(RowNumber , Options, Col1, Col2, ...)

パラメータ

RowNumber

型:整数

The row number of the newly inserted row. RowNumber以下の行は、新しい行のためのスペースを確保するために下方にシフトされます。RowNumberがリストの行数より大きい場合(2147483647でも)、新しい行はリストの末尾に追加されます。

Options

型:文字列

If blank or omitted, it defaults to no options. Otherwise, specify one or more options from the list above.

Col1, Col2, ...

型:文字列

新しい行の列で、テキストまたは数値(数値の結果を含む)を指定することができます。To make any field blank, specify "" or the equivalent. フィールドが少なすぎてすべての列を埋めることができない場合、末尾の列は空白になります。フィールドが多すぎる場合、末尾のフィールドは完全に無視されます。

戻り値

型:整数

This method returns the specified row number.

Modify

行の属性やテキストを変更する。

LV.Modify(RowNumber , Options, NewCol1, NewCol2, ...)

パラメータ

RowNumber

型:整数

The number of the row to modify. If 0, all rows in the control are modified.

Options

型:文字列

If blank or omitted, it defaults to no options. Otherwise, specify one or more options from the list above. The Col option may be used to update specific columns without affecting the others.

NewCol1, NewCol2, ...

型:文字列

指定された行の新しい列。テキストまたは数値(数値の結果を含む)を指定することができる。To make any field blank, specify "" or the equivalent. パラメータが少なすぎてすべての列をカバーできない場合、末尾の列は変更されません。フィールドが多すぎる場合、末尾のフィールドは完全に無視されます。

備考

最初の2つのパラメータのみが存在する場合、行の属性のみが変更され、テキストは変更されない。

Delete

指定された行またはすべての行を削除する。

LV.Delete(RowNumber)

パラメータ

RowNumber

型:整数

If omitted, all rows in the ListView are deleted. Otherwise, specify the number of the row to delete.

ModifyCol

指定されたカラムとそのヘッダーの属性とテキストを変更します。

LV.ModifyCol(ColumnNumber, Options, ColumnTitle)

パラメータ

ColumnNumber

型:整数

If this and the other parameters are all omitted, the width of every column is adjusted to fit the contents of the rows. This has no effect when not in Report (Details) view.

Otherwise, specify the number of the column to modify. 1列目は1(0ではない)です。

Options

型:文字列

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


一般的なオプションです:

N: Specify for N the new width of the column, in pixels. この数字は、唯一の選択肢であるならば、引用されないことも可能です。例えば、以下はどちらも有効です:LV.ModifyCol(1, 50) and LV.ModifyCol(1, "50 Integer").

Auto: Adjusts the column's width to fit its contents. This has no effect when not in Report (Details) view.

AutoHdr: Adjusts the column's width to fit its contents and the column's header text, whichever is wider. 最後のカラムに適用すると、ListViewの残りのスペースと同じ幅になるようにします。この設定は、行が追加された後に適用するのが一般的です。そうすることで、新たに到着した垂直スクロールバーを考慮して、最後の列のサイズを設定することができるからです。This has no effect when not in Report (Details) view.

Icon: Specify the word Icon followed immediately by the number of the ImageList's icon to display next to the column header's text. Specify -Icon (minus icon) to remove any existing icon.

IconRight: Puts the icon on the right side of the column rather than the left.


データ型オプション:

Float: For sorting purposes, indicates that this column contains floating point numbers (hexadecimal format is not supported). FloatとTextカラムのソート性能は、整数の場合と比べて最大25倍遅くなります。

Integer: For sorting purposes, indicates that this column contains integers. 正しくソートするためには、各整数は32ビット、つまり-2147483648から2147483647の範囲内でなければなりません。値のいずれかが整数でない場合、ソート時にゼロとみなされます(ただし、数値で始まる場合は、その数値が使用されます)。数値は10進数または16進数(例:0xF9E0)で表示することができます。

Text: Changes the column back to text-mode sorting, which is the initial default for every column. Only the first 8190 characters of text are significant for sorting purposes (except for the Logical option, in which case the limit is 4094).


アライメントオプションです:

Center: Centers the text in the column. To center an Integer or Float column, specify the word Center after the word Integer or Float.

Left: Left-aligns the column's text, which is the initial default for every column. 古いOSでは、1列目が強制的に左寄せになる場合があります。

Right: Right-aligns the column's text. IntegerおよびFloatカラムはデフォルトで右寄せになっているため、この属性を指定する必要はありません。このデフォルトは、"Integer Left""Float Center"などの指定で上書きすることができます。


ソートオプション:

Case: The sorting of the column is case-sensitive (affects only text columns). CaseCaseLocaleLogicalの各オプションが省略された場合、大文字のA〜Zは小文字と同一とみなされ、ソートが行われます。

CaseLocale: The sorting of the column is case-insensitive based on the current user's locale (affects only text columns). 例えば、ほとんどの英語と西ヨーロッパのロケールでは、A-Zの文字とÄやÜなどのANSI文字が小文字と同じように扱われます。また、この方法では、「coop」と「co-op」のような単語が一緒になるようにハイフンやアポストロフィーを処理する「ワードソート」を使用しています。

Desc: Descending order. ユーザーが初めてソートするときは、列は降順で始まります。

Logical: Same as CaseLocale except that any sequences of digits in the text are treated as true numbers rather than mere characters. 例えば、文字列 "T33"は "T4"よりも大きいと判断されるでしょう。LogicalCaseは現在相互排他的です:直近で指定されたものだけが有効になります。

NoSort: Prevents a user's click on this column from having any automatic sorting effect. しかし、ColClickイベントはまだ発生するので、スクリプトはカスタムソートなどのアクションで応答することができます。一部の列だけでなく、すべての列のソートを無効にするには、リストビューのオプションにNoSortを含めます。

Sort: Immediately sorts the column in ascending order (even if it has the Desc option).

SortDesc: Immediately sorts the column in descending order.

Uni: Unidirectional sort. これにより、同じ列を2回目にクリックしても、ソートの方向が逆になることを防ぐことができます。

ColumnTitle

型:文字列

If omitted, the current header is left unchanged. Otherwise, specify the new header of the column.

InsertCol

指定されたカラム番号に新しいカラムを挿入します。

ColumnNumber := LV.InsertCol(ColumnNumber, Options, ColumnTitle)

パラメータ

ColumnNumber

型:整数

If omitted or larger than the number of columns currently in the control, the new column is added next to the last column on the right side.

Otherwise, specify the column number of the newly inserted column. ColumnNumberの右側にある列は、新しい列のために右側にシフトされます。1列目は1(0ではない)です。

Options

型:文字列

If omitted, the column always starts off at its defaults, such as whether or not it uses integer sorting. Otherwise, specify one or more options from the list above.

ColumnTitle

型:文字列

If blank or omitted, it defaults to an empty header. Otherwise, specify the header of the column.

戻り値

型:整数

This method returns the new column's position number.

備考

新しく挿入された列は、それが最初の列でない限り、その下に何もない状態で始まり、その場合、古い最初の列の内容を引き継ぎ、古い最初の列は空白の内容を獲得する。

ListViewの最大列数は200列です。

DeleteCol

指定された列とその下にあるすべてのコンテンツを削除する。

LV.DeleteCol(ColumnNumber)

パラメータ

ColumnNumber

型:整数

The number of the column to delete. 列が削除されると、その右側にある列の列番号は1つ減らされます。その結果、LV.DeleteCol(2)を2回呼び出すと、2列目と3列目が削除されます。

GetCount

コントロールの行数または列数を返します。

Count := LV.GetCount(Mode)

パラメータ

Mode

型:文字列

If blank or omitted, the method returns the total number of rows in the control. Otherwise, specify one of the following strings:

S or Selected: The count includes only the selected/highlighted rows.

Col or Column: The method returns the number of columns in the control.

戻り値

型:整数

This method returns the number of rows or columns in the control. The value is always returned immediately because the control keeps track of these counts.

備考

This method is often used in the top line of a Loop, in which case the method would get called only once (prior to the first iteration). 事例:

Loop LV.GetCount()
{
    RetrievedText := LV.GetText(A_Index)
    if InStr(RetrievedText, "some filter text")
        LV.Modify(A_Index, "Select")  ; 最初のフィールドがフィルターテキストを含む各行を選択します。
}

ListViewのカラムの幅を取得し、INIファイルに保存してセッション間で記憶させるなどの使い方をする場合は、次のようにします:

Loop LV.GetCount("Column")
{
    ColWidth := SendMessage(0x101D, A_Index - 1, 0, LV)  ; 0x101D is LVM_GETCOLUMNWIDTH.
    MsgBox("Column " A_Index "'s width is " ColWidth ".")
}

GetNext

次に選択、チェック、またはフォーカスされた行の行番号を返します。

RowNumber := LV.GetNext(StartingRowNumber, RowType)

パラメータ

StartingRowNumber

型:整数

省略された場合、または1未満の場合は、リストの先頭から検索を開始します。Otherwise, specify the number of the row after which to begin the search.

RowType

型:文字列

If blank or omitted, the method searches for the next selected/highlighted row (see the example below). Otherwise, specify one of the following strings:

C or Checked: Find the next checked row.

F or Focused: Find the focused row. There is never more than one focused row in the entire list, and sometimes there is none at all.

戻り値

型:整数

This method returns the row number of the next selected, checked, or focused row. If none is found, it returns 0.

備考

次の例では、ListViewで選択されたすべての行をレポートします:

RowNumber := 0  ; This causes the first loop iteration to start the search at the top of the list.
Loop
{
    RowNumber := LV.GetNext(RowNumber)  ; 前の反復で見つかった行の次の行から検索を再開する。
    if not RowNumber  ; 上記は0を返したので、もう選択された行はない。
        break
    Text := LV.GetText(RowNumber)
    MsgBox('The next selected row is #' RowNumber ', whose first field is "' Text '".')
}

特定の行番号にチェックが入っているかどうかを調べる別の方法として、次のようなものがあります:

ItemState := SendMessage(0x102C, RowNumber - 1, 0xF000, LV)  ; 0x102C は LVM_GETITEMSTATE です。0xF000はLVIS_STATEIMAGEMASKです。
IsChecked := (ItemState >> 12) - 1  ;RowNumberがチェックされていればIsCheckedをtrueに、そうでなければfalseに設定します。

GetText

指定された行番号と列番号のテキストを取得する。

Text := LV.GetText(RowNumber , ColumnNumber)
RowNumber

型:整数

The number of the row whose text to be retrieved. If 0, the column header text is retrieved.

ColumnNumber

型:整数

省略された場合、デフォルトは1(1列目のテキスト)です。Otherwise, specify the number of the column where RowNumber is located.

戻り値

型:文字列

The method returns the retrieved text. Only up to 8191 characters are retrieved.

備考

スクリプトが見る列番号は、ユーザーが行った列のドラッグ&ドロップによって変更されることはありません。例えば、ユーザーが他の列の右側にドラッグしても、元の1列目は1番のままです。

SetImageList

Sets or replaces an ImageList for displaying icons.

PrevImageListID := LV.SetImageList(ImageListID , IconType)

パラメータ

ImageListID

型:整数

The ID number returned from a previous call to IL_Create.

IconType

型:整数

If omitted, the type of icons in the ImageList is detected automatically as large or small. Otherwise, specify 0 for large icons, 1 for small icons, or 2 for state icons (which are not yet directly supported, but could be used via SendMessage).

戻り値

型:整数

This method returns the ImageList ID that was previously associated with the ListView. On failure, it returns 0. このような切り離されたImageListは、通常IL_Destroyで破棄する必要があります。

備考

このメソッドは通常、ListViewに行を追加する前に呼び出されます。It sets the ImageList whose icons will be displayed by the ListView's rows (and optionally, its columns).

ListViewは、最大2つのImageListを持つことができます:small-iconおよびlarge-iconのいずれか、または両方を使用します。スクリプトでラージアイコン表示との切り替えが可能な場合に有効です。To add more than one ImageList to a ListView, call the SetImageList method a second time, specifying the ImageList ID of the second list. 大アイコンと小アイコンのImageListを持つListViewは、両方のリストに同じ順序でアイコンを含むことを保証する必要があります。これは、あるアイコンの大きいバージョンと小さいバージョンの両方を参照するために、同じID番号が使用されるためです。

アイコンとタイルを除くすべての表示モードでは、小さなアイコンを表示するのが伝統的ですが、SetImageListメソッドに大きなアイコンリストを渡し、第2パラメータに1(小さなアイコン)を指定することでこれをオーバーライドすることができます。また、ListViewの各行の高さを大きくして、大きなアイコンに合わせます。

イベント情報

OnEventを呼び出してコールバック関数やメソッドを登録することで、以下のイベントを検出することができます:

Event育てたのは...
Clickコントロールがクリックされます。
DoubleClickコントロールがダブルクリックされます。
ColClickカラムヘッダがクリックされます。
ContextMenuコントロールにキーボードフォーカスがある状態で、ユーザーがコントロールを右クリックするか、メニューまたはShift+F10を押した場合。
Focusコントロールがキーボードフォーカスを獲得します。
LoseFocusコントロールはキーボードフォーカスを失います。
ItemCheck項目がチェックされているか、チェックされていないかの状態です。
ItemEditアイテムのラベルは、ユーザーが編集します。
ItemFocusフォーカスされる項目が変わります。
ItemSelect項目が選択または非選択になる。

OnNotifyを使用することで、追加の(ほとんど使用されない)通知を検出することができます。これらの通知については、Microsoft Docsで文書化されています。Microsoft Docsには、各通知コードの数値は掲載されていませんので、Windows SDKやインターネットで検索してご確認ください。

ImageLists

イメージリストは、メモリ上に保存された同一サイズのアイコンのグループです。作成時、各 ImageList は空である。スクリプトはIL_Addを繰り返し呼び出してアイコンをリストに追加し、各アイコンには1から始まる連番が割り当てられています。行や列のヘッダーに特定のアイコンを表示する際に、スクリプトが参照する番号です。ここでは、ListViewの行にアイコンを配置する方法を示す動作例を示します:

MyGui := Gui()  ; MyGuiのウィンドウを作成します。
LV := MyGui.Add("ListView", "h200 w180", ["Icon & Number", "Description"])  ; ListViewを作成します。
ImageListID :=IL_Create(10)  ; 小さなアイコンを10個保持するImageListを作成します。
LV.SetImageList(ImageListID)  ;上記ImageListを現在のListViewに割り当てる。
Loop 10  ;DLLから一連のアイコンをImageListに読み込ませる。
    IL_Add(ImageListID, "shell32.dll", A_Index)
Loop 10  ; ListViewに行を追加する(デモのため、各アイコンに1つずつ)。
    LV.Add("Icon" . A_Index, A_Index, "n/a")
MyGui.Show

IL_Create

Creates a new ImageList that is initially empty.

ImageListID := IL_Create(InitialCount, GrowCount, LargeIcons)

パラメータ

InitialCount

型:整数

If omitted, it defaults to 2. Otherwise, specify the number of icons you expect to put into the list immediately.

GrowCount

型:整数

If omitted, it defaults to 5. Otherwise, specify the number of icons by which the list will grow each time it exceeds the current list capacity.

LargeIcons

型:論理値

省略されたときは、デフォルトはfalseです。

If false, the ImageList will contain small icons.

If true, the ImageList will contain large icons.

リストに追加されたアイコンは、大小のアイコンについてシステムの寸法に合わせて自動的に拡大縮小されます。

戻り値

型:整数

On success, this function returns the unique ID of the newly created ImageList. On failure, it returns 0.

IL_Add

Adds an icon or picture to the specified ImageList.

IconIndex := IL_Add(ImageListID, IconFileName , IconNumber)
IconIndex := IL_Add(ImageListID, PicFileName, MaskColor, Resize)

パラメータ

ImageListID

型:整数

The ID number returned from a previous call to IL_Create.

IconFileName

型:文字列

The name of an icon (.ICO), cursor (.CUR), or animated cursor (.ANI) file (animated cursors will not actually be animated when displayed in a ListView), or an icon handle such as "HICON:" handle. その他、アイコンのソースとして、以下のような種類のファイルがあります:EXE、DLL、CPL、SCRなど、アイコンリソースを含むタイプ。

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. If the number is negative, its absolute value is assumed to be the resource ID of an icon within an executable file. 次の例では、2番目のアイコングループのデフォルトアイコンが使用されることになります:IL_Add(ImageListID, "C:\My Application.exe", 2).

PicFileName

型:文字列

The name of a non-icon image such as BMP, GIF, JPG, PNG, TIF, Exif, WMF, and EMF, or a bitmap handle such as "HBITMAP:" handle.

MaskColor

型:整数

The mask/transparency color number. 0xFFFFFF (the color white) might be best for most pictures.

Resize

型:論理値

If true, the picture is scaled to become a single icon.

If false, the picture is divided up into however many icons can fit into its actual width.

戻り値

型:整数

On success, this function returns the new icon's index (1 is the first icon, 2 is the second, and so on). On failure, it returns 0.

IL_Destroy

指定された ImageList を削除します。

IsDestroyed := IL_Destroy(ImageListID)

パラメータ

ImageListID

型:整数

The ID number returned from a previous call to IL_Create.

戻り値

型:整数(ブーリアン)

On success, this function returns 1 (true). On failure, it returns 0 (false).

備考

ImageListはListViewに装着されると、ListViewまたはその親ウィンドウが破壊されたときに自動的に破壊されるため、通常は破壊する必要はない。However, if the ListView shares ImageLists with other ListViews (by having 0x40 in its options), the script should explicitly destroy the ImageList after destroying all the ListViews that use it. 同様に、スクリプトがListViewの古いImageListの1つを新しいものに置き換える場合、古いものを明示的に破棄する必要があります。

備考

Gui.Submitは、ListViewコントロールには効果がありません。

カラムがソートされた後(ユーザーがヘッダーをクリックするか、スクリプトがLV.ModifyCol(1, "Sort") を呼び出す)、その後に追加された行は、ソート順序に従わずにリストの一番下に表示されます。ただし、SortSortDescは例外で、新しく追加された行を正しい位置に移動させるスタイルです。

ListViewにフォーカスがある状態でユーザーがEnterを押したことを検出するには、デフォルトのボタンを使用します(必要に応じて非表示にすることができます)。事例:

MyGui.Add("Button", "Hidden Default", "OK").OnEvent("Click", LV_Enter)
...
LV_Enter(*) {
    global
    if MyGui.FocusedCtrl != LV
        return
    MsgBox("Enter was pressed. The focused row number is " LV.GetNext(0, "Focused"))
}

キーボードによる行から行への移動に加えて、ユーザーは最初の列の項目の最初の数文字を入力することによって、インクリメンタルサーチを行うこともできます。これにより、選択範囲は最も近い一致する行にジャンプします。

リストビューの各フィールドには任意の長さのテキストを格納することができますが、最初の260文字だけが表示されます。

ListViewの最大行数は利用可能なシステムメモリによってのみ制限されますが、Countオプションで説明するように、行追加のパフォーマンスを大幅に向上させることができます。

画像は、ListViewの周囲の背景として使用することができます(つまり、ListViewをフレーム化する)。To do this, create the picture control after the ListView and include 0x4000000 (which is WS_CLIPSIBLINGS) in the picture's Options.

スクリプトは、1つのウィンドウに1つ以上のListViewを作成することができます。

SendMessageで直接カラムの挿入や削除を行わない方がよいでしょう。これは、プログラムが各カラムのソート環境設定のコレクションを保持するためで、そうすると同期が取れなくなります。代わりに、組み込みのカラムメソッドを使用します。

リストビューのサイズ変更、非表示、フォント変更などの操作を行うには、GuiControlオブジェクトを参照してください。

外部のListView(スクリプトが所有していないもの)からテキストを抽出するには、ListViewGetContentを使用します。

TreeView, Other Control Types, Gui(), ContextMenu event, Gui object, GuiControl object, ListView styles table

行番号に0を指定して、すべての行を選択または非選択にします。

LV.Modify(0, "Select")   ; Select all.
LV.Modify(0, "-Select")  ; De-select all.
LV.Modify(0, "-Check")  ; Uncheck all the checkboxes.

すべてのカラムを、その内容に合わせて自動サイズ調整します。

LV.ModifyCol  ; このモードではパラメータはありません。

以下は、このページのトップ付近にあるスクリプトよりも、より精巧に作られたワーキングスクリプトです。ユーザーが選択したフォルダー内のファイルを表示し、各ファイルにはその種類に関連するアイコンが割り当てられます。ユーザーは、ファイルをダブルクリックするか、1つまたは複数のファイルを右クリックしてコンテキストメニューを表示することができます。

; GUIウィンドウを作成する:
MyGui := Gui("+Resize")  ; ウィンドウの最大化、ドラッグリサイズができるようにする。

; いくつかのボタンを作成します:
B1 := MyGui.Add("Button", "Default", "Load a folder")
B2 := MyGui.Add("Button", "x+20", "Clear List")
B3 := MyGui.Add("Button", "x+20", "Switch View")

; MyGui.AddでListViewとそのカラムを作成します:
LV := MyGui.Add("ListView", "xm r20 w700", ["Name","In Folder","Size (KB)","Type"])
LV.ModifyCol(3, "Integer")  ; ソートのため、Size列が整数であることを示す。

; ListViewにアイコンを表示できるように、ImageListを作成する:
ImageListID1 := IL_Create(10)
ImageListID2 := IL_Create(10, 10, true)  ; 小さいアイコンと一緒に大きいアイコンのリストです。; ListViewにImageListを貼り付けて、後でアイコンを表示できるようにする:
LV.SetImageList(ImageListID1)
LV.SetImageList(ImageListID2)

; コントロールイベントを適用する:
LV.OnEvent("DoubleClick", RunFile)
LV.OnEvent("ContextMenu", ShowContextMenu)
B1.OnEvent("Click", LoadFolder)
B2.OnEvent("Click", (*) => LV.Delete())
B3.OnEvent("Click", SwitchView)

; ウィンドウイベントを適用する:
MyGui.OnEvent("Size", Gui_Size)

; Create a popup menu to be used as the context menu:
ContextMenu := Menu()
ContextMenu.Add("Open", ContextOpenOrProperties)
ContextMenu.Add("Properties", ContextOpenOrProperties)
ContextMenu.Add("Clear from ListView", ContextClearRows)
ContextMenu.Default := "Open"  ; Make "Open" a bold font to indicate that double-click does the same thing.

; Display the window:
MyGui.Show()

LoadFolder(*)
{
    static IconMap := Map()
    MyGui.Opt("+OwnDialogs")  ; Forces user to dismiss the following dialog before using main window.
    Folder := DirSelect(, 3, "Select a folder to read:")
    if not Folder  ; The user canceled the dialog.
        return

; Check if the last character of the folder name is a backslash, which happens for root
    ; C:⇄などのディレクトリがあります。If it is, remove it to prevent a double-backslash later on.
    if SubStr(Folder, -1, 1) = "\"
        Folder := SubStr(Folder, 1, -1)  ; Remove the trailing backslash.

; Calculate buffer size required for SHFILEINFO structure.
    sfi_size := A_PtrSize + 688
    sfi := Buffer(sfi_size)

; Gather a list of file names from the selected folder and append them to the ListView:
    LV.Opt("-Redraw")  ; Improve performance by disabling redrawing during load.
    Loop Files, Folder "\*.*"
    {
        FileName := A_LoopFilePath  ; Must save it to a writable variable for use below.

; Build a unique extension ID to avoid characters that are illegal in variable names,
        ; ダッシュのようなまた、このユニークID方式は、アイテムを探す際に
        ; in the array does not require search-loop.
        SplitPath(FileName,,, &FileExt)  ; Get the file's extension.
        if FileExt ~= "i)\A(EXE|ICO|ANI|CUR)\z"
        {
            ExtID := FileExt  ; Special ID as a placeholder.
            IconNumber := 0  ; Flag it as not found so that these types can each have a unique icon.
        }
        else  ; Some other extension/file-type, so calculate its unique ID.
        {
            ExtID := 0  ; Initialize to handle extensions that are shorter than others.
            Loop 7     ; Limit the extension to 7 characters so that it fits in a 64-bit value.
            {
                ExtChar := SubStr(FileExt, A_Index, 1)
                if not ExtChar  ; No more characters.
                    break
                ; Derive a Unique ID by assigning a different bit position to each character:
                ExtID := ExtID | (Ord(ExtChar) << (8 * (A_Index - 1)))
            }
            ; Check if this file extension already has an icon in the ImageLists. If it does,
            ; 何度も呼び出す必要がなく、ロードのパフォーマンスが大幅に向上します、
            ; especially for a folder containing hundreds of files:
            IconNumber := IconMap.Has(ExtID) ? IconMap[ExtID] : 0
        }
        if not IconNumber  ; There is not yet any icon for this extension, so load it.
        {
            ; Get the high-quality small-icon associated with this file extension:
            if not DllCall("Shell32\SHGetFileInfoW", "Str", FileName
            , "Uint", 0, "Ptr", sfi, "UInt", sfi_size, "UInt", 0x101)  ; 0x101 is SHGFI_ICON+SHGFI_SMALLICON
                IconNumber := 9999999  ; Set it out of bounds to display a blank icon.
            else ; Icon successfully loaded.
            {
                ; Extract the hIcon member from the structure:
                hIcon := NumGet(sfi, 0, "Ptr")
                ; Add the HICON directly to the small-icon and large-icon lists.
; Below uses +1 to convert the returned index from zero-based to one-based:
                IconNumber := DllCall("ImageList_ReplaceIcon", "Ptr", ImageListID1, "Int", -1, "Ptr", hIcon) + 1
                DllCall("ImageList_ReplaceIcon", "Ptr", ImageListID2, "Int", -1, "Ptr", hIcon)
                ; Now that it's been copied into the ImageLists, the original should be destroyed:
                DllCall("DestroyIcon", "Ptr", hIcon)
                ; Cache the icon to save memory and improve loading performance:
                IconMap[ExtID] := IconNumber
            }
        }

; Create the new row in the ListView and assign it the icon number determined above:
        LV.Add("Icon" . IconNumber, A_LoopFileName, A_LoopFileDir, A_LoopFileSizeKB, FileExt)
    }
    LV.Opt("+Redraw")  ; Re-enable redrawing (it was disabled above).
    LV.ModifyCol()  ; Auto-size each column to fit its contents.
    LV.ModifyCol(3, 60)  ; Make the Size column at little wider to reveal its header.
}

SwitchView(*)
{
    static IconView := false
    if not IconView
        LV.Opt("+Icon")        ; Switch to icon view.
    else
        LV.Opt("+Report")      ; Switch back to details view.
    IconView := not IconView   ; Invert in preparation for next time.
}

RunFile(LV, RowNumber)
{
    FileName := LV.GetText(RowNumber, 1) ; Get the text of the first field.
    FileDir := LV.GetText(RowNumber, 2)  ; Get the text of the second field.
    try
        Run(FileDir "\" FileName)
    catch
        MsgBox("Could not open " FileDir "\" FileName ".")
}

ShowContextMenu(LV, Item, IsRightClick, X, Y)  ; In response to right-click or Apps key.
{
    ; Show the menu at the provided coordinates, X and Y.  These should be used
    ; because they provide correct coordinates even if the user pressed the Apps key:
    ContextMenu.Show(X, Y)
}

ContextOpenOrProperties(ItemName, *)  ; The user selected "Open" or "Properties" in the context menu.
{
    ; For simplicitly, operate upon only the focused row rather than all selected rows:
    FocusedRowNumber := LV.GetNext(0, "F")  ; Find the focused row.
    if not FocusedRowNumber  ; No row is focused.
        return
    FileName := LV.GetText(FocusedRowNumber, 1) ; Get the text of the first field.
    FileDir := LV.GetText(FocusedRowNumber, 2)  ; Get the text of the second field.
    try
    {
        if (ItemName = "Open")  ; User selected "Open" from the context menu.
            Run(FileDir "\" FileName)
        else
            Run("properties " FileDir "\" FileName)
    }
    catch
        MsgBox("Could not perform requested action on " FileDir "\" FileName ".")
}

ContextClearRows(*)  ; The user selected "Clear" in the context menu.
{
    RowNumber := 0  ; This causes the first iteration to start the search at the top.
    Loop
    {
        ; Since deleting a row reduces the RowNumber of all other rows beneath it,
        ; 検索対象が、以前と同じ行番号になるように、1を引きます。
        ; found (in case adjacent rows are selected):
        RowNumber := LV.GetNext(RowNumber - 1)
        if not RowNumber  ; The above returned zero, so there are no more selected rows.
            break
        LV.Delete(RowNumber)  ; Clear the row from the ListView.
    }
}

Gui_Size(thisGui, MinMax, Width, Height)  ; Expand/Shrink ListView in response to the user's resizing.
{
    if MinMax = -1  ; The window has been minimized. No action needed.
        return
    ; Otherwise, the window has been resized or maximized. Resize the ListView to match.
    LV.Move(,, Width - 20, Height - 40)
}