📄 filedowndlg.cpp
字号:
// FileDownDlg.cpp : implementation file
//
#include "stdafx.h"
#include "FileDown.h"
#include "FileDownDlg.h"
#include <wininet.h>
#include <afxmt.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)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CFileDownDlg dialog
CFileDownDlg::CFileDownDlg(CWnd* pParent /*=NULL*/)
: CDialog(CFileDownDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CFileDownDlg)
// 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 CFileDownDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CFileDownDlg)
DDX_Control(pDX, IDC_FILELIST, m_filelist);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CFileDownDlg, CDialog)
//{{AFX_MSG_MAP(CFileDownDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BTDOWNLOAD, OnDownLoad)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFileDownDlg message handlers
BOOL CFileDownDlg::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
m_filelist.SetExtendedStyle(LVS_EX_GRIDLINES);
m_filelist.InsertColumn(0,"Index",LVCFMT_LEFT,20);
m_filelist.InsertColumn(1,"文件URL地址",LVCFMT_LEFT,200);
m_filelist.InsertColumn(2,"本地保存路径",LVCFMT_LEFT,200);
return TRUE;
}
void CFileDownDlg::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 CFileDownDlg::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 CFileDownDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
CMutex cMutex(FALSE,NULL);
DWORD WINAPI ThreadProc1(LPVOID lparam)
{
CFileDownDlg*pdlg=(CFileDownDlg*)lparam;
CHAR szHead[]="Accept: */*\r\n\r\n";
if(pdlg->strurl=="")return 0;
VOID* szTemp[25];
DWORD dwSize;
HINTERNET hConnect;
HINTERNET hOpen;
FILE * pFile;
cMutex.Lock();
hOpen=InternetOpen(NULL,
INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
hConnect=InternetOpenUrl(hOpen,pdlg->strurl,szHead,lstrlen(szHead),INTERNET_FLAG_DONT_CACHE,0);
cMutex.Unlock();
if(!hConnect)return 0;
pFile=fopen(pdlg->strlocalname,"wb");
while(1){
InternetReadFile (hConnect,szTemp,50,&dwSize);
if(!dwSize)
goto end;
else
fwrite(szTemp,sizeof(char),dwSize,pFile);
}
end:
MessageBox(NULL,"下载完成","提示",MB_OK);
fflush (pFile);
fclose (pFile);
InternetCloseHandle(hConnect);
InternetCloseHandle(hOpen);
return 0;
}
DWORD WINAPI ThreadProc2(LPVOID lparam)
{
CFileDownDlg*pdlg=(CFileDownDlg*)lparam;
if(pdlg->strurl=="")return 0;
HINTERNET hInternetSession;
HINTERNET hFTPSession;
hInternetSession = InternetOpen(
"Microsoft Internet Explorer",
INTERNET_OPEN_TYPE_PRECONFIG,
NULL,NULL,0);
hFTPSession=InternetConnect(
hInternetSession,pdlg->strftpserver,
INTERNET_INVALID_PORT_NUMBER,
NULL,NULL,
INTERNET_SERVICE_FTP,0,(DWORD)0);
if(!hFTPSession)return 0;
DWORD dwt=FtpGetFile(hFTPSession,
pdlg->strresult,
pdlg->strlocalname,
FALSE,FILE_ATTRIBUTE_NORMAL,
FTP_TRANSFER_TYPE_BINARY,0);
if(dwt)MessageBox(NULL,"下载完成","提示",MB_OK);
InternetCloseHandle(hInternetSession);
InternetCloseHandle(hFTPSession);
return 0;
}
void CFileDownDlg::OnDownLoad()
{
CString rightstrtmp,midstrtmp;
int index=m_filelist.GetItemCount();
GetDlgItem(IDC_EDFILENAME)->GetWindowText(strurl);
if(strurl.IsEmpty())return;
//检查URL,可以下载
strurl.TrimLeft();
strurl.TrimRight();
//strprotocol设置协议字符串,strleft提取的协议字符
CString strprotocol="",strleft;
strleft=strurl.Left(7);
if(strleft.Compare("http://")==0)
{
strprotocol="http";
}
else
{
strleft=strurl.Left(6);
if(strleft.Compare("ftp://")==0)
{
strprotocol="ftp";
}
else
{
AfxMessageBox("不能识别的URL");
return;
}
}
if(strprotocol.Compare("http")==0)
{
rightstrtmp=strurl.Right(strurl.GetLength()-7);
int pos=rightstrtmp.Find("/");
midstrtmp=rightstrtmp.Right(rightstrtmp.GetLength()-pos-1);
if(midstrtmp.IsEmpty())
{
AfxMessageBox("没有指定文件");
return;
}
pos=midstrtmp.Find("/");
while(pos>0)
{
midstrtmp=midstrtmp.Right(midstrtmp.GetLength()-pos-1);
pos=midstrtmp.Find("/");
}
strresult=midstrtmp;
}
if(strprotocol.Compare("ftp")==0)
{
rightstrtmp=strurl.Right(strurl.GetLength()-6);
int pos=rightstrtmp.Find("/");
strftpserver=rightstrtmp.Left(pos);
midstrtmp=rightstrtmp.Right(rightstrtmp.GetLength()-pos-1);
strftpfile=midstrtmp;
if(midstrtmp.IsEmpty())
{
AfxMessageBox("没有指定文件");
return;
}
pos=midstrtmp.Find("/");
while(pos>0)
{
midstrtmp=midstrtmp.Right(midstrtmp.GetLength()-pos-1);
pos=midstrtmp.Find("/");
}
strresult=midstrtmp;
}
BROWSEINFO bi;
char buffer[MAX_PATH];
ZeroMemory(buffer,MAX_PATH);
bi.hwndOwner=GetSafeHwnd();
bi.pidlRoot=NULL;
bi.pszDisplayName=buffer;
bi.lpszTitle="选择一个文件夹";
bi.ulFlags=BIF_EDITBOX;
bi.lpfn=NULL;
bi.lParam=0;
bi.iImage=0;
LPITEMIDLIST pList=NULL;
if((pList=SHBrowseForFolder(&bi))!=NULL)
{
char path[MAX_PATH];
ZeroMemory(path,MAX_PATH);
SHGetPathFromIDList(pList,path);
m_filelist.InsertItem(index,"");
//strindex为下载的序号
CString strindex;strindex.Format("%d",index+1);
m_filelist.SetItemText(index,0,strindex);
m_filelist.SetItemText(index,1,strurl);
CString str;
path[strlen(path)]=0;
str.Format("%s",path);
//判断是否是根目录
if(str.Right(1)!="\\")
{
str+="\\";
str+=strresult;
}
else
str+=strresult;
strlocalname=str;
m_filelist.SetItemText(index,2,strlocalname);
if(strprotocol.Compare("http")==0)
CreateThread(NULL,0,ThreadProc1,this,0,NULL);
if(strprotocol.Compare("ftp")==0)
CreateThread(NULL,0,ThreadProc2,this,0,NULL);
strprotocol="";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -