个人制作美女图片下载器时的一些新的思想和方法总结
- 时间:2015年04月02日 15:33:59 来源:魔法猪系统重装大师官网 人气:15077
最近在做美女图片下载器的时候,尝试用一些新的思想和方法,感觉不错,总结一下。
一、【单例窗体】在WinForm应用程序中,当我们点击某个按钮之后,会显示一个窗体。如果用传统的new一个窗体然后再调用其Show()方法的话,当多次点击那个按钮是就会弹出多个窗体,但这往往不是我们想要的结果,我们想要的是,不论点击那个按钮多少次,总是显示一个窗体。这就是单例的设计思想。下面我通过使用反射来实现单例窗体对象。
如下是获取指定类型的单例窗体的方法:
1 ///2 /// 获取指定类型的单例窗体 3 /// 4 /// 窗体的类型 5 public static Form GetSingletonForm(Type type) 6 { 7 //遍历当前应用程序域中已打开的窗体 8 foreach (Form fm in Application.OpenForms) 9 { 10 //判断类型名称是否相同 11 if (fm.GetType().Name == type.Name) 12 { 13 //获取主显示器工作区域的宽度 14 int scWidth = Screen.PrimaryScreen.WorkingArea.Width; 15 //获取主显示器工作区域的高度 16 int scHeight = Screen.PrimaryScreen.WorkingArea.Height; 17 //获取要显示的窗体的宽度 18 int fmWidth = fm.Width; 19 //获取要显示的窗体的高度 20 int fmHeight = fm.Height; 21 //将要显示的窗体的位置设置为屏幕正中间 22 fm.Location = new Point((scWidth - fmWidth) / 2, (scHeight - fmHeight) / 2); 23 //返回已打开的窗体对象 24 return fm; 25 } 26 } 27 //如果要显示的窗体未被创建则通过反射将其创建 28 //根据传入的窗体的类型全名称获取其所在的程序集的引用 29 Assembly asm = Assembly.Load(type.Assembly.FullName); 30 //使用反射创建传入类型的窗体的实例 31 Form newForm = (Form)Activator.CreateInstance(type); 32 //设置新创建的窗体的初始位置为屏幕正中间 33 newForm.StartPosition = FormStartPosition.CenterScreen; 34 //返回新创建的窗体 35 return newForm; 36 }
我的程序里面有一个窗体,名为Options,我现在要点击一个按钮显示出它,下面是调用的实例:
1 Form fm = Utils.FormUtil.GetSingletonForm(typeof(Options)); 2 //显示Options窗体 3 fm.Show(); 4 //激活Options窗体 5 fm.Activate();
经过测试,无论点击多少次按钮,总是显示同一个窗体。
二、【带进度条的异步下载】假如我要下载30个图片,如果网络快的话还好,但如果网络慢的话,程序一定卡死。如果用异步下载的话,就算网络慢程序也不会卡死,因为下载的线程是异步的,不会阻塞主线程。
1 //创建一个WebClient用于下载图片 2 wc_Pic = new WebClient(); 3 //注册异步下载数据完成时的事件 4 wc_Pic.DownloadDataCompleted += new DownloadDataCompletedEventHandler(wc_DownloadDataCompleted); 5 //注册异步下载进度改变时的事件 6 wc_Pic.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_Pic_DownloadProgressChanged); 7 //执行下载 8 Uri uri = new Uri(string.Format(url + txtFileExt.Text.Trim(), 1)); 9 wc_Pic.DownloadDataAsync(uri); 10 11 ///12 /// 下载完成时触发的事件 13 /// 14 void wc_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e) 15 { 16 if (e.Cancelled) 17 {} 18 else if (e.Error != null) 19 { 20 MessageBox.Show("下载出错!原因如下:" + e.Error.Message , "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 21 return; 22 } 23 else 24 { 25 if (e.Result != null) 26 { 27 using (System.IO.MemoryStream ms = new System.IO.MemoryStream(e.Result)) 28 { 29 if (ms != null) 30 { 31 if (!Directory.Exists(txtSavePath.Text.Trim())) 32 { 33 Directory.CreateDirectory(txtSavePath.Text.Trim()); 34 } 35 36 Image img = Image.FromStream(ms); 37 38 string filePath = txtSavePath.Text.Trim() + @"\" + chr + "-" + index + ".jpg"; 39 try 40 { 41 using (FileStream fs = new FileStream(filePath, FileMode.Create)) 42 { 43 fs.Write(ms.ToArray(), 0, ms.ToArray().Length); 44 } 45 } 46 catch (Exception ex) 47 { 48 MessageBox.Show("保存文件:" + filePath + " 失败,原因如下:" + ex.Message); 49 } 50 } 51 } 52 } 53 } 54 } 55 56 ///57 /// 下载进度改变触发的事件 58 /// 59 void wc_Pic_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) 60 { 61 //设置ProgressBar的最大值 62 pgb.Maximum = end; 63 //设置ProgressBar当前值 64 pgb.Value = begin; 65 }





