跳轉到內容

Google 桌面外掛開發

0% developed
來自華夏公益教科書

你需要的東西

[編輯 | 編輯原始碼]

如何開始

[編輯 | 編輯原始碼]

你需要建立一個 COM 物件。

什麼是 COM 物件,為什麼我需要建立它?

[編輯 | 編輯原始碼]

GD 使用 COM 物件作為外掛。實際上,這些類似於 ActiveX 元件。它們旨在可重複使用。

如何建立 COM 物件

[編輯 | 編輯原始碼]

在 Visual Studio 中,建立一個 C# 類庫。

類庫專案的初步程式碼

[編輯 | 編輯原始碼]

你應該實現兩種(2)方法

static void ComRegisterFunctionAttribute(Type t) {
include initialization code here
}

每當你將 COM 物件註冊到系統時,都會呼叫此函式。你可以在這裡放置初始化程式碼,例如將我們的外掛註冊到 GD 的程式碼(稍後詳細介紹)。

static void ComUnregisterFunctionAttribute(Type t) {
}

每當你登出 COM 物件時,都會呼叫此函式。

你應該有以下語句

using System.Runtime.InteropServices;

當我編譯類庫專案時會建立哪些型別的檔案?

[編輯 | 編輯原始碼]

構建類庫專案後,Visual Studio 會在你的輸出資料夾中生成以下檔案型別:.dll 和 .tlb

生成的 .dll 檔案實際上是一個 COM 物件,我們需要將其註冊到系統。

如何載入/註冊 COM 物件

[編輯 | 編輯原始碼]
Use: regasm [dllfile] /tlb

Regasm.exe 位於你的 .Net Framework bin 資料夾中。

載入 COM 物件後首先執行什麼方法/程式碼?

[編輯 | 編輯原始碼]

系統會呼叫 ComRegisterFunctionAttribute,因此你必須實現此方法。我們在這裡放置我們的初始化,例如將我們的外掛註冊到 GD 的程式碼塊。

將外掛註冊到 GD

[編輯 | 編輯原始碼]
try {
GoogleDesktopRegistrarClass registrar = new GoogleDesktopRegistrarClass();
// Start component registration by specifying our attributes
object[] descriptions = {
"Title", pluginName,
"Description", pluginName,
"Icon", ""
};

registrar.StartComponentRegistration(controlGuid, descriptions);

IGoogleDesktopRegisterDisplayPlugin displayRegistration = 
(IGoogleDesktopRegisterDisplayPlugin)

registrar.GetRegistrationInterface("GoogleDesktop.DisplayPluginRegistration");

displayRegistration.RegisterPlugin(controlGuid, false);
// Done with component registration.
registrar.FinishComponentRegistration();
}
catch (Exception e) {
MessageBox.Show("Exception thrown during registration. Description=" + e.Message);
}

我的外掛在 GD 中註冊後會發生什麼?

[編輯 | 編輯原始碼]

你的外掛將出現在 Google 桌面的新增/刪除面板列表中。

高階主題

[編輯 | 編輯原始碼]
Clipboard

待辦事項

華夏公益教科書