MyFtpDlg.cpp
上传用户:qzzxgm
上传日期:2009-12-14
资源大小:1882k
文件大小:12k
- // MyFtpDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "MyFtp.h"
- #include "MyFtpDlg.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()
- /////////////////////////////////////////////////////////////////////////////
- // CMyFtpDlg dialog
- CMyFtpDlg::CMyFtpDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CMyFtpDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CMyFtpDlg)
- m_strPassword = _T("");
- m_strServer = _T("");
- m_strUserName = _T("");
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- m_pInetSession = NULL;
- m_pFtpConnection = NULL;
- m_pRemoteFinder = NULL;
- m_strLocalParentRoot = m_strRemoteParentRoot = "";
- }
- void CMyFtpDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CMyFtpDlg)
- DDX_Control(pDX, IDC_LIST_REMOTE, m_ctrlRemoteFiles);
- DDX_Control(pDX, IDC_LIST_LOCAL, m_ctrlLocalFiles);
- DDX_Text(pDX, IDC_PASSWORD, m_strPassword);
- DDX_Text(pDX, IDC_SERVER, m_strServer);
- DDX_Text(pDX, IDC_USER, m_strUserName);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CMyFtpDlg, CDialog)
- //{{AFX_MSG_MAP(CMyFtpDlg)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(IDC_CONNECT, OnConnect)
- ON_BN_CLICKED(IDC_DOWNLOAD, OnDownload)
- ON_BN_CLICKED(IDC_LOCAL_UP, OnLocalUp)
- ON_BN_CLICKED(IDC_LOCAL_DOWND, OnLocalDownd)
- ON_BN_CLICKED(IDC_REMOTE_UP, OnRemoteUp)
- ON_BN_CLICKED(IDC_REMOTE_DOWND, OnRemoteDownd)
- ON_BN_CLICKED(IDC_UPLOAD, OnUpload)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CMyFtpDlg message handlers
- BOOL CMyFtpDlg::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
-
- //初始化两个列表框控件
- SetListCtrlStyle(&m_ctrlLocalFiles);
- SetListCtrlStyle(&m_ctrlRemoteFiles);
- AddHeaders(&m_ctrlLocalFiles);
- AddHeaders(&m_ctrlRemoteFiles);
-
- //本地文件目录以c盘为根目录
- BrowseDir("c:",&m_ctrlLocalFiles,&m_LocalFinder,&m_arrLocalFiles);
- return TRUE; // return TRUE unless you set the focus to a control
- }
- void CMyFtpDlg::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 CMyFtpDlg::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 CMyFtpDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- //设置列表控件CListCtrl的风格
- void CMyFtpDlg::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 CMyFtpDlg::AddHeaders(CListCtrl* pLstCtrl)
- {
- TCHAR rgtsz[2][10] = {_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 CMyFtpDlg::OnConnect()
- {
- UpdateData(TRUE);
- //新建对话
- m_pInetSession=new CInternetSession(AfxGetAppName(),1,PRE_CONFIG_INTERNET_ACCESS);
- try
- {
- //新建连接对象
- m_pFtpConnection=m_pInetSession->GetFtpConnection(m_strServer,m_strUserName,
- m_strPassword);
- }
- catch(CInternetException *pEx)
- {
- //获取错误
- TCHAR szError[1024];
- if(pEx->GetErrorMessage(szError,1024))
- AfxMessageBox(szError);
- else
- AfxMessageBox("There was an exception");
- pEx->Delete();
- m_pFtpConnection=NULL;
- return;
- }
- m_pRemoteFinder = new CFtpFileFind(m_pFtpConnection);
- //获得服务器根目录的所有文件并在列表框中显示
- BrowseDir("",&m_ctrlRemoteFiles,m_pRemoteFinder,&m_arrRemoteFiles);
- }
- //获得指定目录下的所有文件,并在列表框中显示
- void CMyFtpDlg::BrowseDir(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 CMyFtpDlg::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);
- }
-
- }
- //响应“download”按钮
- void CMyFtpDlg::OnDownload()
- {
- //如果没有建立连接,退出
- if(m_pFtpConnection==NULL)
- return;
- //下载所有选中文件
- POSITION pos = m_ctrlRemoteFiles.GetFirstSelectedItemPosition();
- if (pos == NULL)
- AfxMessageBox("请选择要下载的文件");
- else
- {
- while (pos)
- {
- int nItem = m_ctrlRemoteFiles.GetNextSelectedItem(pos);
- DownFile(m_arrRemoteFiles.GetAt(nItem));
- }
- }
- }
- //响应“Upload”按钮
- void CMyFtpDlg::OnUpload()
- {
- //如果没有建立连接,退出
- if(m_pFtpConnection==NULL)
- return;
- //上传所有选中文件
- POSITION pos = m_ctrlLocalFiles.GetFirstSelectedItemPosition();
- if (pos == NULL)
- AfxMessageBox("请选择要上传的文件");
- else
- {
- while (pos)
- {
- int nItem = m_ctrlLocalFiles.GetNextSelectedItem(pos);
- UpFile(m_arrLocalFiles.GetAt(nItem));
- }
- }
- }
- //下载单个文件
- void CMyFtpDlg::DownFile(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 CMyFtpDlg::UpFile(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 CMyFtpDlg::OnLocalUp()
- {
- if(m_strLocalParentRoot=="")
- return;
- BrowseDir(m_strLocalParentRoot,&m_ctrlLocalFiles,&m_LocalFinder,&m_arrLocalFiles);
- }
- //本地文件目录中向下一层
- void CMyFtpDlg::OnLocalDownd()
- {
- POSITION pos = m_ctrlLocalFiles.GetFirstSelectedItemPosition();
- if(pos)
- {
- int nItem = m_ctrlRemoteFiles.GetNextSelectedItem(pos);
- FILEITEM fileItem = m_arrLocalFiles.GetAt(nItem);
- if(fileItem.bDir==TRUE)
- {
- CString strDir;
- strDir.Format("%s\%s",m_LocalFinder.GetRoot(),fileItem.strFileName);
- m_strLocalParentRoot = m_LocalFinder.GetRoot();
- BrowseDir(strDir,&m_ctrlLocalFiles,&m_LocalFinder,&m_arrLocalFiles);
- }
- }
- }
- //远程文件目录中向上一层
- void CMyFtpDlg::OnRemoteUp()
- {
- if(m_pFtpConnection==NULL)
- return;
- if(m_strRemoteParentRoot=="")
- return;
- BrowseDir(m_strRemoteParentRoot,&m_ctrlRemoteFiles,m_pRemoteFinder,&m_arrRemoteFiles);
- }
- //远程文件目录中向下一层
- void CMyFtpDlg::OnRemoteDownd()
- {
- POSITION pos = m_ctrlRemoteFiles.GetFirstSelectedItemPosition();
- if(pos)
- {
- int nItem = m_ctrlRemoteFiles.GetNextSelectedItem(pos);
- FILEITEM fileItem = m_arrRemoteFiles.GetAt(nItem);
- if(fileItem.bDir==TRUE)
- {
- CString strDir;
- strDir.Format("%s\%s",m_pRemoteFinder->GetRoot(),fileItem.strFileName);
- m_strRemoteParentRoot = m_pRemoteFinder->GetRoot();
- BrowseDir(strDir,&m_ctrlRemoteFiles,m_pRemoteFinder,&m_arrRemoteFiles);
- }
- }
- }