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

📄 netdownmtrdlg.cpp

📁 多线程下载的
💻 CPP
📖 第 1 页 / 共 2 页
字号:
			int nFileSize = ( (CDownloadMTR*)pDownloadNotifyPara->pDownloadMTR )->Get_FileTotaleSize();
			CDownloadMTR *pDownloadMTR = ( (CDownloadMTR*)pDownloadNotifyPara->pDownloadMTR );
			ASSERT ( pDownloadMTR );
			int nTotalDownloadedSize = pDownloadMTR->Get_TotalDownloadedSize ();
			if ( nFileSize > 0 && nTotalDownloadedSize > 0 )
			{
				if ( nTotalDownloadedSize <= nFileSize )
				{
					m_progress.SetPos ( nTotalDownloadedSize );
					m_progress.Invalidate ();
				}

				// 计算下载速度
				int nTotalDownloadedSize_ThisTimes = pDownloadMTR->Get_TotalDownloadedSize_ThisTimes ();
				int nElapsedTime = pDownloadMTR->GetDownloadElapsedTime ();
				if ( nElapsedTime > 5*1000 || ( nElapsedTime > 2*1000 && nTotalDownloadedSize_ThisTimes > 1024*10 ) )
				{
					double dSpeed = (double)nTotalDownloadedSize_ThisTimes / ((double)nElapsedTime / 1000.0);
					CString csSpeed = FormatFileSize ( dSpeed );
					csSpeed += "/S";
					SetDlgItemText ( IDC_STATIC_Speed, csSpeed );
				}
			}
			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),
		( (CButton*)GetDlgItem(IDC_CHECK_Force) )->GetCheck () );
	SetDlgItemText ( IDC_EDIT_Log, "" );
}

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" );

	( (CButton*)GetDlgItem(IDC_CHECK_Force) )->SetCheck ( AfxGetApp()->GetProfileInt ( "Setting", "IDC_CHECK_Force", 0 ) );
}

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 );

	AfxGetApp()->WriteProfileInt ( "Setting", "IDC_CHECK_Force", ( (CButton*)GetDlgItem(IDC_CHECK_Force) )->GetCheck () );
}

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_CHECK_Force)->EnableWindow ( !m_bDownloadIsStart );
	GetDlgItem(IDC_BUTTON_Stop)->EnableWindow ( m_bDownloadIsStart );
}

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

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
}

void CNetDownMTRDlg::SetFileNameEditSel()
{
	CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT_FileName);
	CString csText;
	pEdit->GetWindowText ( csText );
	int nPos = csText.ReverseFind ( '.' );
	if ( nPos >= 0 )
	{
		pEdit->SetSel ( 0, 0 );
		pEdit->SetSel ( 0, nPos );
	}
}

BOOL CNetDownMTRDlg::PreTranslateMessage(MSG* pMsg) 
{
	if ( WM_LBUTTONDOWN == pMsg->message || WM_LBUTTONUP == pMsg->message )
	{
		if ( !m_bFileNameGetFocus )
		{
			CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT_FileName);
			CRect rc;
			pEdit->GetWindowRect ( &rc );
			if ( rc.PtInRect (pMsg->pt) )
			{
				GetDlgItem(IDC_EDIT_FileName)->SetFocus ();
				SetFileNameEditSel ();
				m_bFileNameGetFocus = TRUE;
				return TRUE;
			}
		}
	}

	return CDialog::PreTranslateMessage(pMsg);
}

void CNetDownMTRDlg::OnKillfocusEDITFileName() 
{
	m_bFileNameGetFocus = FALSE;
}

void CNetDownMTRDlg::OnSetfocusEDITFileName() 
{
	SetFileNameEditSel ();
}

LRESULT CNetDownMTRDlg::OnWM_SHOWLOG(WPARAM wParam, LPARAM lParam)
{
	UINT nLevel = wParam;
	CStringArray StrAryLog;
	CUIntArray UIntAryLevel;
	GetLastLogStrAry ( StrAryLog, UIntAryLevel );
	ASSERT ( StrAryLog.GetSize() == UIntAryLevel.GetSize() );
	for ( int i=0; i<StrAryLog.GetSize(); i++ )
	{
		CString csLogText = StrAryLog.GetAt ( i );
		AddLogTextToEditCtrl ( (CRichEditCtrl*)GetDlgItem (IDC_EDIT_Log), csLogText, UIntAryLevel.GetAt(i) );
	}
	return 0L;

}

⌨️ 快捷键说明

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