jinyuttt

导航

实现文件下载c++/cli

添加net空间,在窗体中添加2个textbox,4个标签label。2个按钮button。设置属性。源码如下:

namespace 下载 {

 using namespace System;
 using namespace System::ComponentModel;
 using namespace System::Collections;
 using namespace System::Windows::Forms;
 using namespace System::Data;
 using namespace System::Drawing;
 using namespace System::Net;
 /// <summary>
 /// Form1 摘要
 /// </summary>
 public ref class Form1 : public System::Windows::Forms::Form
 {
 public:
  Form1(void)
  {  
   InitializeComponent();
   this->wc=gcnew System::Net::WebClient();
      //this->textBox1->Text="http://ftp.pconline.com.cn/pub/download/201007/QQ2010.zip";//测试地址
   //
   //TODO: 在此处添加构造函数代码
   //
  }

 protected:
  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  ~Form1()
  {
   if (components)
   {
    delete components;
   }
  }
 private: System::Windows::Forms::Button^  button1;
 protected:
 private: System::Windows::Forms::Button^  button2;
 private: System::Windows::Forms::TextBox^  textBox1;
 private: System::Windows::Forms::Label^  label1;
 private: System::Windows::Forms::Label^  label2;
 private: System::Windows::Forms::Label^  label3;
 private: System::Windows::Forms::Label^  label4;
 private: System::Windows::Forms::ProgressBar^  progressBar1;
 private: System::Net::WebClient ^wc;
 private: System::Windows::Forms::TextBox^  textBox2;
 private: System::Windows::Forms::Label^  label5;
 private:
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
  /// <summary>
  /// 设计器支持所需的方法 - 不要
  /// 使用代码编辑器修改此方法的内容。
  /// </summary>
  void InitializeComponent(void)
  {
   this->button1 = (gcnew System::Windows::Forms::Button());
   this->button2 = (gcnew System::Windows::Forms::Button());
   this->textBox1 = (gcnew System::Windows::Forms::TextBox());
   this->label1 = (gcnew System::Windows::Forms::Label());
   this->label2 = (gcnew System::Windows::Forms::Label());
   this->label3 = (gcnew System::Windows::Forms::Label());
   this->label4 = (gcnew System::Windows::Forms::Label());
   this->progressBar1 = (gcnew System::Windows::Forms::ProgressBar());
   this->textBox2 = (gcnew System::Windows::Forms::TextBox());
   this->label5 = (gcnew System::Windows::Forms::Label());
   this->SuspendLayout();
   //
   // button1
   //
   this->button1->Location = System::Drawing::Point(27, 160);
   this->button1->Name = L"button1";
   this->button1->Size = System::Drawing::Size(75, 23);
   this->button1->TabIndex = 0;
   this->button1->Text = L"下载";
   this->button1->UseVisualStyleBackColor = true;
   this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
   //
   // button2
   //
   this->button2->Location = System::Drawing::Point(229, 160);
   this->button2->Name = L"button2";
   this->button2->Size = System::Drawing::Size(75, 23);
   this->button2->TabIndex = 1;
   this->button2->Text = L"取消";
   this->button2->UseVisualStyleBackColor = true;
   //
   // textBox1
   //
   this->textBox1->Location = System::Drawing::Point(70, 23);
   this->textBox1->Name = L"textBox1";
   this->textBox1->Size = System::Drawing::Size(333, 21);
   this->textBox1->TabIndex = 2;
   //
   // label1
   //
   this->label1->AutoSize = true;
   this->label1->Location = System::Drawing::Point(23, 26);
   this->label1->Name = L"label1";
   this->label1->Size = System::Drawing::Size(23, 12);
   this->label1->TabIndex = 4;
   this->label1->Text = L"URI";
   //
   // label2
   //
   this->label2->AutoSize = true;
   this->label2->Location = System::Drawing::Point(12, 95);
   this->label2->Name = L"label2";
   this->label2->Size = System::Drawing::Size(29, 12);
   this->label2->TabIndex = 5;
   this->label2->Text = L"进度";
   //
   // label3
   //
   this->label3->AutoSize = true;
   this->label3->Location = System::Drawing::Point(406, 95);
   this->label3->Name = L"label3";
   this->label3->Size = System::Drawing::Size(41, 12);
   this->label3->TabIndex = 6;
   this->label3->Text = L"label3";
   //
   // label4
   //
   this->label4->AutoSize = true;
   this->label4->Location = System::Drawing::Point(77, 125);
   this->label4->Name = L"label4";
   this->label4->Size = System::Drawing::Size(41, 12);
   this->label4->TabIndex = 7;
   this->label4->Text = L"label4";
   //
   // progressBar1
   //
   this->progressBar1->Location = System::Drawing::Point(82, 84);
   this->progressBar1->Name = L"progressBar1";
   this->progressBar1->Size = System::Drawing::Size(294, 23);
   this->progressBar1->TabIndex = 8;
   //
   // textBox2
   //
   this->textBox2->Location = System::Drawing::Point(70, 55);
   this->textBox2->Name = L"textBox2";
   this->textBox2->Size = System::Drawing::Size(333, 21);
   this->textBox2->TabIndex = 9;
   //
   // label5
   //
   this->label5->AutoSize = true;
   this->label5->Location = System::Drawing::Point(12, 58);
   this->label5->Name = L"label5";
   this->label5->Size = System::Drawing::Size(53, 12);
   this->label5->TabIndex = 10;
   this->label5->Text = L"文件名称";
   //
   // Form1
   //
   this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
   this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
   this->ClientSize = System::Drawing::Size(459, 220);
   this->Controls->Add(this->label5);
   this->Controls->Add(this->textBox2);
   this->Controls->Add(this->progressBar1);
   this->Controls->Add(this->label4);
   this->Controls->Add(this->label3);
   this->Controls->Add(this->label2);
   this->Controls->Add(this->label1);
   this->Controls->Add(this->textBox1);
   this->Controls->Add(this->button2);
   this->Controls->Add(this->button1);
   this->Name = L"Form1";
   this->Text = L"Form1";
   this->ResumeLayout(false);
   this->PerformLayout();

  }
#pragma endregion
 private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
                      if (wc->IsBusy)
                      {
        wc->CancelAsync();

                      }
       else
       { this->wc->DownloadProgressChanged += gcnew DownloadProgressChangedEventHandler(this,&Form1::wc_WebClient_DownloadProgressChanged);

            this->wc->DownloadFileCompleted +=gcnew AsyncCompletedEventHandler(this,&Form1::wc_WebClient_OnDownloadFileCompleted);
      if (this->textBox1->Text!="")
      {    String ^s=this->textBox1->Text;
            String ^k=s->Remove(0,s->LastIndexOf('/')+1);
         this->textBox2->Text=k;

  this-> wc->DownloadFileAsync(gcnew Uri(this->textBox1->Text),k);
      }
      else
       MessageBox::Show("请输入下载地址");
          }

     }
 private:void wc_WebClient_DownloadProgressChanged(Object ^sender,DownloadProgressChangedEventArgs ^e)
   {

    this->progressBar1->Value=e->ProgressPercentage;
    this->label3->Text=e->ProgressPercentage.ToString()+"%";
    
    this->label4->Text=String::Format("正在下载文件,当前下载{0}/{1}(字节)",e->BytesReceived,e->TotalBytesToReceive);
   }
   

   
private: void wc_WebClient_OnDownloadFileCompleted(Object ^sender, AsyncCompletedEventArgs ^e)
   {
    
   
    MessageBox::Show("下载完成");
    
   }

private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
    wc->CancelAsync();

   }
};
}

最主要的是事件注册。其他就是对webclient类的了解和应用。

初学者参考,这里不能上传源码。有值得看的源码全部在csdn了。用户名jinyuttt

posted on 2010-07-12 10:18  代码苦行僧  阅读(480)  评论(0编辑  收藏  举报