為Windows Live Writer開發外掛程式——InsertSearchPageLink

來源:互聯網
上載者:User

兩周前,我曾經發布了一個Windows Live Writer的小外掛程式——InsertSearchPageLink,用來在Windows Live Writer中插入一個指向某個搜尋引擎的某個搜尋索引鍵頁面的超連結(關於這個外掛程式的功能介紹以及下載,請參考《為Windows Live Writer寫了一個小外掛程式——插入搜尋網頁面連結》)。今天終於有空,把編寫的過程寫出來與大家分享,希望能起到拋磚引玉的作用,讓朋友們都能編寫出自己的外掛程式,擴充Windows Live Writer的功能,方便大家!

 

Windows Live Writer擴充綜述

目前為止,Windows Live Writer的擴充功能還不是很強,它提供給我們如下兩個方面對其擴充的可能:

  1. 應用程式層級的API:這部分API讓我們能夠在外部程式中啟動並調用到Windows Live Writer的功能,以COM組件的形式提供。如果你要編寫一個Firefox工具條上的小按鈕,例如“Blog It!”之類,那麼顯然應該使用這類API。
  2. 文章內容外掛程式:這部分API允許開發人員對正在書寫的文章內容進行修飾,實現與Windows Live Writer中內建的“Insert Link...”、“Insert Picture...”等類似的功能。

若要編寫文章內容外掛程式,則需要從ContentSource或SmartContentSource兩個基類中選擇其一併繼承自它。這兩個基類提供了編寫文章內容外掛程式所需要的基礎設施。ContentSource和SmartContentSource的區別如下:

  1. ContentSource:如果你需要編寫的外掛程式將要在內容中插入一段簡單的,插入之後不再修改(所謂“單向插入”)的內容片斷,例如一個超連結,或是自訂的一段著作權資訊等,那麼應該選擇該類型的基類。Windows Live Writer中內建的“Insert Link...”就屬於這類外掛程式。
  2. SmartContentSource:如果你需要編寫的外掛程式將在內容中插入一段較為複雜的,插入之後還可能要修改(所謂“雙向互動”)的內容片杜阿,例如一張圖片、一幅地圖等,那麼則應該選擇這個類型的基類。Windows Live Writer中內建的“Insert Picture...”就屬於這類外掛程式。

對於繼承於SmartContentSource基類的外掛程式,在Windows Live Writer的編輯器中選擇用該外掛程式插入的內容,你將會在右邊看到該內容的可設定屬性。例如用“Insert Picture...”插入一幅圖片之後,選中該圖片,可以看到Windows Live Writer視窗的右邊變成了如下的配置面板:

而對於繼承於ContentSource基類的外掛程式,一旦內容插入之後,將自動變成HTML形式,於其他的手工輸入內容沒有任何區別。

我們將要編寫的InsertSearchPageLink,就是一個最簡單的,繼承於ContentSource基類的外掛程式。

 

在Visual Studio中建立項目

開啟Visual Studio,建立一個“Class Library”類型的項目。

然後在項目中添加WindowsLive.Writer.Api.dll程式集的引用(一般位於C:\Program Files\Windows Live Writer\檔案夾中),以及相應的System.Drawing、System.Windows.Forms等引用。添加好之後Solution Explorer如下:

按右鍵Class Library項目,選擇“Properties”。在“Build Event”選項卡中的“Post Build Event command line”中寫入如下命令,讓每次編譯完成之後自動將該外掛程式拷貝到Windows Live Writer的外掛程式目錄下:

XCOPY /D /Y /R "$(TargetPath)" "C:\Program Files\Windows Live Writer\Plugins\"

 

繼承於ContentSource基類

接下來,讓我們在這個Class Library中建立一個類,並繼承於WindowsLive.Writer.Api.ContentSource基類:

[WriterPlugin("CA5ACC8E-5007-4df8-B110-DD6420C4CD4F", "Search Page Link",
    PublisherUrl = "http://dflying.cnblogs.com",
    ImagePath = "Icon.bmp",
    Description = "Insert Search Page Link.")]
[InsertableContentSource("Search Page Link")]
public class InsertSearchPageLinkPlugin : ContentSource
{
    //......
}

注意我們為該類添加了WriterPlugin屬性:

  1. 第一個參數為一個Guid,我們可以用Visual Studio自動產生,其標識的作用。
  2. 第二個參數為該外掛程式的名稱,將在Windows Live Writer的外掛程式管理器中看到,
  3. PublisherUrl為發行者的網站地址,這裡就寫了我的Blog地址。
  4. ImagePath為外掛程式的表徵圖檔案路徑,注意該表徵圖應為18*16大小,且一定以Embedded Resource形式編譯在程式集中。如下:
  5. Description為外掛程式的一小段描述介紹。

該類還應用了InsertableContentSource屬性,其中的參數表示“Insert”菜單和“Insert”快捷面板(在Windows Live Writer的的右下角)中該外掛程式的名稱。

 

覆寫基類的CreateContent()方法

CreateContent()方法用來得到該外掛程式所產生的一個HTML字串,一旦使用者點擊了“Insert”菜單或“Insert”快捷面板中的“Insert XXX”連結,Windows Live Writer就將自動調用該方法,所以我們應該在該方法中彈出一個小視窗用來配置講要插入的內容。

該方法的代碼如下:

public override DialogResult CreateContent(IWin32Window dialogOwner, ref string newContent)
{
    using (InsertLinkForm insertLinkForm = new InsertLinkForm())
    {
        DialogResult result = insertLinkForm.ShowDialog();
        newContent = insertLinkForm.LinkHTMLString;
 
        return result;
    }
}

注意到CreateContent()方法的第二個參數為ref string newContent,這個ref string就表示由該外掛程式產生的HTML代碼。

