www.pudn.com > HttpDown.rar > DownLoadDlg.cpp
// DownLoadDlg.cpp : implementation file // #include "stdafx.h" #include "DownLoad.h" #include "DownLoadDlg.h" #include "DirDialog.h" #include#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) //}}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) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CDownLoadDlg dialog CDownLoadDlg::CDownLoadDlg(CWnd* pParent /*=NULL*/) : CDialog(CDownLoadDlg::IDD, pParent) { //{{AFX_DATA_INIT(CDownLoadDlg) m_strFilePath = _T(""); m_strURL = _T(""); //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); m_hDownLoadThread = NULL; m_bExist = false; } void CDownLoadDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CDownLoadDlg) DDX_Control(pDX, IDC_PROGRESS1, m_ProgressCtrl); DDX_Text(pDX, IDC_PATH_EDIT, m_strFilePath); DDX_Text(pDX, IDC_URL_EDIT, m_strURL); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CDownLoadDlg, CDialog) //{{AFX_MSG_MAP(CDownLoadDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BROWSEBTN, OnBrowsebtn) ON_BN_CLICKED(IDC_CANCELBTN, OnCancelbtn) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CDownLoadDlg message handlers BOOL CDownLoadDlg::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 // TODO: Add extra initialization here return TRUE; // return TRUE unless you set the focus to a control } void CDownLoadDlg::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 CDownLoadDlg::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 CDownLoadDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CDownLoadDlg::OnOK() { // TODO: Add extra validation here UpdateData(true); LPSTR pCh = (LPSTR)(LPCSTR)m_strURL; LPSTR pName = _tcsrchr(pCh,'/') + 1; m_strFilePath += "\\"; m_strFilePath += pName; //启动下载线程 m_hDownLoadThread = CreateThread(NULL, 0, DownloadFileThread, this, CREATE_SUSPENDED, 0); if(m_hDownLoadThread == NULL) { //显示错误提示信息 MessageBox(_T("启动版本更新失败,请重新启动计算机后再尝试升级!"), _T("自动升级"), MB_OK | MB_ICONERROR); //程序退出 PostQuitMessage(0); } //启动线程 m_bExist = FALSE; ResumeThread(m_hDownLoadThread); // DownloadFile(strPath,m_strURL); // CDialog::OnOK(); } void CDownLoadDlg::OnCancelbtn() { // TODO: Add your control notification handler code here if(!m_bExist) { m_bExist = true; } Sleep(50); m_hDownLoadThread = NULL; } void CDownLoadDlg::OnCancel() { // TODO: Add extra cleanup here if(!m_bExist) { m_bExist = true; } Sleep(50); m_hDownLoadThread = NULL; CDialog::OnCancel(); } void CDownLoadDlg::OnBrowsebtn() { // TODO: Add your control notification handler code here CDirDialog dlg; dlg.m_strTitle = "保存路径"; dlg.m_strSelDir = m_strFilePath; if (dlg.DoBrowse()) { m_strFilePath = dlg.m_strPath; UpdateData(false); } } DWORD WINAPI CDownLoadDlg::DownloadFileThread(LPVOID lpvoid) { CDownLoadDlg* pThis = (CDownLoadDlg *)lpvoid; if(pThis == NULL) return 0; CInternetSession aInternetSession; //一个会话 CHttpConnection* pHttpConnection = NULL; //链接 CHttpFile* pHttpFile = NULL; const TCHAR szHeaders[] = _T("Accept: */*\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)\r\n"); //协议 DWORD dwFileStatus; BYTE szBuffer[1024]; DWORD nLen; CFile aFile; DWORD nSuccess = -1; CString strServer; INTERNET_PORT nPort; CString strObject; DWORD dwServiceType; DWORD dwDownloadSize = 0; CString strLogs; //分解URL if(AfxParseURL(pThis->m_strURL, dwServiceType, strServer, strObject, nPort)) { //如果服务类型是HTTP下载 if(dwServiceType != AFX_INET_SERVICE_HTTP && dwServiceType != AFX_INET_SERVICE_HTTPS) { //返回成功 return FALSE; } } try { //建立http连接 pHttpConnection = aInternetSession.GetHttpConnection(strServer, nPort); //如果失败则线程退出 if(pHttpConnection != NULL) { //取得HttpFile对象 pHttpFile = pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_GET, strObject, NULL, 1, NULL, NULL, INTERNET_FLAG_RELOAD | INTERNET_FLAG_DONT_CACHE); //如果失败则线程退出 if(pHttpFile != NULL) { //增加请求头,如果失败 if(pHttpFile->AddRequestHeaders(szHeaders)) { //发送文件请求 if(pHttpFile->SendRequest()) { //查询文件状态 if(pHttpFile->QueryInfoStatusCode(dwFileStatus)) { //如果文件状态正常 if ((dwFileStatus/100)*100 == HTTP_STATUS_OK) { //打开文件 if(aFile.Open(pThis->m_strFilePath, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary)) { DWORD dwFileLen; DWORD dwWordSize = sizeof(dwFileLen); pHttpFile->QueryInfo(HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_CONTENT_LENGTH, &dwFileLen,&dwWordSize,NULL); pThis->m_ProgressCtrl.SetRange32(0, dwFileLen); //接收数据 nLen = pHttpFile->Read(szBuffer, 1024); //接收文件数据每次接收一个块 while(nLen >= 1024 && !pThis->m_bExist) { //如果接收到数据 if(nLen > 0) { //写入文件 aFile.Write(szBuffer, nLen); dwDownloadSize += nLen; //SetFileProgress(dwDownloadSize); pThis->m_ProgressCtrl.SetPos(dwDownloadSize); } //接收一个块的数据 nLen = pHttpFile->Read(szBuffer, 1024); } //如果接收到数据 if(nLen > 0) { //写入文件 aFile.Write(szBuffer, nLen); dwDownloadSize += nLen; pThis->m_ProgressCtrl.SetPos(dwDownloadSize); } //关闭文件 aFile.Close(); //设置成功标志 nSuccess = 0; AfxMessageBox("下载完成"); } } } } } //删除http文件对象 delete pHttpFile; } //删除http连接对象 delete pHttpConnection; } //关闭http连接 aInternetSession.Close(); //返回 return nSuccess; } //异常处理 catch(CInternetException* e) { TCHAR szCause[MAX_PATH] = {0}; //取得错误信息 e->GetErrorMessage(szCause, MAX_PATH); //错误信息写入日志 TRACE("internet exception:%s\n", szCause); //删除异常对象 e->Delete(); //删除http文件对象 if(pHttpFile != NULL ) delete pHttpFile; //删除http连接对象 if(pHttpConnection != NULL) delete pHttpConnection; //关闭http连接 aInternetSession.Close(); //返回 return -1; } }