首页 技术 正文
技术 2022年11月17日
0 收藏 897 点赞 3,703 浏览 5805 个字

first neet add an user control "DownloadBar":

/*

  • Created by SharpDevelop.
  • User: gwang
  • Date: 2016/8/25
  • Time: 14:01
  • To change this template use Tools | Options | Coding | Edit Standard Headers.

    */

    using System;

    using System.ComponentModel;

    using System.Drawing;

    using System.Windows.Forms;

namespace testHttp

{

///

/// Description of DownloadBar.

///

public partial class DownloadBar : UserControl

{

public DownloadBar()

{

//

// The InitializeComponent() call is required for Windows Forms designer support.

//

InitializeComponent();

Anchor=AnchorStyles.Left|AnchorStyles.Right;

//Width=Parent.Width -40;

Height=65;

//
// TODO: Add constructor code after the InitializeComponent() call.
//
}public DownloadBar(DownLoadInfo info)
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
MinimumSize= new Size(126,66);
this.info=info;
btn1.Text=""+info.title +info.uid;
btn1.Visible=false;
lb1.Visible=true ;if (info.ProgressPercentage<100){lb1.Visible=true;
lb1.Text=info.ProgressPercentage+"%";}t = new Timer();
t.Tick+=t_tick;
t.Start ();
}protected override void OnLoad(EventArgs e)
{base.OnLoad(e);
//Anchor=AnchorStyles.Left|AnchorStyles.Right;
// this.Size = new System.Drawing.Size(274, 65);
this.Size = new System.Drawing.Size(Parent.Width-20, 65);
this.ResumeLayout(false);}Timer t;
DownLoadInfo info;void t_tick(object sender, EventArgs e){if(info.ProgressPercentage>=100 || info.isCompleted )
{
//lb1.Visible=false;
t.Stop();
btn1.Visible=true ;
lb1.Visible=false;
}lb1.Text=info.ProgressPercentage+"%";
lb1.Width=(int )( Width* ((double )info.ProgressPercentage * 0.01d));}
void Btn1Click(object sender, EventArgs e)
{
MessageBox.Show(info.title );
}}

}

———partial class for ui InitializeComponent————————

/*

  • Created by SharpDevelop.

  • User: gwang

  • Date: 2016/8/25

  • Time: 14:01

  • To change this template use Tools | Options | Coding | Edit Standard Headers.

    */

    namespace testHttp

    {

    partial class DownloadBar

    {

    ///

    /// Designer variable used to keep track of non-visual components.

    ///

    private System.ComponentModel.IContainer components = null;

    private System.Windows.Forms.Label lb1;

    private System.Windows.Forms.Button btn1;

     /// <summary>
    /// Disposes resources used by the control.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
    if (disposing) {
    if (components != null) {
    components.Dispose();
    }
    }
    base.Dispose(disposing);
    } /// <summary>
    /// This method is required for Windows Forms designer support.
    /// Do not change the method contents inside the source code editor. The Forms designer might
    /// not be able to load this method if it was changed manually.
    /// </summary>
    private void InitializeComponent()
    {
    this.lb1 = new System.Windows.Forms.Label();
    this.btn1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    //
    // lb1
    //
    this.lb1.BackColor = System.Drawing.Color.DodgerBlue;
    this.lb1.Dock = System.Windows.Forms.DockStyle.Left;
    this.lb1.Location = new System.Drawing.Point(0, 0);
    this.lb1.Name = "lb1";
    this.lb1.Size = new System.Drawing.Size(55, 65);
    this.lb1.TabIndex = 0;
    this.lb1.Text = "10%";
    this.lb1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
    //
    // btn1
    //
    this.btn1.Dock = System.Windows.Forms.DockStyle.Fill;
    this.btn1.Location = new System.Drawing.Point(0, 0);
    this.btn1.Name = "btn1";
    this.btn1.Size = new System.Drawing.Size(274, 65);
    this.btn1.TabIndex = 1;
    this.btn1.UseVisualStyleBackColor = true;
    this.btn1.Click += new System.EventHandler(this.Btn1Click);
    //
    // DownloadBar
    //
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.BackColor = System.Drawing.Color.LightSteelBlue;
    this.Controls.Add(this.lb1);
    this.Controls.Add(this.btn1);
    this.Name = "DownloadBar";
    this.Size = new System.Drawing.Size(274, 65);
    this.ResumeLayout(false); }

    }

    }


2 main ui form code behind "FrmDownload":

/*

  • Created by SharpDevelop.
  • User: gwang
  • Date: 2016/8/25
  • Time: 14:34
  • To change this template use Tools | Options | Coding | Edit Standard Headers.

    */

    using System;

    using System.Drawing;

    using System.Windows.Forms;

namespace testHttp

{

///

/// Description of FrmDownload.

///

public partial class FrmDownload : Form

{

public FrmDownload()

{

//

// The InitializeComponent() call is required for Windows Forms designer support.

//

InitializeComponent();

//
// TODO: Add constructor code after the InitializeComponent() call.
//
}int uid=1;void Button1Click(object sender, EventArgs e)
{
DownLoadInfo info = new DownLoadInfo();
info.url="http://sw.bos.baidu.com/sw-search-sp/av_aladdin/a997f8c5c22/rj_hb1174.exe";
info.title="test it "+ uid ;
info.uid=""+uid ;
HttpUtils.downloadFile(info);
uid++;this.flowLayoutPanel1.Controls.Add(new DownloadBar(info));
Text="there "+ this.flowLayoutPanel1.Controls.Count +" downloading.........";
}}

}

oh… first need define util class "HttpUtils"

/*

  • Created by SharpDevelop.

  • User: gwang

  • Date: 2016/8/24

  • Time: 17:23

  • To change this template use Tools | Options | Coding | Edit Standard Headers.

    */

    using System;

    using System.Net;

    using System.ComponentModel;

    using System.Collections;

    using System.Collections.Generic;

    using System.Linq;

    namespace testHttp

    {

    ///

    /// Description of HttpUtils.

    ///

    public class HttpUtils

    {

    public HttpUtils()

    {

     } public static List<DownLoadInfo> listDownloadInfo= new List<DownLoadInfo>(); public static void downloadFile(DownLoadInfo info){ if(listDownloadInfo.Count (n=> n.uid==info.uid )>0)
    { return;
    } listDownloadInfo.Add(info);
    WebClient wc=new WebClient();
    wc.DownloadProgressChanged+= wc_DownloadProgressChanged;
    wc.DownloadDataCompleted+=wc_DownloadDataCompleted; //wc.DownloadStringAsync(new Uri("https://jirafnd.dev.activenetwork.com/browse/HYTEK-274",UriKind.Absolute ),info);
    wc.DownloadDataAsync(new Uri(info.url ,UriKind.Absolute ),info); } private static void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e){ (e.UserState as DownLoadInfo ).ProgressPercentage= e.ProgressPercentage; } private static void wc_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e){ (e.UserState as DownLoadInfo ).isCompleted =( e.Error==null) ; }

    }

    // this for basic info class

    public class DownLoadInfo{

     public string title{get;set;}
    public string url{get;set;}
    public string uid{get;set;}
    public int ProgressPercentage{ get;set;}
    public bool isCompleted{get;set;}

    }

}

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,075
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,551
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,399
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,811
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,893