共用方式為


載入字元和動畫資料

[從 Windows 7 開始,Microsoft Agent 已被取代,而且在後續版本的 Windows 中可能無法使用。]

擁有 IAgentEx 介面的指標之後,您可以使用 Load 方法來載入字元並擷取其 IAgentCharacterEx 介面。 字元的 Load 路徑有三種不同的可能性。 第一個與 Microsoft Agent 1.5 相容,其中指定的路徑是字元檔案的完整路徑和檔案名。 第二種可能性是只指定檔案名,在此情況下,Agent 會在其 Chars 目錄中尋找。 最後一個可能性是提供空的 Variant 參數,以載入預設字元。

   // Create a variant to store the filename of the character to load

   const LPWSTR kpwszCharacter = L"merlin.acs";

   VariantInit(&vPath);

   vPath.vt = VT_BSTR;
   vPath.bstrVal = SysAllocString(kpwszCharacter);

   // Load the character

   hRes = pAgentEx->Load(vPath, &lCharID, &lRequestID);

   // Get its IAgentCharacterEx interface

   hRes = pAgentEx->GetCharacterEx(lCharID, &pCharacterEx);

您可以使用這個介面來存取字元的方法:

   // Show the character.  The first parameter tells Microsoft
   // Agent to show the character by playing an animation.

   hRes = pCharacterEx->Show(FALSE, &lRequestID);

   // Make the character speak

   bszSpeak = SysAllocString(L"Hello World!");

   hRes = pCharacterEx->Speak(bszSpeak, NULL, &lRequestID);

   SysFreeString(bszSpeak);

當您不再需要 Microsoft Agent 服務時,例如用戶端應用程式關閉時,請釋放其介面。 請注意,釋放字元介面並不會卸載字元。 在釋放IAgentEx介面之前,請先呼叫Unload方法來執行此動作:

// Clean up

if (pCharacterEx) {

   // Release the character interface

   pCharacterEx->Release();

   // Unload the character.  NOTE:  releasing the character
   // interface does NOT make the character go away.  You must
   // call Unload.

   pAgentEx->Unload(lCharID);
}
   
// Release the Agent

pAgentEx->Release();

VariantClear(&vPath);