夏日部落客BloggerAds《2008‧夏》

顯示具有 程式文 標籤的文章。 顯示所有文章
顯示具有 程式文 標籤的文章。 顯示所有文章

2008年6月16日 星期一

Microsoft .Net Framework 3.5 poster

0 意見

image

微軟 .Net Framework 3.5 的海報,不過這麼小的圖實在不夠爽。以下是載點,此外問一下,這樣的海報有誰知道要怎樣索取,想掛在公司感覺還不錯。

下載點: 可選擇不同格式下載.

2008年4月17日 星期四

C# 複製檔案進度列實作 (Copy file + Progress Bar)

0 意見

應用程式常常會有搬移檔案的動作,但複製檔案時若沒有處裡,很容易造成使用者介面沒回應的畫面,此外若涉及複製大檔案,通常需要顯示進度列,便於使用者知道現在的複製進度,是要繼續下去或者是去取消,這一類的問題,有幾個要注意的問題(1)UI的同步更新問題,(2)非同步複製檔案的實作,我相信這問題已經是老掉牙的問題,但是我在網路上找到的例子大都是這一個使用C#在进度条中显示复制文件的进度 (大部分都是貼這個範例),我大概看過且稍為用了一下(我的開發平台是VS2008),發現有一個問題是他在更新ProgressBar的時候,會有InvalidOperationException[Cross-Thread operation not vaild... 的問題產生,這問題可以上Google稍微查一下就發現問題所在,所以我稍微改寫了一下,畫面及程式碼如下:

Copy

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

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        int totalSize;  //Total Size
        int position;   //Position
        const int BUFFER_SIZE = 4096;
        byte[] buffer;
        FileStream fileWriteStream;
        FileStream fileReadStream;
        public Form1()
        {
            InitializeComponent();
        }

        private void ctrlCopy_Click(object sender, EventArgs e)
        {
            string strFile = "";
            position = 0;
            totalSize = 0;
            OpenFileDialog openDialog = new OpenFileDialog();
            if (openDialog.ShowDialog() == DialogResult.OK)
                strFile = openDialog.FileName;
            else
                return;

            // Reset Status in order to prevent another copying process
            this.totalSize = 0;
            this.position = 0;
            if (this.buffer != null) this.buffer = null;
            if (this.fileWriteStream != null) this.fileWriteStream.Close();
            if (this.fileReadStream != null) this.fileReadStream.Close();

            //Delete file which aready exists.
            if (System.IO.File.Exists("c:\\copyedFile.bin"))
                System.IO.File.Delete("c:\\copyedFile.bin");

            this.fileReadStream = new FileStream(strFile, FileMode.Open, FileAccess.Read);
            this.fileWriteStream = new FileStream("C:\\copyedFile.bin", FileMode.Append, FileAccess.Write);
            totalSize = (int)fileReadStream.Length;
            this.progressBar1.Maximum = totalSize;

            //Copy file while larger than 4KB.
            if (totalSize > BUFFER_SIZE)
            {
                buffer = new byte[BUFFER_SIZE];

                // Async Invoke
                fileReadStream.BeginRead(buffer, 0, BUFFER_SIZE, new AsyncCallback(AsyncCopyFile), null);
            }
            else
            {
                this.fileReadStream.Close();
            }
        }
        // Asynchronously copy file
        private void AsyncCopyFile(IAsyncResult ar)
        {
            int readedLength;

            // Lock FileStream
            lock (this.fileReadStream)
            {
                readedLength = this.fileReadStream.EndRead(ar);   // When stream endread, get readed length
            }

            // Write to disk
            this.fileWriteStream.Write(buffer, 0, buffer.Length);

            // Current stream position
            position += readedLength;

            // Response UI
            MethodInvoker m = new MethodInvoker(SynchProgressBar);
            if (this.progressBar1.InvokeRequired)
            {
                this.progressBar1.Invoke(m);
            }

            if (position >= totalSize) // Read over.
            {
                this.fileWriteStream.Close();        //Close FileStream 
                this.fileReadStream.Close();
                return;
            }

            // Continue to read and write
            lock (this.fileReadStream)
            {
                int leftSize = totalSize - position;

                if (leftSize < BUFFER_SIZE)
                    buffer = new byte[leftSize];

                this.fileReadStream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(AsyncCopyFile), null);
            }
        }

        private void SynchProgressBar()
        {
            this.progressBar1.Value = position;
        }
    }
}

