ComObjConnect()

提供: AutoHotkey Wiki
移動: 案内検索

実行制御 | GUI表示 | 演算・変数 | メモリ・DLL操作 | 文字列操作 | キーボード | マウス | シェル | ウィンドウ | ウィンドウグループ
ステータスバー | コントロール | サウンド | ファイル | INIファイル | レジストリ | 環境変数 | AutoHotkey | その他 | 設定関係 | オブジェクト

ComObjConnect() [AHK_L 53+][編集]

このページの訳は怪しいです。あまり信用しないように。
AHKL 指定の接頭辞で始まるユーザ定義関数に、COMオブジェクトのイベントを接続する。

ComObjConnect(ComObject [, Prefix])

Parameters[編集]

引数名 説明
ComObject イベントを発生させるオブジェクト。
Prefix イベント発生時に、どの関数を呼び出すかを決めるユーザ定義関数の接頭辞を文字列で指定。
省略時はオブジェクトは「非接続」となる。つまりスクリプト側ではイベント通知を受けとることはなくなる。

[v1.1.01+]: This parameter can be an object defined by the script. When an event is raised, the corresponding method is called. The first parameter, which is usually the hidden this parameter, refers to the script-defined object, not the COM object. To catch all events without defining a method for each one, define a __Call meta-function.

Usage[編集]

ComObjConnect を有効利用するには、まず最初にイベントを取り扱う(ハンドルする)ユーザ定義関数をスクリプト内に作成する必要がある。その関数(=イベントハンドラ)は、以下のような構造をとる。

PrefixEventName([Params..., ComObject])
{
    ... イベントを扱うコードを記述 ...
    Return returnValue
}

Prefix は任意の文字列を利用できる一方、EventName は取り扱いたいイベント名を指定しなければならない。

Params はイベント処理で必要となるものをきちんと揃える必要がある。パラメタを必要としないイベント処理では Params は省略する ComObject は省略可能であり、必要な数の Params がきちんと定義されている場合のみに有効となもので、ComObjConnect に引き渡すべきオリジナルのラッパーオブジェクトをへの参照を含んでいるものとなる。なお引き数名の "ComObject" は、意味が通るように適切な名前を利用することが望ましい。

イベントハンドラは戻り値を返すこともある。COMに特有な型の値を返す必要がある場合は、ComObjParameter を用いて必要な型にする。
例) ComObjRetVal(type, value)ComObj(type, value).

ComObjConnect(yourObject, "Prefix") とすることでイベント処理が可能となる。

ComObjConnect(yourObject) とすることでオブジェクトのイベントの接続を切る(ハンドリングを中止する)。

Related[編集]

ComObjCreate(), ComObjGet(), ComObjActive(), ComObjError(), WScript.ConnectObject (MSDN)

Example(s)[編集]

ie := ComObjCreate("InternetExplorer.Application")

; Connects events to corresponding script functions with the prefix "IE_".
ComObjConnect(ie, "IE_")

ie.Visible := true
ie.Navigate("http://www.autohotkey.net/~Lexikos/AutoHotkey_L/")
#Persistent

IE_DocumentComplete(ieEventParam, url, ieFinalParam) {
    global ie
    If (ie != ieEventParam)
        s .= "First parameter is a new wrapper object.`n"
    If (ie == ieFinalParam)
        s .= "Final parameter is the original wrapper object.`n"
    If ((disp1:=ComObjUnwrap(ieEventParam)) == (disp2:=ComObjUnwrap(ieFinalParam)))
        s .= "Both wrapper objects refer to the same IDispatch instance.`n"
    ObjRelease(disp1), ObjRelease(disp2)
    MsgBox % s . "Finished loading " ie.Document.title " @ " url
    ie.Quit()
    ExitApp
}