// testDlg.cpp : implementation file // #include "stdafx.h" #include "test.h" #include "testDlg.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() ///////////////////////////////////////////////////////////////////////////// // CTestDlg dialog CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/) : CDialog(CTestDlg::IDD, pParent) { //{{AFX_DATA_INIT(CTestDlg) // 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); } void CTestDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CTestDlg) // NOTE: the ClassWizard will add DDX and DDV calls here //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CTestDlg, CDialog) //{{AFX_MSG_MAP(CTestDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_WHERE, OnWhere) ON_BN_CLICKED(IDC_START, OnStart) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CTestDlg message handlers BOOL CTestDlg::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 CTestDlg::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 CTestDlg::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 CTestDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CTestDlg::OnWhere() { // TODO: Add your control notification handler code here GetDlgItemText(IDC_URL,url); if(url=="") { AfxMessageBox("请填入URL地址"); return; } //下面得到文件类型,即.后面的字符 CString aboutfile=""; int len = url.GetLength(); for(int i=len-1;url[i] != '.';i--) aboutfile.Insert(0,url[i]); CString strFileName,strFilePath; CString m_szDir = "c:\\"; char BASED_CODE szFilter[] = "All Files (*.*)|*.*||"; CFileDialog OpenDlg(false, NULL, NULL, /*OFN_ALLOWMULTISELECT |*/ OFN_EXPLORER, szFilter); OpenDlg.m_ofn.lStructSize = sizeof(OpenDlg.m_ofn); OpenDlg.m_ofn.hwndOwner = NULL; OpenDlg.m_ofn.lpstrTitle = "保存目标文件"; OpenDlg.m_ofn.lpstrInitialDir=m_szDir; //初始目录 if( OpenDlg.DoModal() == IDOK) { strFilePath = OpenDlg.GetPathName(); } savefilename=strFilePath+"."+aboutfile; SetDlgItemText(IDC_PATH,savefilename); } void CTestDlg::OnStart() { // TODO: Add your control notification handler code here 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 dwServiceType; DWORD strObject; DWORD dwDownloadSize = 0; CString strLogs; //分解URL if(AfxParseURL(url, dwServiceType, strServer, strObject, nPort)) { //如果服务类型不是HTTP下载 if(dwServiceType != AFX_INET_SERVICE_HTTP && dwServiceType != AFX_INET_SERVICE_HTTPS) { return; } } while(pHttpConnection == NULL) 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(savefilename, 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); nLen = pHttpFile->Read(szBuffer, 1024);//接收数据 while(nLen >= 1024)//接收文件数据每次接收一个块 { if(nLen > 0)//如果接收到数据 aFile.Write(szBuffer, nLen); //写入文件 nLen = pHttpFile->Read(szBuffer, 1024);//接收一个块的数据 } if(nLen > 0)//如果接收到数据 { aFile.Write(szBuffer, nLen);//写入文件 dwDownloadSize += nLen; } aFile.Close();//关闭文件 nSuccess = 0;//设置成功标志 AfxMessageBox("下载完成"); } } } } delete pHttpFile; } delete pHttpConnection;//删除http连接对象 } aInternetSession.Close();//关闭http连接 }