不過細看有一些細節的部份要處裡,如檔案小於4KB時要如何處理,若要將此方法包成物件,要如何用delegate or event傳給上層UI... 這些問題就交給各位看官嚕。 此外有些觀念也是很重要的,在此附上連結: http://www.cnblogs.com/c2303191/articles/826571.html

2008年4月14日 星期一

HtmlTextWriter 實作簡介

0 意見

HtmlTextWriter使用動機

最近剛好在研究html由程式產生,發現在.net framework下有一個實用簡單的類別-HtmlTextWriter,藉由這個類別可以快速的由程式中寫出Html碼,命名空間是 System.Web.UI,如果在WinForm下使用,請引用(Reference)System.Web.dll。

HtmlTextWriter與Html32TextWriter
HtmlTextWriter支援Html4.0標準,而Html32TextWriter支援Html3.2標準,在msdn中一般不建議將採用Html32TextWriter,它為支援低版本的瀏覽器而設計的,所以建議採用HtmlTextWriter物件,由名稱上也可猜測這類別比較正統,沒有多餘的贅字。

 

TextWriter - StringWriter, StreamWriter 簡介
在介紹HtmlTextWriter使用前,必須先介紹一些相關的類別,因為HtmlTextWriter本身並不支援輸出的功能,必須由TextWriter的類別來實現輸出的功能。

TextWriter: 表示可以寫入一連串連續字元的寫入器,這個類別是抽象的。

StringWriter: 實作TextWriter以便將資訊寫入字串。資訊儲存在基礎 StringBuilder 中。

StreamWriter: 實作以特定的編碼方式將字元寫入位元組資料流的TextWriter。 

HtmlTextWriter初始化

HtmlTextWriter在建構時必須要接串接TextWriter,本身不提供輸出的方法或屬性。

建構子: public HtmlTextWriter (TextWriter writer)

輸出成檔案

System.IO.StreamWriter streamWriter = new System.IO.StreamWriter("C:\\htmlSample.htm");
System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(streamWriter);

streamWriter.toString();

streamWriter.Close();

在C:下可看到htmlSample.htm檔案

輸出成字串:

StringWriter strWriter=new System.IO.StringWriter();
HtmlTextWriter htmlWriter=new HtmlTextWriter(strWriter);

獲得輸出結果 string result =  strWriter.toString();

HtmlTextWriter常用方法

1. void RenderBeginTag(HtmlTextWriterTag tagKey)
    用於Html開始標籤,如<body>,HtmlTextWriterTag是列舉類別,內部有所有Html 4.0的列舉標籤,如果要增加body標籤,就是RenderBeginTag(HtmlTextWriterTag.Body)

2. void RenderEndTag() 
    與RenderBeginTag對應,有幾個RenderBeginTag就必須有幾個RenderEndTag。如下面的例子:
<html>
    <head></head>
    <body></body>
</html>
上面一段是html基本架構,使用HtmlTextWriter生成就是
   htmlWriter.RenderBeginTag(HtmlTextWriterTag.Html);
   htmlWriter.RenderBeginTag(HtmlTextWriterTag.Head);
   htmlWriter.RenderEndTag();   //End of Head
   htmlWriter.RenderBeginTag(HtmlTextWriterTag.Body);
   htmlWriter.RenderEndTag();  //End of Body
   htmlWriter.RenderEndTag();  //End of Html
