⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cs

📁 一个使用.NET Framework开发的、免费的、开发源码的下载管理程序.
💻 CS
字号:
/************************************************************

UnrealDownload version 0.3
by Joe Hardy (yoda@pconsulting.com.au), 2000

Project home page:
http://www.pconsulting.com.au/unrealdownload/

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*************************************************************

Main.cs

The entry point for execution of UnrealDownload, which
creates the main form.

Revision history:
	November 14, 2000, by Scott Glasgow
		Changed the event handlers to work under .NET Beta 1
	November 17, 2000
		Changed Resource Manager from obsolete method (in beta 2) to non obsolete.
		Resources are now compiled into executable
	November 17, 2000, by Joe Hardy
		- All code was split into modules from downloader.cs (now obsolete)
	November 21, 2000 by Joe Hardy
		- Moved toolbar images into the resource file (have a look at the
		  new writeresources utility in the resources directory). I'll
		  probably change this toolbar to something with more elegant
		  behaviour than a bunch of picture boxes soon.
		- Directory structure changes
	November 28, 2000 by Joe Hardy
		- Added preferences menu item
		- Fixed bug where app would crash if the resume button was pressed when
		  there were no active jobs listed
	Late June - 6 July, 2000 by Joe Hardy
		- Cut all the code across to work with beta 2

************************************************************/

using System;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
using System.Threading;
using System.Collections;
using System.Resources;
using System.Reflection;

public class MainForm : Form {
	TextBox urlBox = new TextBox();
	Label urlBoxDesc = new Label();
	Button cmdGo = new Button();
	ContextMenu lvFilesContextMenu = new ContextMenu();
	StatusBar sbStatus = new StatusBar();

	MainMenu mainMenu = new MainMenu();
	MenuItem[] mainMenuItems = new MenuItem[] { new MenuItem("&File"), new MenuItem("&Help") };
	MenuItem[] fileMenuItems = new MenuItem[] { new MenuItem("&New Job"), new MenuItem("-"), new MenuItem("&Preferences"), new MenuItem("-"), new MenuItem("E&xit") };
	MenuItem[] helpMenuItems = new MenuItem[] { new MenuItem("&Help"), new MenuItem("-"), new MenuItem("&About") };
	
	PictureBox pbToolbar = new PictureBox();
	PictureBox pbAddFile = new PictureBox();
	PictureBox pbResume = new PictureBox();
	PictureBox pbStop = new PictureBox();
	PictureBox pbPause = new PictureBox();

	Image imgAddFile;
	Image imgAddFileHover;
	Image imgAddFileDown;

	Image imgResume;
	Image imgResumeHover;

	Image imgStop;
	Image imgStopHover;

	Image imgPause;
	Image imgPauseHover;

	public static ListView lvFiles = new ListView();

	ResourceManager rm = new ResourceManager("downloader",Assembly.GetExecutingAssembly());

	ImageList il = new ImageList();

	public static int Main() {
		Application.Run(new MainForm());
		return 0;
	}
	public MainForm() {
		// form settings
		this.Text = "UnrealDownload 0.3";
		this.Size = new Size(640, 450);
		this.Font = new Font(new FontFamily("Tahoma"), 8);
		this.Resize += new EventHandler(this.Form_Resize);

		Menu.MenuItemCollection menuItems = lvFilesContextMenu.MenuItems;
		menuItems.Add("&Resume", new EventHandler(menuResume_Click));
		menuItems.Add("&Stop", new EventHandler(menuStop_Click));
		menuItems.Add("&Pause", new EventHandler(menuPause_Click));
		menuItems.Add("-");
		menuItems.Add("&Delete", new EventHandler(menuDelete_Click));

		menuItems[1].Visible=false;

		lvFilesContextMenu.Popup += new EventHandler(lvFilesContextMenu_Popup);

		il.Images.Add((Image)rm.GetObject("Img_Downloading"), Color.White);
		il.Images.Add((Image)rm.GetObject("Img_Paused"), Color.White);

		sbStatus.ShowPanels = true;
		sbStatus.Dock = DockStyle.Bottom;
		sbStatus.Panels.Add(new StatusBarPanel());
		sbStatus.Panels[0].AutoSize = StatusBarPanelAutoSize.Spring;
		sbStatus.Panels[0].Text = "PLEASE NOTE THAT THIS IS ALPHA SOFTWARE - Bug reports, flames, suggestions etc to yoda@pconsulting.com.au";

		lvFiles.Location = new Point(0, 47);
		lvFiles.View = View.Details;
		lvFiles.FullRowSelect = true;
		lvFiles.HeaderStyle = ColumnHeaderStyle.Nonclickable;
		lvFiles.Columns.Add("Local File Name", 100, HorizontalAlignment.Left);
		lvFiles.Columns.Add("URL", 420, HorizontalAlignment.Left);
		lvFiles.ContextMenu = lvFilesContextMenu;
		lvFiles.SmallImageList=il;

		pbToolbar.BackgroundImage = (Image)rm.GetObject("tb_background");
		pbToolbar.Dock = DockStyle.Top;
		pbToolbar.Size = new Size(0, 47);

		imgAddFile = (Image)rm.GetObject("tb_addfile");
		imgAddFileHover = (Image)rm.GetObject("tb_addfile_hover");
		imgAddFileDown = (Image)rm.GetObject("tb_addfile_down");

		imgResume = (Image)rm.GetObject("tb_resume");
		imgResumeHover = (Image)rm.GetObject("tb_resume_hover");

		imgPause = (Image)rm.GetObject("tb_pause");
		imgPauseHover = (Image)rm.GetObject("tb_pause_hover");

		imgStop = (Image)rm.GetObject("tb_stop");
		imgStopHover = (Image)rm.GetObject("tb_stop_hover");

		pbAddFile.Image = imgAddFile;
		pbResume.Image = imgResume;
		pbStop.Image = imgStop;
		pbPause.Image = imgPause;

		pbAddFile.Location = new Point(8, 2);
		pbResume.Location = new Point(73, 2);
		pbStop.Location = new Point(135, 2);
		pbPause.Location = new Point(190, 2);

		pbAddFile.Size = new Size(51, 45);
		pbResume.Size = new Size(51, 45);
		pbStop.Size = new Size(51, 45);
		pbPause.Size = new Size(51, 45);

		pbAddFile.MouseEnter += new EventHandler(this.pbAddFile_MouseEnter);
		pbAddFile.MouseLeave += new EventHandler(this.pbAddFile_MouseLeave);
		pbAddFile.Click += new EventHandler(this.newJob_Click);
		pbAddFile.MouseDown += new MouseEventHandler(this.pbAddFile_MouseDown);
		pbAddFile.MouseUp += new MouseEventHandler(this.pbAddFile_MouseUp);

		pbResume.MouseEnter += new EventHandler(this.pbResume_MouseEnter);
		pbResume.MouseLeave += new EventHandler(this.pbResume_MouseLeave);
		pbResume.Click += new EventHandler(this.menuResume_Click);

		pbStop.MouseEnter += new EventHandler(this.pbStop_MouseEnter);
		pbStop.MouseLeave += new EventHandler(this.pbStop_MouseLeave);

		pbPause.MouseEnter += new EventHandler(this.pbPause_MouseEnter);
		pbPause.MouseLeave += new EventHandler(this.pbPause_MouseLeave);

		mainMenu.MenuItems.Add("&File", fileMenuItems);
		mainMenu.MenuItems.Add("&Help", helpMenuItems);

		fileMenuItems[0].Shortcut = Shortcut.CtrlN;
		helpMenuItems[0].Shortcut = Shortcut.F1;

		fileMenuItems[0].Click += new EventHandler(this.newJob_Click);
		fileMenuItems[2].Click += new EventHandler(this.menuFilePreferences_Click);
		fileMenuItems[4].Click += new EventHandler(this.CloseMe);
		helpMenuItems[2].Click += new EventHandler(this.helpAbout_Click);

		this.Menu = mainMenu;

		LoadJobs(true);

		this.Controls.Add(lvFiles);
		this.Controls.Add(pbToolbar);
		this.Controls.Add(sbStatus);
		pbToolbar.Controls.Add(pbPause);
		pbToolbar.Controls.Add(pbStop);
		pbToolbar.Controls.Add(pbResume);
		pbToolbar.Controls.Add(pbAddFile);
	}

	private void Form_Resize(Object Src, EventArgs e) {
		lvFiles.Size = new Size(this.Width-8, this.Height-pbToolbar.Height-sbStatus.Height-46);
	}

	private void menuFilePreferences_Click(Object Src, EventArgs e) {
		(new PrefsForm()).ShowDialog(this);
	}

	private void helpAbout_Click(Object Src, EventArgs e) {
		MessageBox.Show("UnrealDownload Version 0.3\nCreated by Joe Hardy, 2000", "About ...", MessageBoxButtons.OK, MessageBoxIcon.Information);
	}

	private void pbAddFile_MouseEnter(Object Src, EventArgs e) {
		pbAddFile.Image = imgAddFileHover;
	}

	private void pbAddFile_MouseLeave(Object Src, EventArgs e) {
		pbAddFile.Image = imgAddFile;
	}

	private void pbAddFile_MouseDown(Object Src, MouseEventArgs e) {
		pbAddFile.Image = imgAddFileDown;
	}

	private void pbAddFile_MouseUp(Object Src, MouseEventArgs e) {
		pbAddFile.Image = imgAddFile;
	}

	private void pbResume_MouseEnter(Object Src, EventArgs e) {
		pbResume.Image = imgResumeHover;
	}

	private void pbResume_MouseLeave(Object Src, EventArgs e) {
		pbResume.Image = imgResume;
	}

	private void pbStop_MouseEnter(Object Src, EventArgs e) {
		pbStop.Image = imgStopHover;
	}

	private void pbStop_MouseLeave(Object Src, EventArgs e) {
		pbStop.Image = imgStop;
	}

	private void pbPause_MouseEnter(Object Src, EventArgs e) {
		pbPause.Image = imgPauseHover;
	}

	private void pbPause_MouseLeave(Object Src, EventArgs e) {
		pbPause.Image = imgPause;
	}

	private void newJob_Click(Object Src, EventArgs e) {
		NewJobForm dlg = new NewJobForm();
		dlg.ShowDialog(this);
		return;
	}

	private void menuStop_Click(Object Src, EventArgs e) {
		MessageBox.Show("Needs implementing ... wanna help? :)");
		return;
	}

	private void lvFilesContextMenu_Popup(Object Src, EventArgs e) {
		if (lvFiles.SelectedItems.Count > 0) {
			lvFilesContextMenu.MenuItems[0].Enabled=lvFilesContextMenu.MenuItems[1].Enabled=lvFilesContextMenu.MenuItems[2].Enabled=lvFilesContextMenu.MenuItems[4].Enabled=true;
			lvFilesContextMenu.MenuItems[1].Visible=(lvFiles.SelectedItems[0].ImageIndex==0);
			lvFilesContextMenu.MenuItems[0].Visible=!lvFilesContextMenu.MenuItems[1].Visible;
		}
		else
			lvFilesContextMenu.MenuItems[0].Enabled=lvFilesContextMenu.MenuItems[1].Enabled=lvFilesContextMenu.MenuItems[2].Enabled=lvFilesContextMenu.MenuItems[4].Enabled=false;
	}

	private void menuResume_Click(Object Src, EventArgs e) {
		if (lvFiles.SelectedItems.Count > 0) {
			DownloadForm dlf = new DownloadForm(JobManagement.GetJob(lvFiles.SelectedItems[0].SubItems[1].Text), false);
			Thread thread = new Thread(new ThreadStart(dlf.StartDownload));
			thread.Start();
			lvFiles.SelectedItems[0].ImageIndex=0;
		}
		return;
	}

	private void menuPause_Click(Object Src, EventArgs e) {
	}

	private void menuDelete_Click(Object Src, EventArgs e) {
		JobData jobData = JobManagement.GetJob(lvFiles.SelectedItems[0].SubItems[1].Text);
		DialogResult result = MessageBox.Show("Do you want to delete the data downloaded for this job as well?", "Delete Job?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);

		if (result==DialogResult.Yes) {
			if (File.Exists(jobData.localPath + "\\" + jobData.localName)) File.Delete(jobData.localPath + "\\" + jobData.localName);
		}
		else if (result==DialogResult.Cancel)
			return;

		JobManagement.DeleteJob(jobData.url);
		LoadJobs(false);
	}

	// loads all current jobs into lvFiles. startInterrupted will check whether jobs are marked as
	// downloading and if so, will start the download. This will probably only be used on the MainForm
	// constructor (for when, for example, a system crashes while a download is in progress)
	public static void LoadJobs(bool startInterrupted) {
		int i=0;
		ArrayList al = JobManagement.GetAllJobs();

		JobData jobData;

		// clear the listview
		lvFiles.Items.Clear();

		// is there anything in al
		if (al==null) return; //nuttin' 'ere

		for (i=0;i<al.Count;i++) {
			jobData = (JobData)al[i];

			lvFiles.Items.Add(new ListViewItem(new String[] { jobData.localName, jobData.url} , (jobData.nowDownloading ? 0 : 1)));

			if (startInterrupted && jobData.nowDownloading) {
				if (MessageBox.Show("The download of the file " + jobData.localName + " was terminated abnormally.\n\nWould you like to resume the download now?", "Resume download?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) {
					DownloadForm dlf = new DownloadForm(jobData, false);
					Thread thread = new Thread(new ThreadStart(dlf.StartDownload));
					thread.Start();
				}
				else {
					jobData.nowDownloading=false;
					JobManagement.ModifyJob(jobData, false);
					lvFiles.Items[lvFiles.Items.Count-1].ImageIndex=1;
				}
			}
		}
	}

	private void CloseMe(Object Src, EventArgs e) {
		this.Close();
		this.Dispose();
		return;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -