[此功能僅支援在 Windows XP 或更早版本下。 ]
本文包含屬於 Windows Shell API 之 ActiveDesktop 對象的相關信息。 此物件透過其 IActiveDesktop 介面,可讓您在桌面上新增、移除及變更專案。
Active Desktop 介面的概觀
Active Desktop 是 Microsoft Internet Explorer 4.0 引進的功能,可讓您將 HTML 檔和專案(例如Microsoft ActiveX 控件和 Java 小程式)直接包含在桌面上。 IActiveDesktop 介面是 Windows Shell API 的一部分,可用來以程式設計方式新增、移除和修改桌面上的專案。 您也可以使用通道定義格式 (CDF) 檔案來新增 Active Desktop 專案。
存取使用中桌面
若要存取 Active Desktop,用戶端應用程式必須使用 CoCreateInstance 函式建立 ActiveDesktop 對象的實例 CLSID_ActiveDesktop,並擷取物件的 IActiveDesktop 介面指標。
下列範例示範如何擷取 IActiveDesktop 介面的指標。
HRESULT hr;
IActiveDesktop *pActiveDesktop;
//Create an instance of the Active Desktop
hr = CoCreateInstance(CLSID_ActiveDesktop, NULL, CLSCTX_INPROC_SERVER,
IID_IActiveDesktop, (void**)&pActiveDesktop);
//Insert code to call the IActiveDesktop methods
// Call the Release method
pActiveDesktop->Release();
新增桌面項目
有三種方法可用來新增桌面專案:IActiveDesktop::AddDesktopItem、IActiveDesktop::AddDesktopItemWithUI,以及 IActiveDesktop::AddUrl。 新增至使用中桌面的每個桌面項目都必須有不同的來源URL。
IActiveDesktop::AddDesktopItemWithUI 和 IActiveDesktop::AddUrl 方法都提供選項,以顯示可在將桌面專案新增至 Active Desktop 之前顯示的各種使用者介面。 介面會驗證使用者是否要將桌面專案新增至其作用中桌面。 介面也會通知使用者 URL 安全性區域設定所保證的任何安全性風險,並詢問使用者是否要為此桌面專案建立訂用帳戶。 這兩種方法也提供隱藏使用者介面的方式。 IActiveDesktop::AddDesktopItem 方法需要呼叫 IActiveDesktop::ApplyChanges,才能更新登錄。 針對 IActiveDesktop::AddDesktopItemWithUI,用戶端應用程式必須立即釋放 IActiveDesktop 介面,然後使用 CoCreateInstance 函式來擷取包含新加入桌面專案的 ActiveDesktop 物件實例的介面。
IActiveDesktop::AddDesktopItem 方法將指定的桌面項目新增至 Active Desktop,除非 URL 安全區域設定阻止此項操作。 如果 URL 安全性區域設定不允許在未提示使用者的情況下新增桌面專案,此方法就會失敗。 IActiveDesktop::AddDesktopItem 也需要呼叫 IActiveDesktop::ApplyChanges,才能更新登錄。
下列範例示範如何使用 IActiveDesktop::AddDesktopItem 方法新增桌面專案。
HRESULT hr;
IActiveDesktop *pActiveDesktop;
COMPONENT compDesktopItem;
//Create an instance of the Active Desktop
hr = CoCreateInstance(CLSID_ActiveDesktop, NULL, CLSCTX_INPROC_SERVER,
IID_IActiveDesktop, (void**)&pActiveDesktop);
// Initialize the COMPONENT structure
compDesktopItem.dwSize = sizeof(COMPONENT);
// Insert code that adds the information about the desktop item
// to the COMPONENT structure
// Add the desktop item
pActiveDesktop->AddDesktopItem(&compDesktopItem,0);
// Save the changes to the registry
pActiveDesktop->ApplyChanges(AD_APPLY_ALL);
列舉桌面項目
若要列舉當前安裝在 Active Desktop 上的桌面項目,用戶端應用程式必須藉由呼叫 IActiveDesktop::GetDesktopItemCount 方法來擷取已安裝的桌面項目總數,然後建立一個迴圈,藉由使用桌面項目索引來呼叫 IActiveDesktop::GetDesktopItem 方法,擷取每個桌面項目的 COMPONENT 結構。
下列範例示範列舉桌面專案的一種方式。
HRESULT hr;
IActiveDesktop *pActiveDesktop;
COMPONENT compDesktopItem;
int intCount;
int intIndex = 0;
//Create an instance of the Active Desktop
hr = CoCreateInstance(CLSID_ActiveDesktop, NULL, CLSCTX_INPROC_SERVER,
IID_IActiveDesktop, (void**)&pActiveDesktop);
pActiveDesktop->GetDesktopItemCount(&intCount,0);
compDesktopItem.dwSize = sizeof(COMPONENT);
while(intIndex<=(intCount-1))
{
//get the COMPONENT structure for the current desktop item
pActiveDesktop->GetDesktopItem(intIndex, &compDesktopItem,0);
//Insert code that processes the structure
//Increment the index
intIndex++;
//Insert code to clean-up structure for next component
}
// Call the Release method
pActiveDesktop->Release();