3. void AddAttribute() 
   用於增加標籤的屬性,例如<img>標籤的url屬性、width屬性等。注意:AddAttribute必須出現在RenderBeginTag的前面

ex:
   htmlWriter.AddAttribute("url","../xxx.gif");
   htmlWriter.AddAttribute("width","50");
   htmlWriter.AddAttribute("height","50");
   htmlWriter.RenderBeginTag(HtmlTextWriterTag.Img);
   htmlWriter.RenderEndTag();

輸出: <img url="../xxx.gif" width="50" height="50" />。

4.void AddStyleAttribute()

    由於Html並不是很嚴謹的語言,所以上面的函式就可實作所須輸出的字串,但建議還是採用以下範例,以下3個例子,所產生的字串都相同,也能正確輸出


(a)
htmlWriter.AddAttribute("style", "margin:0px; color: #ff0; overflow: auto");
(b)
htmlWriter.AddStyleAttribute("margin", "0px");
htmlWriter.AddStyleAttribute("color", "#ff0");
htmlWriter.AddStyleAttribute("overflow", "auto");
(c)
htmlWriter.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.Margin, "0px");
htmlWriter.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.Color, ""#ff0");
htmlWriter.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.Overflow, "auto");
輸出: Style="margin:0px; color: #ff0; overflow: auto"
我採用的是第三種方法,是因為它有提供對應的列舉,不想全部手動自己寫。

5. void Write()和void WriteLine() 
   產生Html標籤以外的所有信息。

此外,若是從WinForm上的顏色轉換到網頁上的顏色,可採用此函式System.Drawing.ColorTranslator.ToHtml(Color c);

實作 Web 跑馬燈

 image

private void SaveMessageToHtml1(string htmPath)
{
    System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(htmPath);
    System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(streamWriter);

    // Html
    htmlWriter.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Html);

    // Head
    htmlWriter.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Head);
    htmlWriter.RenderEndTag(); // End of Head

    // Meta
    htmlWriter.AddAttribute("http-equiv", "content-type");
    htmlWriter.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Content, "text/html; charset=UTF-8");
    htmlWriter.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Meta);
    htmlWriter.RenderEndTag(); // End of Meta

    // Body
    htmlWriter.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.FontFamily, "Arial");
    htmlWriter.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.Margin, "0px");
    htmlWriter.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.Color, System.Drawing.ColorTranslator.ToHtml(Color.Yellow));
    htmlWriter.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.BackgroundColor, System.Drawing.ColorTranslator.ToHtml(Color.Black));
    htmlWriter.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.Overflow, "auto");
    htmlWriter.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Body);

    // Marquee
    htmlWriter.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.Position, "relative");

    htmlWriter.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.Top, "25%");
    htmlWriter.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.Height, "100%");
    htmlWriter.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.FontSize, string.Format("{0}px", "20"));
    htmlWriter.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.Overflow, "hidden");
    htmlWriter.AddAttribute("behavior", "scroll");
    htmlWriter.AddAttribute("direction", "left");

    htmlWriter.AddAttribute("loop", "-1");
    htmlWriter.AddAttribute("scrollamount", this.scrollOffset.Text);
    htmlWriter.AddAttribute("scrolldelay", this.scrollDelay.Text);
    htmlWriter.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Marquee);
    htmlWriter.Write("Hello! This is Web Brower-Marquee");

    htmlWriter.WriteLine();
    htmlWriter.RenderEndTag(); // End of Marquee
    htmlWriter.RenderEndTag(); // End of Body
    htmlWriter.RenderEndTag(); // End of Html

    streamWriter.ToString();
    streamWriter.Close();
    htmlWriter.Close();
    streamWriter.Dispose();
    htmlWriter.Dispose();

    // 除錯用
    System.Diagnostics.Process.Start("IEXPLORE.EXE", htmPath);
}

Reference:

http://www.cnblogs.com/tonyqus/archive/2005/02/15/104576.html

MSDN