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

namespace 下载示例
{
    public delegate void form1ContentControl(bool isFinish);

    public partial class Form2 : Form
    {
        public Form2(string _sourceFile,string _destFile)
        {
            InitializeComponent();
            sourceFile = _sourceFile;
            destFile = _destFile;
        }

        public string sourceFile;
        public string destFile;

        public event form1ContentControl contentControl;

        private void Form2_Load(object sender, EventArgs e)
        {
            label1.Text = String.Format("[{0}]下载中...",Path.GetFileName(sourceFile));
            fileDownloading();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.Text == "取消")
            {
                this.Close();
            }
            else
            {
                contentControl(true);
                this.Close();
            }
        }

        private void fileDownloading()
        {
            try
            {
                WebClient client = new WebClient();
                //进行异变下载
                client.DownloadFileAsync(new Uri(sourceFile), destFile);
                //绑定进度改变时的事件
                client.DownloadProgressChanged += client_DownloadProgressChanged;
                //绑定完成时事件
                client.DownloadFileCompleted += client_DownloadFileCompleted;
            }
            catch (WebException we)
            {
                MessageBox.Show(we.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 事件:WEBCIENT下载进度改变时触发的回调事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            Action<DownloadProgressChangedEventArgs> onCompleted = progressChanging;
            onCompleted.Invoke(e);
        }

        /// <summary>
        /// 事件:WEBCLIENT下载完成时的回调事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            Action<AsyncCompletedEventArgs> onCompleted = progressCompleted;
            onCompleted.Invoke(e);
        }

        /// <summary>
        /// 文件下载进度改变时触发
        /// </summary>
        /// <param name="de"></param>
        private void progressChanging(DownloadProgressChangedEventArgs de)
        {
            progressBar1.Value = de.ProgressPercentage;
            linkLabel1.Text = String.Format("{0}%",de.ProgressPercentage);
            linkLabel2.Text = String.Format("{0}M/{1}M",Math.Round((double)de.BytesReceived/1024/1024,2),Math.Round((double)de.TotalBytesToReceive/1024/1024,2));
        }

        /// <summary>
        /// 当文件下载完成时触发
        /// </summary>
        /// <param name="de"></param>
        private void progressCompleted(AsyncCompletedEventArgs de)
        {
            label1.Text = String.Format("[{0}]下载已经完成!", Path.GetFileName(sourceFile));
            button1.Text = "完成";
        }
    }
}