<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆 - 117  文章 - 72  trackbacks - 0

    聲明:原創(chuàng)作品(標(biāo)有[原]字樣)轉(zhuǎn)載時(shí)請(qǐng)注明出處,謝謝。

    常用鏈接

    常用設(shè)置
    常用軟件
    常用命令
     

    訂閱

    訂閱

    留言簿(7)

    隨筆分類(130)

    隨筆檔案(123)

    搜索

    •  

    積分與排名

    • 積分 - 155536
    • 排名 - 390

    最新評(píng)論

    [標(biāo)題]: 開發(fā)自己的Windows Live Writer插件
    [時(shí)間]:2009-10-04
    [摘要]: 開發(fā)一個(gè)Windows Live Writer插件,在寫博客時(shí),添加自己的代碼樣式。最終效果是,在Windows Live Writer中選中代碼,然后點(diǎn)擊插件,將代碼包含在<div class="mycode">your code</div>中。
    [關(guān)鍵字]: plugin、Windows Live Writer、code、format、highlight、 插件、博客、blog、msi、package、打包、C#、前綴、后綴、簡單代碼、樣式
    [環(huán)境]: Windows Live Writer 14.0.8089.726 zh-cn, Visual Studio 2008 , Windows XP SP3 , Wordpress 2.8.4
    [作者]:Winty  (wintys@gmail.com) http://www.tkk7.com/wintys

    [正文]:

             開發(fā)一個(gè)Windows Live Writer插件,在寫博客時(shí),添加自己的代碼樣式。最終效果是,在Windows Live Writer中選中代碼,然后點(diǎn)擊插件,將代碼包含在<div class="mycode">your code</div>中。當(dāng)然要在最終發(fā)表的博客上添加.mycode 的CSS樣式才有效果。

            .mycode樣式如下:

    .mycode {
        margin: 10px;
        padding: 10px;
        background: #DDEDFB;
        border: 1px solid #428EDE; 
        text-align: left;
        /*width:500px;*/
        overflow-x:auto;
        font-size:20px;
        white-space:nowrap;
        *white-space: normal;
        WORD-WRAP: break-word;/*IE*/
        word-break:break-all;/*IE*/
    }
    選中代碼,點(diǎn)擊"WintyCodeArea":
    image 
     
    效果如下圖
    (在Writer需啟用"使用主題編輯",并已從博客中獲取主題,才能立即看到效果):
    image 
     

    0、準(zhǔn)備工作

    • 在Visual Studio 2008中新建C#"Class Library"項(xiàng)目
    • 在項(xiàng)目中添加References:"C:\Program Files\Windows Live\Writer\WindowsLive.Writer.Api.dll"
    • 在項(xiàng)目屬性的"Build Events"=>"Post Build Event command line"添加:(XCOPY /D /Y /R "$(TargetPath)" "C:\Program Files\Windows Live\Writer\Plugins")

     

    1、"Class Library"主類,繼承于ContentSource

    WintyCodeArea.cs:

    using System.Windows.Forms;
    using WindowsLive.Writer.Api;

    /*
    2009-10-02

    Winty

    wintys@gmail.com

    http://www.tkk7.com/wintys
      */
    namespace MyWindowsLiveWriterPlugin
    {
        /*Plugin 主類*/
        [WriterPlugin("{7DFB5431-D7DA-4e61-9E4B-056D30DFDB63}",
            "WintyCodeArea",
            PublisherUrl = "http://www.tkk7.com/wintys",
            ImagePath = "image.jpg",
            HasEditableOptions = true,
            Description = "Insert <div class=\"mycode\">your code</div>\nhttp://www.tkk7.com/wintys\nwintys@gmail.com")]
        [InsertableContentSource("WintyCodeArea")]
        public class WintyCodeArea : ContentSource
        {
            WintyCodeAreaSettings m_settings;

            public override void Initialize(IProperties pluginOptions)
            {
                base.Initialize(pluginOptions);
                m_settings = new WintyCodeAreaSettings(pluginOptions);
            }
            public override DialogResult CreateContent(IWin32Window dialogOwner, ref string content)
            {
                string originalContent = content;

                content = m_settings.FrontCode;
                if(m_settings.EscapeCode)
                    content +=  System.Web.HttpUtility.HtmlEncode(originalContent);
                else
                    content += originalContent;
                content += m_settings.BackCode;

                return DialogResult.OK;
            }

            public override void EditOptions(IWin32Window dialogOwner)
            {
                SettingForm settingForm = new SettingForm(m_settings);
                settingForm.ShowDialog(dialogOwner);
            }
        }
    }

     

             Initialize()、EditOptions()并不是必須的,這里因?yàn)橛玫搅?設(shè)置選項(xiàng)"窗口,才需要。

    CreateContent(IWin32Window dialogOwner, ref string content)在此為必須,content傳入值為Live Writer當(dāng)前被選中的高亮區(qū)的HTML代碼,無論在編輯還是在源代碼視圖中都是這樣的。content的傳出值為你修改后的HTML代碼,最終將在Live Writer中顯示的。

            在CreateContent()中也可以彈出窗體,此處并未用到。以下是代碼示例:

    public override DialogResult CreateContent(IWin32Window dialogOwner, ref string content)
    {
        using (InsertCodeForm insertCodeForm = new InsertCodeForm())
        {
            DialogResult result = insertCodeForm.ShowDialog();
            content = insertCodeForm.MyCode;
            return result;
        }
    }

            相應(yīng)的InsertCodeForm類的部分代碼如下:

    public partial class InsertCodeForm : Form
    {
        private string m_MyCode;
        public string MyCode
        {
            get { return m_MyCode; }
            set { m_MyCode = value; }
        }

        public InsertCodeForm()
        {
            InitializeComponent();
        }

        private void buttonInsert_Click(object sender, EventArgs e)
        {
            if (textBoxCode.Text == string.Empty)
            {
                return;
            }

            m_MyCode = "<div class=\"mycode\">";
            m_MyCode += System.Web.HttpUtility.HtmlEncode(textBoxCode.Text);
            m_MyCode += "</div>";           

            this.DialogResult = DialogResult.OK;
        }
    }


     

    2、用于設(shè)置WintyCodeArea插件行為的類

    WintyCodeAreaSettings.cs:

    using WindowsLive.Writer.Api;

    namespace MyWindowsLiveWriterPlugin
    {
        class WintyCodeAreaSettings
        {
            IProperties m_properties;

            private const string FRONT_CODE = "FRONT_CODE";//前綴代碼
            private const string BACK_CODE = "BACK_CODE";//后綴代碼
            private const string ESCAPE_CODE = "ESCAPE_CODE";//是否轉(zhuǎn)義代碼

            public const string DEFAULT_FRONT_CODE = "<div class=\"mycode\">";
            public const string DEFAULT_BACK_CODE = "</div>";
            public const bool   DEFAULT_ESCAPE_CODE = false;

            public WintyCodeAreaSettings(IProperties properties)
            {
                m_properties = properties;
            }

            public string FrontCode
            {
                get
                {
                    return m_properties.GetString(FRONT_CODE, DEFAULT_FRONT_CODE);
                }
                set
                {
                    m_properties.SetString(FRONT_CODE, value);
                }
            }

            public string BackCode
            {
                get
                {
                    return m_properties.GetString(BACK_CODE, DEFAULT_BACK_CODE);
                }
                set
                {
                    m_properties.SetString(BACK_CODE, value);
                }
            }

            public bool EscapeCode
            {
                get
                {
                    return m_properties.GetBoolean(ESCAPE_CODE, DEFAULT_ESCAPE_CODE);
                }
                set
                {
                    m_properties.SetBoolean(ESCAPE_CODE, value);
                }
            }

        }
    }

     

    3、"設(shè)置窗口"的代碼

            點(diǎn)擊"工具=>選項(xiàng)"就可以找到這個(gè)設(shè)置窗口。

    image

            WintyCodeArea的設(shè)置窗口:

    image

            所謂的轉(zhuǎn)義原始內(nèi)容,就是將所選內(nèi)容中的特殊HTML字符進(jìn)行編碼(空格與換行不變)。

     

    SettingForm.cs:

    using System;
    using System.Windows.Forms;

    namespace MyWindowsLiveWriterPlugin
    {
        partial class SettingForm : Form
        {
            WintyCodeAreaSettings m_settings;

            public SettingForm(WintyCodeAreaSettings settings)
            {
                InitializeComponent();
                //Winty's initialization
                m_settings = settings;
                txtFrontCode.Text = m_settings.FrontCode;
                chkEscapeCode.Checked = m_settings.EscapeCode;
                textBackCode.Text = m_settings.BackCode;
            }

            /*保存設(shè)置*/
            private void btnOK_Click(object sender, EventArgs e)
            {
                m_settings.FrontCode = txtFrontCode.Text;
                m_settings.EscapeCode = chkEscapeCode.Checked;
                m_settings.BackCode = textBackCode.Text;

                Close();
            }

            /*恢復(fù)默認(rèn)設(shè)置*/
            private void btnRestoreDefault_Click(object sender, EventArgs e)
            {
                m_settings.FrontCode = WintyCodeAreaSettings.DEFAULT_FRONT_CODE;
                m_settings.EscapeCode = WintyCodeAreaSettings.DEFAULT_ESCAPE_CODE;
                m_settings.BackCode = WintyCodeAreaSettings.DEFAULT_BACK_CODE;

                txtFrontCode.Text = m_settings.FrontCode;
                chkEscapeCode.Checked = m_settings.EscapeCode;
                textBackCode.Text = m_settings.BackCode;
            }
        }
    }

     

    SettingForm.Designer.cs(這是Visual Studio根據(jù)設(shè)計(jì)的窗體生成的代碼):

    namespace MyWindowsLiveWriterPlugin
    {
        partial class SettingForm
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }

            #region Windows Form Designer generated code

            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.btnOK = new System.Windows.Forms.Button();
                this.chkEscapeCode = new System.Windows.Forms.CheckBox();
                this.labelFont = new System.Windows.Forms.Label();
                this.labelBack = new System.Windows.Forms.Label();
                this.txtFrontCode = new System.Windows.Forms.TextBox();
                this.textBackCode = new System.Windows.Forms.TextBox();
                this.btnRestoreDefault = new System.Windows.Forms.Button();
                this.SuspendLayout();
                //
                // btnOK
                //
                this.btnOK.Location = new System.Drawing.Point(222, 211);
                this.btnOK.Name = "btnOK";
                this.btnOK.Size = new System.Drawing.Size(113, 29);
                this.btnOK.TabIndex = 0;
                this.btnOK.Text = "設(shè)置";
                this.btnOK.UseVisualStyleBackColor = true;
                this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
                //
                // chkEscapeCode
                //
                this.chkEscapeCode.AutoSize = true;
                this.chkEscapeCode.Location = new System.Drawing.Point(127, 94);
                this.chkEscapeCode.Name = "chkEscapeCode";
                this.chkEscapeCode.Size = new System.Drawing.Size(96, 16);
                this.chkEscapeCode.TabIndex = 1;
                this.chkEscapeCode.Text = "轉(zhuǎn)義原始內(nèi)容";
                this.chkEscapeCode.UseVisualStyleBackColor = true;
                //
                // labelFont
                //
                this.labelFont.AutoSize = true;
                this.labelFont.Location = new System.Drawing.Point(48, 12);
                this.labelFont.Name = "labelFont";
                this.labelFont.Size = new System.Drawing.Size(53, 12);
                this.labelFont.TabIndex = 2;
                this.labelFont.Text = "前綴代碼";
                //
                // labelBack
                //
                this.labelBack.AutoSize = true;
                this.labelBack.Location = new System.Drawing.Point(48, 125);
                this.labelBack.Name = "labelBack";
                this.labelBack.Size = new System.Drawing.Size(53, 12);
                this.labelBack.TabIndex = 4;
                this.labelBack.Text = "后綴代碼";
                //
                // txtFrontCode
                //
                this.txtFrontCode.Location = new System.Drawing.Point(125, 12);
                this.txtFrontCode.Multiline = true;
                this.txtFrontCode.Name = "txtFrontCode";
                this.txtFrontCode.Size = new System.Drawing.Size(247, 64);
                this.txtFrontCode.TabIndex = 5;
                //
                // textBackCode
                //
                this.textBackCode.Location = new System.Drawing.Point(125, 125);
                this.textBackCode.Multiline = true;
                this.textBackCode.Name = "textBackCode";
                this.textBackCode.Size = new System.Drawing.Size(247, 64);
                this.textBackCode.TabIndex = 6;
                //
                // btnRestoreDefault
                //
                this.btnRestoreDefault.Location = new System.Drawing.Point(88, 211);
                this.btnRestoreDefault.Name = "btnRestoreDefault";
                this.btnRestoreDefault.Size = new System.Drawing.Size(106, 29);
                this.btnRestoreDefault.TabIndex = 7;
                this.btnRestoreDefault.Text = "恢復(fù)默認(rèn)設(shè)置";
                this.btnRestoreDefault.UseVisualStyleBackColor = true;
                this.btnRestoreDefault.Click += new System.EventHandler(this.btnRestoreDefault_Click);
                //
                // SettingForm
                //
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(420, 252);
                this.Controls.Add(this.btnRestoreDefault);
                this.Controls.Add(this.textBackCode);
                this.Controls.Add(this.txtFrontCode);
                this.Controls.Add(this.labelBack);
                this.Controls.Add(this.labelFont);
                this.Controls.Add(this.chkEscapeCode);
                this.Controls.Add(this.btnOK);
                this.Name = "SettingForm";
                this.Text = "WintyCodeArea Settings";
                this.ResumeLayout(false);
                this.PerformLayout();

            }

            #endregion

            private System.Windows.Forms.Button btnOK;
            private System.Windows.Forms.CheckBox chkEscapeCode;
            private System.Windows.Forms.Label labelFont;
            private System.Windows.Forms.Label labelBack;
            private System.Windows.Forms.TextBox txtFrontCode;
            private System.Windows.Forms.TextBox textBackCode;
            private System.Windows.Forms.Button btnRestoreDefault;
        }
    }

     

    4、總結(jié)

            工程最終生成WintyCodeArea.dll,將其復(fù)制到"C:\Program Files\Windows Live\Writer\Plugins"目錄,啟動(dòng)Windows Live Writer就可以使用這個(gè)插件了?;?qū)⑵渥龀蒞intyCodeAreaWLWPluginSetup.msi(見附件),點(diǎn)安裝即可(msi制作方法參考[11])。

           此插件主要供自己使用,其他人可能不會(huì)想要我的這種效果,所以暫命名WintyCodeArea。但是除了添加<div class="mycode">your code</div>外,還可以進(jìn)行代碼轉(zhuǎn)義設(shè)置,并且前后綴代碼都可以自定義,根據(jù)需要自己添加前綴后綴代碼就行了,所以,希望對(duì)別人有點(diǎn)用處。

     

    補(bǔ)充:

          如果插件需要訪問剪貼板,可參考如下代碼:

    //System.Windows.Forms.Clipboard

    IDataObject iData = Clipboard.GetDataObject();
    if (iData.GetDataPresent(DataFormats.Text))
    {
        str = (String)iData.GetData(DataFormats.Text);
        ......
    }

     

    [參考資料]:

    [1]*《Developing Plugins for Windows Live Writer 》 : http://www.devx.com/codemag/Article/38214/1954

    [2]*《為Windows Live Writer開發(fā)插件——InsertSearchPageLink》 : http://www.cnblogs.com/dflying/archive/2006/12/03/580602.html

    [3]*《Writing a simple Windows Live Writer plugin》 : http://blog.boyet.com/blog/blog/writing-a-simple-windows-live-writer-plugin/

    [4] 《The New Live Writer SDK》: http://www.liveside.net/developer/archive/2008/06/03/the-new-live-writer-sdk.aspx

    [5] 《Writing Plugins For Windows Live Writer - Working With Forms》  : http://www.liveside.net/developer/archive/2006/10/19/Writing-Plugins-For-Windows-Live-Writer-_2D00_-Working-With-Forms.aspx

    [6] 《Writing Plugins For Windows Live Writer - Using PluginHttpRequest Instead of HttpWebRequest》 : http://www.liveside.net/developer/archive/2007/10/20/writing-plugins-for-windows-live-writer-using-pluginhttprequest-instead-of-httpwebrequest.aspx

    [7] 《Windows Live Writer Plugin - Properties Panel》 : http://blog.benhall.me.uk/2007/09/windows-live-writer-plugin-properties.html

    [8] 《Windows Live Writer Plugin - Display a form / OpenFileDialog》 :  http://blog.benhall.me.uk/2007/09/windows-live-writer-plugin-display-form.html

    [9] LiveSide Source Code : http://cid-fabdddc5cad93494.skydrive.live.com/self.aspx/LiveSide%20-%20Public/SourceCode/MyNewPlugin%20-%20Adding%20An%20Icon.zip?wa=wsignin1.0&sa=732043521

    [10] 《Windows Live Writer Plugin - Hello World!》 : http://blog.benhall.me.uk/2007/09/windows-live-writer-plugin-hello-world.html

    [11]*《Windows Live Writer Plugin - Installation》 : http://blog.benhall.me.uk/2007/10/windows-live-writer-plugin-installation.html

    [12] MSDN Windows Live Writer SDK : http://msdn.microsoft.com/en-us/library/aa738906.aspx

    [13] Windows Live Writer Blog : http://www.live-writer.net/

    [14] Windows Live Gallery : http://gallery.live.com/results.aspx?bt=9&pl=8


    [附件]:

    [1] WintyCodeAreaProject.zip(Visual Studio工程) : WintyCodeAreaProject.zip

    [2] WintyCodeAreaWLWPluginSetup.zip(WintyCodeArea插件安裝程序,msi格式) : http://www.tkk7.com/Files/wintys/WintyCodeAreaWLWPluginSetup.zip

    原創(chuàng)作品,轉(zhuǎn)載請(qǐng)注明出處。
    作者:Winty (wintys@gmail.com)
    博客:http://www.tkk7.com/wintys

     

    posted on 2009-10-05 19:26 天堂露珠 閱讀(2686) 評(píng)論(1)  編輯  收藏 所屬分類: .NET

    FeedBack:
    # re: [原]開發(fā)自己的Windows Live Writer插件 2013-04-02 10:03 天堂露珠
    引用1:《修改LiveWriter插件WintyCodeArea,生成google-code-prettify適用的代碼塊》: http://freewind.me/blog/20130101/1367.html
    上述引用修改后的代碼已由freewind上傳到Github: https://github.com/freewind/WintyCodeArea

    引用2:Windows Live Writer的code插件: http://tooktang.blogcn.com/2011/09/19/windows-live-writer%E7%9A%84code%E6%8F%92%E4%BB%B6/  回復(fù)  更多評(píng)論
      

    只有注冊用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲精品无码永久在线观看你懂的| 久久久久亚洲精品成人网小说| 四虎影视久久久免费观看| 亚洲综合在线另类色区奇米| 在线免费观看你懂的| 黄色网址大全免费| 亚洲电影免费在线观看| 日本黄色免费观看| 少妇人妻偷人精品免费视频| 亚洲AV无码专区亚洲AV桃| 亚洲AV日韩AV天堂久久| 免费v片在线观看无遮挡| 99re免费在线视频| 一级大黄美女免费播放| 国产午夜亚洲精品国产| 亚洲精品午夜无码电影网| 四虎影院免费视频| 最近免费中文字幕大全免费| 无遮挡a级毛片免费看| 亚洲婷婷综合色高清在线| 中文字幕亚洲无线码a| 成人看的午夜免费毛片| 桃子视频在线观看高清免费视频| 粉色视频在线观看www免费| 亚洲精品一区二区三区四区乱码 | 亚洲精品资源在线| 四虎亚洲国产成人久久精品| 国产精品免费观看久久| 久久99精品视免费看| 一级白嫩美女毛片免费| 亚洲欧美日韩中文高清www777| 色噜噜综合亚洲av中文无码| 精品亚洲成α人无码成α在线观看| 女人18毛片a级毛片免费| **aaaaa毛片免费同男同女| 久久精品免费一区二区三区| 精品熟女少妇aⅴ免费久久 | 精品视频一区二区三区免费| 日韩在线视频免费| 色屁屁www影院免费观看视频| 亚洲欧洲精品成人久久曰|