[Microsoft代理程式從 Windows 7 開始已被取代,而且可能無法在後續版本的 Windows 中使用。]
-
描述
-
會傳回或設定目前控制項是否連接到 Microsoft Agent 伺服器。
-
語法
-
*agent.*Connected [ = 布爾值]
部分 描述 布爾值 布爾運算式,指定控制項是否連接。 True 控件已連線。
言論
在許多情況下,指定控件會自動建立與 Microsoft Agent 伺服器的連線。 例如,在網頁的 <OBJECT> 標記中指定 Microsoft Agent 控制件的 CLSID,會自動開啟伺服器連線並結束頁面關閉連線。 同樣地,針對 Visual Basic 或其他可讓您卸除表單上的控件的語言,執行程式會自動開啟連接,並結束程式關閉連接。 如果伺服器目前未執行,它會自動啟動。
不過,如果您想要在運行時間建立 Agent 控制項,您可能也需要使用 Connected 屬性明確開啟與伺服器的新連線。 例如,在 Visual Basic 中,您可以使用 Set 語句搭配 New 關鍵詞 (或 CreateObject 函式),在運行時間建立 ActiveX 物件。 雖然這會建立 物件,但可能不會建立與伺服器的連線。 您可以在呼叫 Microsoft Agent 程式設計介面的任何程式代碼之前,使用 Connected 屬性,如下列範例所示:
' Declare a global variable for the control
Dim MyAgent as Agent
' Create an instance of the control using New
Set MyAgent = New Agent
' Open a connection to the server
MyAgent.Connected = True
' Load a character
MyAgent.Characters.Load "Genie", " Genie.acs"
' Display the character
MyAgent.Characters("Genie").Show
使用此技術建立控件並不會公開 Agent 控制件的事件。 在 Visual Basic 5.0(和更新版本中),您可以在變數宣告中包含 控件,並使用變數宣告中的 WithEvents 關鍵詞來存取控件的事件:
Dim WithEvents MyAgent as Agent
' Create an instance of the control using New
Set MyAgent = New Agent
使用 WithEvents 在運行時間建立Agent控件的實例,會自動開啟與 Microsoft Agent 伺服器的連線。 因此,您不需要包含 Connected 語句。
您可以釋放您對 Agent 物件建立的所有參考,例如 IAgentCtlCharacterEx 和 IAgentCtlCommandEx,以關閉與伺服器的連線。 您也必須釋放對 Agent 控件本身的參考。 在 Visual Basic 中,您可以將物件的變數設定為 Nothing來釋放對象的參考。 如果您已載入字元,請先卸除字元,再釋放字元物件。
Dim WithEvents MyAgent as Agent
Dim Genie as IAgentCtlCharacterEx
Sub Form_Load
' Create an instance of the control using New
Set MyAgent = New Agent
' Open a connection to the server
MyAgent.Connected = True
' Load the character into the Characters collection
MyAgent.Characters.Load "Genie", " Genie.acs"
' Create a reference to the character
Set Genie = MyAgent.Characters("Genie")
End Sub
Sub CloseConnection
' Unload the character
MyAgent.Characters.Unload "Genie"
' Release the reference to the character object
Set Genie = Nothing
' Release the reference to the Agent control
Set MyAgent = Nothing
End Sub
注意
您無法藉由釋放已新增元件的參考,關閉與伺服器的連線。 例如,您無法在網頁上關閉與伺服器的連線,其中您使用 <OBJECT> 標記來宣告控件,或在表單上卸載控件的 Visual Basic 應用程式中。 釋放所有 Agent 參考會減少 Agent 的工作集,聯機會維持在流覽至下一頁或結束應用程式之前。