TestDlg.cpp
资源名称:FTP.rar [点击查看]
上传用户:guibin827
上传日期:2021-12-07
资源大小:165k
文件大小:11k
源码类别:
Ftp客户端
开发平台:
Visual C++
- // TestDlg.cpp : 实现文件
- //
- #include "stdafx.h"
- #include "Test.h"
- #include "TestDlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- // 用于应用程序“关于”菜单项的 CAboutDlg 对话框
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
- // 对话框数据
- enum { IDD = IDD_ABOUTBOX };
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
- // 实现
- protected:
- DECLARE_MESSAGE_MAP()
- };
- CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
- {
- }
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- }
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
- END_MESSAGE_MAP()
- // CTestDlg 对话框
- CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CTestDlg::IDD, pParent)
- {
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- m_StrPassword = _T("");
- m_StrFTPServer = _T("");
- m_StrUserName = _T("");
- m_pIntSession = NULL;
- m_pFTPConnection = NULL;
- m_pRemoteFinder = NULL;
- m_StrLocalParentRoot = m_StrRemoteParentRoot = "";
- }
- void CTestDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- DDX_Control(pDX, IDC_LIST_REMOTE, m_ListRemoteFiles);
- DDX_Control(pDX, IDC_LIST_LOCAL, m_ListLocalFiles);
- DDX_Text(pDX, IDC_PASSWORD, m_StrPassword);
- DDX_Text(pDX, IDC_SERVER, m_StrFTPServer);
- DDX_Text(pDX, IDC_USER, m_StrUserName);
- }
- BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(IDC_CONNECT, OnBtnConnectFTPServer)
- ON_BN_CLICKED(IDC_DOWNLOAD, OnBtnDownloadFile)
- ON_BN_CLICKED(IDC_LOCAL_UP, OnLocalParentFolder)
- ON_BN_CLICKED(IDC_LOCAL_DOWND, OnLocalChildFolder)
- ON_BN_CLICKED(IDC_REMOTE_UP, OnRemoteParentFolder)
- ON_BN_CLICKED(IDC_REMOTE_DOWND, OnRemoteChildFolder)
- ON_BN_CLICKED(IDC_UPLOAD, OnBtnUpLoadFile)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- // CTestDlg 消息处理程序
- BOOL CTestDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // 将“关于...”菜单项添加到系统菜单中。
- // IDM_ABOUTBOX 必须在系统命令范围内。
- 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);
- }
- }
- // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
- // 执行此操作
- SetIcon(m_hIcon, TRUE); // 设置大图标
- SetIcon(m_hIcon, FALSE); // 设置小图标
- //初始化远程和本地两个列表框控件
- SetListCtrlStyle(&m_ListLocalFiles);
- SetListCtrlStyle(&m_ListRemoteFiles);
- AddHeaders(&m_ListLocalFiles);
- AddHeaders(&m_ListRemoteFiles);
- //本地文件夹以C盘为根目录
- BrowseFolder("c:",&m_ListLocalFiles,&m_LocalFinder,&m_ArrayLocalFiles);
- return TRUE; // 除非设置了控件的焦点,否则返回 TRUE
- }
- void CTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- else
- {
- CDialog::OnSysCommand(nID, lParam);
- }
- }
- // 如果向对话框添加最小化按钮,则需要下面的代码
- // 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
- // 这将由框架自动完成。
- void CTestDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // 用于绘制的设备上下文
- SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
- // 使图标在工作矩形中居中
- 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;
- // 绘制图标
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialog::OnPaint();
- }
- }
- //当用户拖动最小化窗口时系统调用此函数取得光标显示。
- HCURSOR CTestDlg::OnQueryDragIcon()
- {
- return static_cast<HCURSOR>(m_hIcon);
- }
- //设置列表控件CListCtrl的风格
- void CTestDlg::SetListCtrlStyle(CListCtrl* pLstCtrl)
- {
- //获得原有风格
- DWORD dwStyle = GetWindowLong(pLstCtrl->m_hWnd, GWL_STYLE);
- dwStyle &= ~(LVS_TYPEMASK);
- dwStyle &= ~(LVS_EDITLABELS);
- //设置新风格
- SetWindowLong(pLstCtrl->m_hWnd, GWL_STYLE, dwStyle | LVS_REPORT|LVS_NOLABELWRAP|LVS_SHOWSELALWAYS);
- //设置扩展风格
- DWORD styles = LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES;
- ListView_SetExtendedListViewStyleEx(pLstCtrl->m_hWnd, styles, styles );
- }
- //给列表控件CListCtrl增加标题头
- void CTestDlg::AddHeaders(CListCtrl* pLstCtrl)
- {
- TCHAR rgtsz[2][20] = {_T("文件/文件夹名"),
- _T("类型")};
- LV_COLUMN lvcolumn;
- CRect rect;
- pLstCtrl->GetWindowRect(&rect);
- for(int i=0;i<2;i++)
- {
- lvcolumn.mask = LVCF_FMT | LVCF_SUBITEM | LVCF_TEXT
- | LVCF_WIDTH | LVCF_ORDER;
- lvcolumn.fmt = LVCFMT_LEFT;
- lvcolumn.pszText = rgtsz[i];
- lvcolumn.iSubItem = i;
- lvcolumn.iOrder = i;
- if(i==0)
- {
- lvcolumn.cx = rect.Width()*2/3 ;
- }
- else
- lvcolumn.cx = rect.Width()/3 ;
- pLstCtrl->InsertColumn(i, &lvcolumn);
- }
- }
- //连接FTP服务器
- void CTestDlg::OnBtnConnectFTPServer()
- {
- UpdateData(TRUE);
- //新建CInternetSession对象
- m_pIntSession=new CInternetSession(AfxGetAppName(),1,PRE_CONFIG_INTERNET_ACCESS);
- try
- {
- //新建连接对象
- m_pFTPConnection=m_pIntSession->GetFtpConnection(m_StrFTPServer,m_StrUserName,m_StrPassword);
- }
- catch(CInternetException *pEx)
- {
- //获取错误信息
- TCHAR szError[1024];
- if(pEx->GetErrorMessage(szError,1024))
- AfxMessageBox(szError);
- else
- AfxMessageBox("出现意外情况");
- pEx->Delete();
- m_pFTPConnection=NULL;
- return;
- }
- m_pRemoteFinder = new CFtpFileFind(m_pFTPConnection);
- //获得服务器根目录的所有文件并在列表框中显示
- BrowseFolder("",&m_ListRemoteFiles,m_pRemoteFinder,&m_ArrayRemoteFiles);
- }
- //获得指定目录下的所有文件,并在列表框中显示
- void CTestDlg::BrowseFolder(CString strDir,CListCtrl* pLstCtrl,CFileFind* pFinder,CArray<FILEITEM,FILEITEM&>* pFilesArr)
- {
- //清空列表框
- pLstCtrl->DeleteAllItems();
- pFilesArr->RemoveAll();
- int nIndex = 0;
- CString szDir = strDir;
- if(szDir.Right(1) != "\")
- szDir += "\";
- szDir += "*.*";
- //查找目录下的文件
- BOOL res = pFinder->FindFile(szDir);
- while(res)
- {
- res = pFinder->FindNextFile();
- //是目录
- if(pFinder->IsDirectory() && !pFinder->IsDots())
- {
- FILEITEM fileitem;
- fileitem.nItem = nIndex;
- fileitem.bDir = TRUE;
- fileitem.StrFileName = pFinder->GetFileName();
- pFilesArr->Add(fileitem);
- AddItem(pLstCtrl,nIndex,TRUE,pFinder->GetFileName());
- }
- //是文件
- else if(!pFinder->IsDirectory() && !pFinder->IsDots())
- {
- FILEITEM fileitem;
- fileitem.nItem = nIndex;
- fileitem.bDir = FALSE;
- fileitem.StrFileName = pFinder->GetFileName();
- pFilesArr->Add(fileitem);
- AddItem(pLstCtrl,nIndex,FALSE,pFinder->GetFileName());
- }
- nIndex++;
- }
- }
- //给列表框增加一条记录
- void CTestDlg::AddItem(CListCtrl* pLstCtrl,int nIndex,BOOL bDir,CString strText)
- {
- LV_ITEM lvitem;
- int iActualItem;
- for(int iSubItem=0;iSubItem<2;iSubItem++)
- {
- lvitem.mask = LVIF_TEXT|(iSubItem == 0? LVIF_IMAGE : 0);
- lvitem.iItem = (iSubItem == 0)? nIndex : iActualItem;
- lvitem.iSubItem = iSubItem;
- switch(iSubItem)
- {
- case 0:
- lvitem.pszText =(LPTSTR)(LPCTSTR)strText;
- break;
- case 1:
- if(bDir)
- lvitem.pszText = "文件夹";
- else
- lvitem.pszText = "文件";
- break;
- }
- if (iSubItem == 0)
- iActualItem = pLstCtrl->InsertItem(&lvitem);
- else
- pLstCtrl->SetItem(&lvitem);
- }
- }
- //响应“下载”按钮
- void CTestDlg::OnBtnDownloadFile()
- {
- //如果没有建立连接,退出
- if(m_pFTPConnection==NULL)
- {
- MessageBox("没有连接FTP服务器!","信息提示",MB_OK);
- return;
- }
- //下载所有选中文件
- POSITION pos = m_ListRemoteFiles.GetFirstSelectedItemPosition();
- if (pos == NULL)
- AfxMessageBox("请选择要下载的文件");
- else
- {
- while (pos)
- {
- int nItem = m_ListRemoteFiles.GetNextSelectedItem(pos);
- DownLoadFile(m_ArrayRemoteFiles.GetAt(nItem));
- }
- }
- }
- //响应“上传”按钮
- void CTestDlg::OnBtnUpLoadFile()
- {
- //如果没有建立连接,退出
- if(m_pFTPConnection==NULL)
- {
- MessageBox("没有连接FTP服务器!","信息提示",MB_OK);
- return;
- }
- //上传所有选中文件
- POSITION pos = m_ListLocalFiles.GetFirstSelectedItemPosition();
- if (pos == NULL)
- AfxMessageBox("请选择要上传的文件");
- else
- {
- while (pos)
- {
- int nItem = m_ListLocalFiles.GetNextSelectedItem(pos);
- UpLoadFile(m_ArrayLocalFiles.GetAt(nItem));
- }
- }
- }
- //下载单个文件
- void CTestDlg::DownLoadFile(FILEITEM fileItem)
- {
- if(fileItem.bDir == TRUE)
- {
- AfxMessageBox("程序暂时不支持下载整个文件夹,请选择文件后下载");
- }
- else
- {
- //格式化文件名
- CString strLocalFile,strRemoteFile;
- strRemoteFile.Format("%s\%s",m_pRemoteFinder->GetRoot(),fileItem.StrFileName);
- strLocalFile.Format("%s\%s",m_LocalFinder.GetRoot(),fileItem.StrFileName);
- //下载
- if(m_pFTPConnection->GetFile(strLocalFile,strLocalFile))
- {
- CString strMsg;
- strMsg.Format("下载文件%s成功!",fileItem.StrFileName);
- AfxMessageBox(strMsg);
- }
- }
- }
- //上传单个文件
- void CTestDlg::UpLoadFile(FILEITEM fileItem)
- {
- if(fileItem.bDir == TRUE)
- {
- AfxMessageBox("程序暂时不支持上载整个文件夹,请选择文件后上传");
- }
- else
- {
- //格式化文件名
- CString strLocalFile,strRemoteFile;
- strRemoteFile.Format("%s\%s",m_pRemoteFinder->GetRoot(),fileItem.StrFileName);
- strLocalFile.Format("%s\%s",m_LocalFinder.GetRoot(),fileItem.StrFileName);
- //上传
- if(m_pFTPConnection->PutFile(strLocalFile,strLocalFile))
- {
- CString strMsg;
- strMsg.Format("上传文件%s成功!",fileItem.StrFileName);
- AfxMessageBox(strMsg);
- }
- }
- }
- //本地文件目录中向上一层
- void CTestDlg::OnLocalParentFolder()
- {
- if(m_StrLocalParentRoot=="")
- return;
- BrowseFolder(m_StrLocalParentRoot,&m_ListLocalFiles,&m_LocalFinder,&m_ArrayLocalFiles);
- }
- //本地文件目录中向下一层
- void CTestDlg::OnLocalChildFolder()
- {
- POSITION pos = m_ListLocalFiles.GetFirstSelectedItemPosition();
- if(pos)
- {
- int nItem = m_ListRemoteFiles.GetNextSelectedItem(pos);
- FILEITEM fileItem = m_ArrayLocalFiles.GetAt(nItem);
- if(fileItem.bDir==TRUE)
- {
- CString strDir;
- strDir.Format("%s\%s",m_LocalFinder.GetRoot(),fileItem.StrFileName);
- m_StrLocalParentRoot = m_LocalFinder.GetRoot();
- BrowseFolder(strDir,&m_ListLocalFiles,&m_LocalFinder,&m_ArrayLocalFiles);
- }
- }
- }
- //远程文件目录中向上一层
- void CTestDlg::OnRemoteParentFolder()
- {
- if(m_pFTPConnection==NULL)
- return;
- if(m_StrRemoteParentRoot=="")
- return;
- BrowseFolder(m_StrRemoteParentRoot,&m_ListRemoteFiles,m_pRemoteFinder,&m_ArrayRemoteFiles);
- }
- //远程文件目录中向下一层
- void CTestDlg::OnRemoteChildFolder()
- {
- POSITION pos = m_ListRemoteFiles.GetFirstSelectedItemPosition();
- if(pos)
- {
- int nItem = m_ListRemoteFiles.GetNextSelectedItem(pos);
- FILEITEM fileItem = m_ArrayRemoteFiles.GetAt(nItem);
- if(fileItem.bDir==TRUE)
- {
- CString strDir;
- strDir.Format("%s\%s",m_pRemoteFinder->GetRoot(),fileItem.StrFileName);
- m_StrRemoteParentRoot = m_pRemoteFinder->GetRoot();
- BrowseFolder(strDir,&m_ListRemoteFiles,m_pRemoteFinder,&m_ArrayRemoteFiles);
- }
- }
- }