WindowsLiveWriter Plugin - 股市即時分析圖

摘要:WindowsLiveWriter Plugin - 股市即時分析圖

自從開始使用WindowsLiveWriter寫Blog時,就很想要自己寫一個WindowsLiveWriter的外掛程式(Plug-in),但是因為自己實在太懶惰的關係...一直沒有實際動手去做。直到最近看到有網友發表了一連串的WindowsLiveWriter外掛程式開發技術文章,心裡才想說自己也來試試看好了。想的很簡單...來做外掛程式,但...到底要做什麼外掛程式呢?此時突然想到自己平常都有在看股市的資料,就決定來寫一個股市即時分析圖的小工具好了。

基本上股市即時分析圖是取自於Yahoo!奇摩股市,當User輸入股票代號時,程式會到Yahoo!奇摩股市取得目標網頁內容,並使用Regular Expression(規則運算式),取得內容的圖片過濾出來,若規則運算式過濾正確則把圖片顯示在PictureBox中,若過濾失敗則顯示股票代碼不存在。最後將圖片暫存在使用者Temp目錄中,以便WindowsLiveWrtier上傳到WindowsLiveSpaces。

你可能會問為什麼要將圖片暫存在使用者Temp目錄,因為Yahoo!奇摩股市的資料是即時更新,如果我這用超鏈結這種方法<img src=http://chat.yahoo....jpg,那麼所顯示的資料會不準確。

【檔案下載】

【程式預覽圖】

APC Capture - 2008.04.18 12.06.32 - 001

APC Capture - 2008.04.18 12.07.10 - 002

APC Capture - 2008.04.18 12.07.57 - 003

【原始碼公開】

Class1.cs

using System;
using System.Collections.Generic;
using System.Text;
using WindowsLive.Writer.Api;
using System.Windows.Forms;

namespace LiveWriteTest
{
    [WriterPluginAttribute("887EC618-8FBE-49a5-A908-2339AF2EC721", "Innovation.NET Plugin",
        ImagePath = "Images.chart_curve.png",
        PublisherUrl = "http://innovationnet.sapces.live.com",
        Description = "Development for Innovation.NET")]

    [InsertableContentSourceAttribute("股市即時分析圖")]
    public class Plugin : ContentSource
    {
        public override DialogResult CreateContent(IWin32Window dialogOwner, ref string content)
        {
            using (Form_Main fmMain = new Form_Main())

            {
                DialogResult result = fmMain.ShowDialog();
                if (result == DialogResult.OK)
                {
                    content = fmMain.ResultContent;
                }

                return DialogResult.OK;
            }
            
        }

    }

}

 

Form_Main.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace LiveWriteTest
{
    public partial class Form_Main : Form
    {
        ArrayList aryLineType1 = new ArrayList(new object[] { "D", "W", "M" });
        ArrayList aryLineType2 = new ArrayList(new object[] { "V", "K", "M", "R", "B", "W", "N", "C" });
        string StockImagePath = String.Empty;
        string _ResultContent;

        /// <summary>
        /// 設定或取得WindowsLiveWrite的內容
        /// </summary>

        public string ResultContent
        {
            get
            {
                return _ResultContent;
            }

            set
            {
                _ResultContent=value;
            }

        }


        public Form_Main()
        {
            InitializeComponent();
        }


        private void btnStockInfo_Click(object sender, EventArgs e)
        {
            string queryUrl = String.Empty;
            string strRegularExpression = String.Empty;
            pictureBox1.ImageLocation = "";

            if (txtStockID.Text == String.Empty || txtStockID.Text.Length < 4)
            {
                MessageBox.Show("注意:股票代號輸入異常!!");
            }

            else
            {
                if (radioButton1.Checked)
                {
                    queryUrl = String.Format("http://tw.stock.yahoo.com/q/bc?s={0}", txtStockID.Text);

                    strRegularExpression = @"http://tw.chart.finance.yahoo.com[/]b[?]s=\d{4}";
                }


                if (radioButton2.Checked)
                {
                    queryUrl =
                        String.Format("http://tw.stock.yahoo.com/q/ta?s={0}&t={1}&a={2}",
                               new object[]{txtStockID.Text,
                                        aryLineType1[cbLineType1.SelectedIndex],
                                        aryLineType2[cbLineType2.SelectedIndex]}
);

                    strRegularExpression = @"http://tw.chart.yahoo.com[/][a-z][/][dwm][/](.*)[/]\d{4}.png";
                }


                DialogResult = DialogResult.OK;
                this.Close();
            }


                string strParseHtml = HtmlToText(queryUrl);

                if (strParseHtml != string.Empty)
                {
                    Match iMatch =
                        Regex.Match(strParseHtml, strRegularExpression, RegexOptions.Compiled | RegexOptions.IgnoreCase);

                    if (iMatch.Success)
                    {
                        pictureBox1.ImageLocation = iMatch.Value;
                    }

                    else
                    {
                        MessageBox.Show("查無此股票代號資料!");
                    }

                }

            }

        }


        /// <summary>
        /// 將網站頁面轉換為字串
        /// </summary>
        /// <param name="url">目標網址位址</param>
        /// <returns></returns>

        private string HtmlToText(string url)
        {
            try
            {
                System.IO.Stream stm =
                    System.Net.HttpWebRequest.Create(url).GetResponse().GetResponseStream();

                return new System.IO.StreamReader(stm, Encoding.GetEncoding("Big5")).ReadToEnd();
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return String.Empty;
            }
            
        }


        private void btnOK_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image != null)
            {
                //將圖片暫存於使用者TEMP目錄下
                string dirTemp = System.IO.Path.GetTempPath();

                using (Bitmap bmp = new Bitmap(pictureBox1.Image))

                {
                    bmp.Save(dirTemp + "887EC618-8FBE-49a5-A908-2339AF2EC721.png", System.Drawing.Imaging.ImageFormat.Png);
                }


                ResultContent = @"<P><img src='file:///" + dirTemp + "887EC618-8FBE-49a5-A908-2339AF2EC721.png" + "' alt='stock'/></P>";

        }


  
        private void Form_Main_Load(object sender, EventArgs e)
        {
            //設定圖型類別ComboBox內容
            cbLineType1.Items.AddRange(new object[] { "日線", "週線", "月線" });
            cbLineType2.Items.AddRange(new object[] { "K線", "KD", "MACD", "RSI", "乖離率", "威廉", "多空", "CDP" });
            cbLineType1.SelectedIndex = 0;
            cbLineType2.SelectedIndex = 0;

            cbLineType1.Enabled = false;
            cbLineType2.Enabled = false;
        }


        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton2.Checked)
            {
                cbLineType1.Enabled = true;
                cbLineType2.Enabled = true;
            }

            else
            {
                cbLineType1.Enabled = false;
                cbLineType2.Enabled = false;
            }

        }


        private void label2_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://innovationnet.spaces.live.com");
        }

    }

}