該方法的實現也非常簡單,僅僅是建立了一個InsertLinkForm,然後就將所有任務都交給了它。

 

編寫InsertLinkForm

InsertLinkForm就是一個再普通不過的WinForm了,一個TextBox、一個ListBox、一個CheckBox和一對“OK”“Cancel”按鈕。設計期介面如下:

切換到代碼部分,在其建構函式中為該ListBox添加一系列的選項:

public InsertLinkForm()
{
    InitializeComponent();
 
    // whatever method you like to fill the list
    List<SearchEngineEntry> searchEngines = new List<SearchEngineEntry>();
    searchEngines.Add(new SearchEngineEntry("Google.com", "http://www.google.com/search?q={0}"));
    searchEngines.Add(new SearchEngineEntry("Google.cn", "http://www.google.cn/search?q={0}"));
    searchEngines.Add(new SearchEngineEntry("Baidu.com", "http://www.baidu.com/s?wd={0}"));
    searchEngines.Add(new SearchEngineEntry("Search.live.com", "http://search.live.com/results.aspx?q={0}"));
    searchEngines.Add(new SearchEngineEntry("Search.yahoo.com", "http://search.yahoo.com/search?p={0}"));
    searchEngines.Add(new SearchEngineEntry("Search.cn.yahoo.com", "http://search.cn.yahoo.com/search?p={0}"));
    searchEngines.Add(new SearchEngineEntry("Answers.com", "http://www.answers.com/main/ntquery?s={0}"));
 
    // bind to search engine list
    lbSearchEngines.DataSource = searchEngines;
    lbSearchEngines.DisplayMember = "DisplayText";
    lbSearchEngines.SelectedIndex = 0;
}

注意到這裡用到了一個名為SearchEngineEntry的類,該類非常簡單:

class SearchEngineEntry{private string m_displayText;public string DisplayText{get { return m_displayText; }set { m_displayText = value; }}private string m_baseUrl;public string BaseUrl{get { return m_baseUrl; }set { m_baseUrl = value; }}public SearchEngineEntry(){}public SearchEngineEntry(string displayText, string baseUrl){m_displayText = displayText;m_baseUrl = baseUrl;}}

回到InsertLinkForm的代碼中,為該類添加一個公有屬性,用來表示該Form配置後產生的HTML字串:

private string m_linkHTMLString;public string LinkHTMLString{get { return m_linkHTMLString; }set { m_linkHTMLString = value; }}

然後是“OK”按鈕的事件處理函數,只是簡單地格式化並產生一段HTML,然後保留在m_linkHTMLString中而已:

private void btnOK_Click(object sender, EventArgs e){// ensure there's input in keyword fieldif (tbKeyword.Text == string.Empty){epKeyword.SetError(tbKeyword, "Please input the Keyword.");tbKeyword.Focus();return;}// build result HTML link stringSearchEngineEntry selectedSearchEngine = lbSearchEngines.SelectedItem as SearchEngineEntry;m_linkHTMLString = string.Format("<a href=\"{0}\" {1} title=\"Search '{2}' using {3}\" >{4}</a>",string.Format(selectedSearchEngine.BaseUrl, tbKeyword.Text),cbNewWindow.Checked ? "target=\"_blank\"" : string.Empty,tbKeyword.Text,selectedSearchEngine.DisplayText,tbKeyword.Text);// result should be ok to close this dialog.this.DialogResult = DialogResult.OK;}

這樣,當使用者在InsertLinkForm中配置完成之後點擊“OK”按鈕,將執行上述事件處理函數,其中產生HTML字串並保留在m_linkHTMLString中,然後該Form將被關閉。

之後該Form的調用者——CreateContent()方法將得到該InsertLinkForm的LinkHTMLString屬性,並將其交給Windows Live Writer。以後的事情,自有Windows Live Writer為我們完成。搞定!

 

調試外掛程式

編寫程式不需要調試是不可思議的,哪怕這個最簡單的小程式也不例外。可是,這個“外掛程式”類型的程式應該如何調試呢?

記得在前面我們已經設定了項目的Post Build Event,每次編譯成功之後將自動將編譯結果拷貝到Windows Live Writer的外掛程式目錄中。這時若你啟動Windows Live Writer,則可以看到該外掛程式已經被順利載入了進去,當然,前提是外掛程式沒有致命錯誤。

然後,我們可以在Visual Studio中點擊Debug\Attach to Process,然後手工將Visual Studio的Debugger附加到這個Windows Live Writer的執行個體中。注意我們需要Attach to Managed Code:

這樣在Visual Studio中的外掛程式代碼中加入斷點,然後切換回Windows Live Writer,啟動該外掛程式,Visual Studio將順利開始調試。

 

代碼下載

下在前請先閱讀Dflying著作權說明。若您下載或使用該軟體,表示您理解並接受該協議;若您不同意,請勿下載或使用。

  1. 本程式用到的InsertSearchPageLink原始碼可以在此下載(62K):Dflying.LiveWriter.InsertSearchPageLinkPlugin-Source.zip
  2. 若您只是想使用該外掛程式,請下載編譯好的DLL(4K):Dflying.LiveWriter.InsertSearchPageLinkPlugin.zip。將其解壓縮至Windows Live Writer的安裝目錄(預設為C:\Program Files\Windows Live Writer\Plugins)中。然後重新啟動Windows Live Writer,即可在介面右下角看到這個外掛程式。

 

相關資源

  1. Windows Live Writer的首頁:http://windowslivewriter.spaces.live.com/
  2. Windows Live Writer的SDK:Windows Live Writer SDK
  3. Writer DevZone

 

(PS:我基本沒用WinForm開發過什麼東西,更是沒有這方面的什麼開發經驗。其中代碼如由不得體之處,還請各位不吝指出。謝謝!)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.