?? myftpdlg.cpp
字號:
// 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);
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -