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

📄 netdownmtrdlg.cpp

📁 ◆◆◆ 《FTP、HTTP 多线程断点续传下载文件》◆◆◆ FlashGet、网络蚂蚁想必大家都很熟悉
💻 CPP
字号:
// NetDownMTRDlg.cpp : implementation file
//

#include "stdafx.h"
#include "NetDownMTR.h"
#include "NetDownMTRDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	virtual BOOL OnInitDialog();
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNetDownMTRDlg dialog

CNetDownMTRDlg::CNetDownMTRDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CNetDownMTRDlg::IDD, pParent)
	, m_pDownloadMTR ( NULL )
	, m_bDownloadIsStart ( FALSE )
{
	//{{AFX_DATA_INIT(CNetDownMTRDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_Ary_DownloadNotifyPara.SetSize ( 0, 10*sizeof(t_DownloadNotifyPara) );
}

void CNetDownMTRDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CNetDownMTRDlg)
	DDX_Control(pDX, IDC_PROGRESS1, m_progress);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CNetDownMTRDlg, CDialog)
	//{{AFX_MSG_MAP(CNetDownMTRDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_Browse, OnBUTTONBrowse)
	ON_WM_DESTROY()
	ON_EN_CHANGE(IDC_EDIT_RemoteFileURL, OnChangeEDITRemoteFileURL)
	ON_BN_CLICKED(IDC_BUTTON_Stop, OnBUTTONStop)
	ON_MESSAGE(WM_DOWNLOAD_NOTIFY,OnWM_DOWNLOAD_NOTIFY)
	ON_BN_CLICKED(IDC_BUTTON_About, OnBUTTONAbout)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNetDownMTRDlg message handlers

BOOL CNetDownMTRDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	SetCtrlValue ();
	EnableSomeWindow ();
	
	BOOL success = m_OleTarget.Register(this);
    if(!success )
		MessageBox("Ole Register Drop Target Failed");             
	
	m_OleTarget.SetParent(this);

	// 总在最前
	CRect rect;
	GetWindowRect(&rect);
	SetWindowPos(&wndTopMost, rect.left, rect.top, rect.Width(), rect.Height(), SWP_NOMOVE); 

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CNetDownMTRDlg::OnSysCommand(UINT nID,LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CNetDownMTRDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CNetDownMTRDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void Callback_DownloadNotify ( t_DownloadNotifyPara *pDownloadNotifyPara, WPARAM wParam )
{
	TRACE ( "%d, %d, 0x%x, %d, 0x%x\n", pDownloadNotifyPara->nIndex, pDownloadNotifyPara->nNotityType,
		pDownloadNotifyPara->lpNotifyData, pDownloadNotifyPara->pDownloadMTR, wParam );
	CNetDownMTRDlg *pNetDownMTRDlg = (CNetDownMTRDlg*)wParam;
	ASSERT ( pNetDownMTRDlg );
	pNetDownMTRDlg->Add_DownloadNotifyPara ( pDownloadNotifyPara );
	pNetDownMTRDlg->PostMessage ( WM_DOWNLOAD_NOTIFY, WPARAM(NULL), LPARAM(NULL) );
}

LRESULT CNetDownMTRDlg::OnWM_DOWNLOAD_NOTIFY(WPARAM wParam, LPARAM lParam)
{
	t_Ary_DownloadNotifyPara Ary_DownloadNotifyPara;
	Get_Ary_DownloadNotifyPara ( Ary_DownloadNotifyPara );
	for ( int i=0; i<Ary_DownloadNotifyPara.GetSize(); i++ )
	{
		t_DownloadNotifyPara &DownloadNotifyPara = Ary_DownloadNotifyPara.GetAt (i);
		Callback_DownloadNotify ( &DownloadNotifyPara );
	}
	return TRUE;
}

void CNetDownMTRDlg::Callback_DownloadNotify(t_DownloadNotifyPara *pDownloadNotifyPara)
{
	ASSERT ( pDownloadNotifyPara );
	CString csStatus;
	switch ( pDownloadNotifyPara->nNotityType )
	{
	case NOTIFY_TYPE_GOT_REMOTE_FILENAME:
		{
			OutStatusString ( pDownloadNotifyPara->nIndex, (LPCTSTR)pDownloadNotifyPara->lpNotifyData );
			break;
		}
	case NOTIFY_TYPE_GOT_REMOTE_FILESIZE:
		{
			int nFileSize = (int)pDownloadNotifyPara->lpNotifyData;
			csStatus.Format ( "File size is : %d bytes", nFileSize );
			OutStatusString ( pDownloadNotifyPara->nIndex, csStatus );
			ASSERT ( nFileSize == ( (CDownloadMTR*)pDownloadNotifyPara->pDownloadMTR )->Get_FileTotaleSize() );
			m_progress.SetRange32 ( 0, nFileSize );
			break;
		}
	case NOTIFY_TYPE_START_DOWNLOAD:
		{
			OutStatusString ( pDownloadNotifyPara->nIndex, _T("Start download") );
			break;
		}
	case NOTIFY_TYPE_END_DOWNLOAD:
		{
			m_bDownloadIsStart = FALSE;
			EnableSomeWindow ();
			OutStatusString ( pDownloadNotifyPara->nIndex, _T("End download") );
			ENUM_DOWNLOAD_RESULT eDownloadResult = (ENUM_DOWNLOAD_RESULT)(int)pDownloadNotifyPara->lpNotifyData;
			CString csMsg, csElapseTime;
			ASSERT ( pDownloadNotifyPara->pDownloadMTR );
			csElapseTime.Format ( "%d ms", ( (CDownloadMTR*)pDownloadNotifyPara->pDownloadMTR )->GetDownloadElapsedTime() );
			if ( eDownloadResult == ENUM_DOWNLOAD_RESULT_SUCCESS )
			{
				csMsg = "Download successfully";
			}
			else if ( eDownloadResult == ENUM_DOWNLOAD_RESULT_FAILED )
			{
				csMsg = "Download failed";
			}
			else if ( eDownloadResult == ENUM_DOWNLOAD_RESULT_CANCEL )
			{
				csMsg = "User cancel download";
			}
			else
			{
				ASSERT ( FALSE );
			}
			csMsg += ", Elapse time : " + csElapseTime;
			AfxMessageBox ( csMsg, (eDownloadResult==ENUM_DOWNLOAD_RESULT_SUCCESS)?MB_ICONINFORMATION:MB_ICONWARNING );

			break;
		}
	case NOTIFY_TYPE_WILL_DOWNLOAD_SIZE:
		{
			csStatus.Format ( "Will download size is : %d bytes", (int)pDownloadNotifyPara->lpNotifyData );
			OutStatusString ( pDownloadNotifyPara->nIndex, csStatus );
			break;
		}
	case NOTIFY_TYPE_THREAD_DOWNLOADED_SIZE:
		{
			csStatus.Format ( "Downloaded size is : %d bytes", (int)pDownloadNotifyPara->lpNotifyData );
			OutStatusString ( pDownloadNotifyPara->nIndex, csStatus );
			int nFileSize = ( (CDownloadMTR*)pDownloadNotifyPara->pDownloadMTR )->Get_FileTotaleSize();
			int nTotalDownloadedSize = ( (CDownloadMTR*)pDownloadNotifyPara->pDownloadMTR )->Get_TotalDownloadedSize ();
			if ( nFileSize > 0 && nTotalDownloadedSize > 0 )
			{
				m_progress.SetPos ( nTotalDownloadedSize );
				m_progress.Invalidate ();
			}
			break;
		}
	}
}

void CNetDownMTRDlg::OnOK() 
{
	CString csRemoteFileURL = GetCtrlValueAndSaveSettingInfo(IDC_EDIT_RemoteFileURL);
	if ( csRemoteFileURL.IsEmpty () )
	{
		Log ( L_WARNING, "Download URL can't be empty" );
		GetDlgItem(IDC_EDIT_RemoteFileURL)->SetFocus ();
		return;
	}
	CString csLocalFilePath = GetCtrlValueAndSaveSettingInfo(IDC_EDIT_LocalFilePath);
	if ( csLocalFilePath.IsEmpty() )
	{
		Log ( L_WARNING, "Save path can't be empty" );
		GetDlgItem(IDC_EDIT_LocalFilePath)->SetFocus ();
		return;
	}
	CString csFileName = GetCtrlValueAndSaveSettingInfo(IDC_EDIT_FileName);
	if ( csFileName.IsEmpty() )
	{
		Log ( L_WARNING, "Save path can't be empty" );
		GetDlgItem(IDC_EDIT_FileName)->SetFocus ();
		return;
	}

	Set_DownloadNotify_Callback ( ::Callback_DownloadNotify, WPARAM(this) );
	if ( !CreateInstance_DownloadMTR () )
	{
		Log ( L_WARNING, "Create download instance failed" );
		return;
	}

	m_bDownloadIsStart = TRUE;
	EnableSomeWindow ();
	m_progress.SetPos ( 0 );
	m_pDownloadMTR->SetThreadCount ( atoi(GetCtrlValueAndSaveSettingInfo(IDC_EDIT_ThreadNumber)) );
	m_pDownloadMTR->Download ( csRemoteFileURL, csLocalFilePath, csFileName,
		GetCtrlValueAndSaveSettingInfo(IDC_EDIT_UserName),
		GetCtrlValueAndSaveSettingInfo(IDC_EDIT_Password) );
}

void CNetDownMTRDlg::OnBUTTONBrowse() 
{
	SelectPathByCommonDlg ( this, IDC_EDIT_LocalFilePath );
}

void CNetDownMTRDlg::SetCtrlValue()
{
	ReadSettingInfoAndSetCtrlValue ( IDC_EDIT_RemoteFileURL, "" );
	ReadSettingInfoAndSetCtrlValue ( IDC_EDIT_LocalFilePath, "" );
	ReadSettingInfoAndSetCtrlValue ( IDC_EDIT_FileName, "" );
	ReadSettingInfoAndSetCtrlValue ( IDC_EDIT_UserName, "" );
	ReadSettingInfoAndSetCtrlValue ( IDC_EDIT_Password, "" );
	ReadSettingInfoAndSetCtrlValue ( IDC_EDIT_ThreadNumber, "4" );
}

void CNetDownMTRDlg::GetCtrlValue()
{
	GetCtrlValueAndSaveSettingInfo ( IDC_EDIT_RemoteFileURL );
	GetCtrlValueAndSaveSettingInfo ( IDC_EDIT_LocalFilePath );
	GetCtrlValueAndSaveSettingInfo ( IDC_EDIT_FileName );
	GetCtrlValueAndSaveSettingInfo ( IDC_EDIT_UserName );
	GetCtrlValueAndSaveSettingInfo ( IDC_EDIT_Password );
	GetCtrlValueAndSaveSettingInfo ( IDC_EDIT_ThreadNumber );
}

void CNetDownMTRDlg::ReadSettingInfoAndSetCtrlValue(UINT nCtrlID, LPCTSTR lpszDefaultValue)
{
	CString csEntry;
	csEntry.Format ( "%d", nCtrlID );
	SetDlgItemText ( nCtrlID, AfxGetApp()->GetProfileString ( "Setting", csEntry, lpszDefaultValue ) );
}

CString CNetDownMTRDlg::GetCtrlValueAndSaveSettingInfo(UINT nCtrlID)
{
	CString csEntry, csValue;
	csEntry.Format ( "%d", nCtrlID );
	GetDlgItemText ( nCtrlID, csValue );
	csValue.TrimLeft(); csValue.TrimRight ();
	 AfxGetApp()->WriteProfileString ( "Setting", csEntry, csValue );
	 return csValue;
}

void CNetDownMTRDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	DeleteInstance_DownloadMTR ();
	GetCtrlValue ();
}

void CNetDownMTRDlg::OnChangeEDITRemoteFileURL() 
{
	CString csFileURL;
	GetDlgItemText ( IDC_EDIT_RemoteFileURL, csFileURL );
	csFileURL.TrimLeft(); csFileURL.TrimRight ();
	CString csFileNameByURL = CDownloadMTR::GetLocalFileNameByURL ( csFileURL );
	SetDlgItemText ( IDC_EDIT_FileName, csFileNameByURL );
	
}

void CNetDownMTRDlg::OutStatusString(int nIndex, LPCTSTR lpszText)
{
	if ( nIndex != -1 && (nIndex < 0 || nIndex > 7) )
		return;
	nIndex ++;
	UINT nID = IDC_STATIC_Status + nIndex;
	SetDlgItemText ( nID, GET_SAFE_STRING(lpszText) );
}

CDownloadMTR* CNetDownMTRDlg::CreateInstance_DownloadMTR()
{
	DeleteInstance_DownloadMTR ();
	m_pDownloadMTR = new CDownloadMTR;
	return m_pDownloadMTR;
}

void CNetDownMTRDlg::DeleteInstance_DownloadMTR()
{
	if ( m_pDownloadMTR )
		delete m_pDownloadMTR;
	m_pDownloadMTR = NULL;
}

void CNetDownMTRDlg::OnBUTTONStop() 
{
	if ( m_pDownloadMTR )
		m_pDownloadMTR->StopDownload ();
}

BOOL CNetDownMTRDlg::Add_DownloadNotifyPara(t_DownloadNotifyPara *pDownloadNotifyPara)
{
	if ( !pDownloadNotifyPara ) return FALSE;

	t_DownloadNotifyPara DownloadNotifyPara = {0};
	memcpy ( &DownloadNotifyPara, pDownloadNotifyPara, sizeof(t_DownloadNotifyPara) );

	m_CSFor_Ary_DownloadNotifyPara.Lock ();
	BOOL bRet = FALSE;
	TRY
	{
		m_Ary_DownloadNotifyPara.Add ( DownloadNotifyPara );
		bRet = TRUE;
	}
	CATCH ( CMemoryException, e )
	{
		e->Delete ();
		bRet = FALSE;
	}
	END_CATCH
	m_CSFor_Ary_DownloadNotifyPara.Unlock ();
	if ( !bRet ) Log ( L_ERROR, "Memory too low" );

	return bRet;
}

void CNetDownMTRDlg::Get_Ary_DownloadNotifyPara(t_Ary_DownloadNotifyPara &Ary_DownloadNotifyPara)
{
	m_CSFor_Ary_DownloadNotifyPara.Lock ();
	Ary_DownloadNotifyPara.Append ( m_Ary_DownloadNotifyPara );
	m_Ary_DownloadNotifyPara.RemoveAll ();
	m_CSFor_Ary_DownloadNotifyPara.Unlock ();
}

void CNetDownMTRDlg::EnableSomeWindow()
{
	GetDlgItem(IDC_EDIT_RemoteFileURL)->EnableWindow ( !m_bDownloadIsStart );
	GetDlgItem(IDC_EDIT_LocalFilePath)->EnableWindow ( !m_bDownloadIsStart );
	GetDlgItem(IDC_BUTTON_Browse)->EnableWindow ( !m_bDownloadIsStart );
	GetDlgItem(IDC_EDIT_FileName)->EnableWindow ( !m_bDownloadIsStart );
	GetDlgItem(IDC_EDIT_UserName)->EnableWindow ( !m_bDownloadIsStart );
	GetDlgItem(IDC_EDIT_Password)->EnableWindow ( !m_bDownloadIsStart );
	GetDlgItem(IDC_EDIT_ThreadNumber)->EnableWindow ( !m_bDownloadIsStart );
	GetDlgItem(IDOK)->EnableWindow ( !m_bDownloadIsStart );
	GetDlgItem(IDC_BUTTON_Stop)->EnableWindow ( m_bDownloadIsStart );
}

void CNetDownMTRDlg::AddURL ( LPCTSTR lpszURL )
{
	SetDlgItemText ( IDC_EDIT_RemoteFileURL, GET_SAFE_STRING(lpszURL) );
}

void CNetDownMTRDlg::OnBUTTONAbout() 
{
	CAboutDlg dlgAbout;
	dlgAbout.DoModal();
}

BOOL CAboutDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	SetDlgItemText ( IDC_EDIT1, NOTE_DownloadMTR );
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

⌨️ 快捷键说明

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