📄 ftpclientdlg.cpp
字号:
// FTPClientDlg.cpp : implementation file
//
#include "stdafx.h"
#include "FTPClient.h"
#include "FTPClientDlg.h"
#include "direct.h" //_chdir(...) required.
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define DIR 0
#define FILE 1
/////////////////////////////////////////////////////////////////////////////
// CFTPClientDlg dialog
CFTPClientDlg::CFTPClientDlg(CWnd* pParent /*=NULL*/)
: CDialog(CFTPClientDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CFTPClientDlg)
m_hostName = _T("172.16.0.36");
m_userName = _T("xxr");
m_userPsw = _T("123");
m_hostPort = 21;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_pDataSocket=0;
m_pFileSocket=0;
m_pControlSocket=0;
m_isConnected=false;
logined=false;
m_file.m_hFile=NULL;
m_state=STATE_LIST;
m_transfered=0;
}
void CFTPClientDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CFTPClientDlg)
DDX_Control(pDX, IDC_TRAN_PROGRESS, m_transportProgress);
DDX_Control(pDX, IDC_TREE_REMOTE, m_treeRemote);
DDX_Control(pDX, IDC_TREE_LOCAL, m_treeLocal);
DDX_Control(pDX, IDC_ACTIVITY_LIST, m_activityList);
DDX_Text(pDX, IDC_HOST_NAME, m_hostName);
DDX_Text(pDX, IDC_USER_NAME, m_userName);
DDX_Text(pDX, IDC_USER_PSW, m_userPsw);
DDX_Text(pDX, IDC_HOST_PORT, m_hostPort);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CFTPClientDlg, CDialog)
//{{AFX_MSG_MAP(CFTPClientDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_CONNECT_SERVER, OnConnectServer)
ON_NOTIFY(TVN_ITEMEXPANDING, IDC_TREE_LOCAL, OnItemexpandingTreeLocal)
ON_NOTIFY(TVN_SELCHANGED, IDC_TREE_LOCAL, OnSelchangedTreeLocal)
ON_NOTIFY(TVN_SELCHANGED, IDC_TREE_REMOTE, OnSelchangedRemoteTree)
ON_NOTIFY(TVN_ITEMEXPANDING, IDC_TREE_REMOTE, OnItemexpandingRemoteTree)
ON_BN_CLICKED(IDC_FILE_DOWNLOAD, OnDownloadFile)
ON_BN_CLICKED(IDC_FILE_UPLOAD, OnUploadFile)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFTPClientDlg message handlers
BOOL CFTPClientDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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
if(!m_imageList)
m_imageList.Create(IDB_FILE,16,16,RGB(0,255,0));
InitRootDirectory();
return TRUE; // return TRUE unless you set the focus to a control
}
// 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 CFTPClientDlg::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 CFTPClientDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CFTPClientDlg::AddActivity(LPCTSTR str)
{
int index=m_activityList.AddString(str);
m_activityList.SetCurSel(index);
}
void CFTPClientDlg::Disconnect()
{
if(m_isConnected)
{
m_pDataSocket->Close();
m_pDataSocket->Close();
delete m_pControlSocket;
delete m_pDataSocket;
m_pControlSocket=0;
m_pDataSocket=0;
m_isConnected=false;
}
}
//DEL void CFTPClientDlg::ProcessCommand()
//DEL {
//DEL
//DEL }
void CFTPClientDlg::OnConnectServer()
{
if(m_isConnected)
{
Disconnect();
}else
{
if(m_pControlSocket==0)
m_pControlSocket=new CControlSocket;
if(m_pControlSocket==0)
{
AddActivity("New CControlSocket failed.");
return;
}
int ret;
m_pControlSocket->Close();
ret=m_pControlSocket->Create();
if(ret==0)
{
AddActivity("m_pControlSocket->Create() failed.");
return;
}
UpdateData();
DWORD dwTickCount = GetTickCount() + 10000;
MSG msg;
while(!m_isConnected)
{
ret=m_pControlSocket->Connect(m_hostName,m_hostPort);
if(ret)
m_isConnected=true;
//Continue to process window message ;
while (PeekMessage(&msg,NULL,NULL,NULL,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if (dwTickCount < GetTickCount())
{
AddActivity("m_pControlSocket->Connect(m_hostName,m_hostPort) Timeout.");
m_isConnected=false;
return;
}
}
if(m_isConnected)
AddActivity("Connect FTP Server "+m_hostName+" succefully.");
else
AddActivity("m_pControlSocket->Connect(m_hostName,m_hostPort) failed.");
}
if(m_isConnected)
{
GetDlgItem(IDC_CONNECT_SERVER)->SetWindowText("Disconnect");
}else
{
GetDlgItem(IDC_CONNECT_SERVER)->SetWindowText("Connect");
}
if(m_isConnected)
{
if(m_pControlSocket->Login(m_userName,m_userPsw)==true)
InitRemoteDirectory();
else
{
CString promote="User "+m_userName+" not login.Confirm your username and password is valid.";
MessageBox(promote);
}
}
}
CFTPClientDlg::~CFTPClientDlg()
{
delete m_pControlSocket;
delete m_pDataSocket;
}
void CFTPClientDlg::InitRootDirectory()
{
CTreeCtrl &m_fileTreeCtrl=m_treeLocal;
HTREEITEM hItem,hti;
CStringArray DriverNameArray;
CString strDriverName;
CString str;
if (m_fileTreeCtrl.GetImageList(TVSIL_NORMAL) == NULL)
m_fileTreeCtrl.SetImageList(&m_imageList,TVSIL_NORMAL);
hItem = m_fileTreeCtrl.InsertItem(_T("我的电脑"),0,0,TVI_ROOT,TVI_LAST);
char szDriverName[512];
memset(szDriverName,0,sizeof(szDriverName));
DWORD nLength = GetLogicalDriveStrings(sizeof(szDriverName),szDriverName);
for (int i=0; i<(int)nLength; i++)
{
if (szDriverName[i] != '\0')
strDriverName += szDriverName[i];
else
{
strDriverName = strDriverName.Left(strDriverName.GetLength() - 1);
DriverNameArray.Add(strDriverName);
strDriverName = "";
}
}
UINT nDriverType;
char *szCurDir = NULL;
char szDriverLabel[512];
memset(szDriverLabel,0,sizeof(szDriverLabel));
for (i=0; i<DriverNameArray.GetSize(); i++)
{
nDriverType = GetDriveType((LPCTSTR)DriverNameArray.GetAt(i));
GetVolumeInformation((LPCTSTR)(DriverNameArray.GetAt(i) + "\\"),szDriverLabel,\
sizeof(szDriverLabel),NULL,NULL,0,NULL,0);
hti = m_fileTreeCtrl.InsertItem((CString)szDriverLabel + "(" + DriverNameArray.GetAt(i) + ")",\
nDriverType,nDriverType,hItem,TVI_LAST);
DisplayButton(hti);
memset(szDriverLabel,0,sizeof(szDriverLabel));
}
hti = m_fileTreeCtrl.GetRootItem();
m_fileTreeCtrl.SelectItem(hti);
m_fileTreeCtrl.Expand(hti,TVE_EXPAND);
}
void CFTPClientDlg::DisplayButton(HTREEITEM hti)
{
CTreeCtrl &m_fileTreeCtrl=m_treeLocal;
TVITEM tvi;
tvi.mask = TVIF_CHILDREN;
tvi.hItem = hti;
tvi.cChildren = 1;
m_fileTreeCtrl.SetItem(&tvi);
}
void CFTPClientDlg::DisplayButtonR(HTREEITEM hti)
{
CTreeCtrl &m_fileTreeCtrl=m_treeRemote;
TVITEM tvi;
tvi.mask = TVIF_CHILDREN;
tvi.hItem = hti;
tvi.cChildren = 1;
m_fileTreeCtrl.SetItem(&tvi);
}
void CFTPClientDlg::OnItemexpandingTreeLocal(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
HTREEITEM hti = pNMTreeView->itemNew.hItem;
CTreeCtrl &tree = m_treeLocal;
if (hti != tree.GetRootItem())
{
//清空儿子
HTREEITEM hChild = tree.GetChildItem(hti);
while (hChild)
{
tree.DeleteItem(hChild);
hChild = tree.GetChildItem(hti);
}
if (!GetSubSiblingItem(hti,GetCurrentSel(hti)))
HideButton(hti);
}
*pResult = 0;
}
BOOL CFTPClientDlg::GetSubSiblingItem(HTREEITEM hItem, LPCTSTR szCurDir)
{
CTreeCtrl &m_fileTreeCtrl=m_treeLocal;
BOOL bHaveChild = FALSE;
CString strEx;
HTREEITEM hti;
if (_chdir(szCurDir) != 0)
return bHaveChild;
HANDLE hFind;
WIN32_FIND_DATA wfd;
hFind = FindFirstFile(_T("*.*"),&wfd);
if(hFind == INVALID_HANDLE_VALUE) return bHaveChild;
if ((wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
&&!(wfd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN))
{
if( wfd.cFileName[0] != '.' ) //Normal direcotry.
{
bHaveChild = TRUE;
hti = m_fileTreeCtrl.InsertItem(wfd.cFileName,6,6,hItem,TVI_FIRST);
m_fileTreeCtrl.SetItemData(hti,DIR);
DisplayButton(hti);
_chdir(".."); // 查找完毕之后, 返回上一级目录
}
}
else
{
if (!(wfd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)) //Should be non-hidden files;
{
hti = m_fileTreeCtrl.InsertItem(wfd.cFileName,1,1,hItem,TVI_LAST);
m_fileTreeCtrl.SetItemData(hti,FILE);
bHaveChild = TRUE;
}
}
while(FindNextFile(hFind, &wfd))
{
if ((wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
!(wfd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN))
{
if(wfd.cFileName[0] != '.')
{
bHaveChild = TRUE;
hti = m_fileTreeCtrl.InsertItem(wfd.cFileName,6,6,hItem,TVI_FIRST);
m_fileTreeCtrl.SetItemData(hti,DIR);
DisplayButton(hti);
_chdir(".."); // 查找完毕之后, 返回上一级目录
}
}
else
{
if (!(wfd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN))
{
hti = m_fileTreeCtrl.InsertItem(wfd.cFileName,1,1,hItem,TVI_LAST);
m_fileTreeCtrl.SetItemData(hti,FILE);
bHaveChild = TRUE;
}
}
}
FindClose(hFind);
return bHaveChild;
}
CString CFTPClientDlg::GetCurrentSel(HTREEITEM hti)
{
CTreeCtrl &tree=m_treeLocal;
HTREEITEM hParent,hRoot;
CString strPath;
CString drive,tail;
hRoot=tree.GetRootItem();
hParent = tree.GetParentItem(hti);
if(!hParent)return "";
strPath = tree.GetItemText(hti);
while (hParent != hRoot) //Currunt isn't drive.
{
strPath = tree.GetItemText(hParent) + "\\" + strPath;
hParent = tree.GetParentItem(hParent);
}
strPath += "\\";
drive = strPath.Mid(strPath.Find(":") - 2 + 1,2);
tail = strPath.Right(strPath.GetLength() - strPath.Find(":") - 2);
strPath = drive + tail;
return strPath;
}
void CFTPClientDlg::OnSelchangedTreeLocal(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
HTREEITEM hti = pNMTreeView->itemNew.hItem;
if(!hti)return;
m_localPath=GetCurrentSel(hti);
m_localPath.TrimRight('\\');
GetDlgItem(IDC_LOCAL_PATH)->SetWindowText(m_localPath);
*pResult = 0;
}
void CFTPClientDlg::HideButton(HTREEITEM hti)
{
CTreeCtrl &m_fileTreeCtrl=m_treeLocal;
TVITEM tvi;
tvi.mask = TVIF_CHILDREN;
tvi.hItem = hti;
tvi.cChildren = 0;
m_fileTreeCtrl.SetItem(&tvi);
}
void CFTPClientDlg::HideButtonR(HTREEITEM hti)
{
CTreeCtrl &m_fileTreeCtrl=m_treeRemote;
TVITEM tvi;
tvi.mask = TVIF_CHILDREN;
tvi.hItem = hti;
tvi.cChildren = 0;
m_fileTreeCtrl.SetItem(&tvi);
}
void CFTPClientDlg::InitRemoteDirectory()
{
UpdateData();
CTreeCtrl &m_fileTreeCtrl=m_treeRemote;
m_fileTreeCtrl.DeleteAllItems();
HTREEITEM hItem,hti;
CStringArray DriverNameArray;
if (m_fileTreeCtrl.GetImageList(TVSIL_NORMAL) == NULL)
m_fileTreeCtrl.SetImageList(&m_imageList,TVSIL_NORMAL);
CString ftpHost="ftp://"+m_userName+"@"+m_hostName;
hItem = m_fileTreeCtrl.InsertItem(ftpHost,0,0,TVI_ROOT,TVI_LAST);
if(!(m_pControlSocket->GetListData("/")))
return ;
while(!m_listData.IsEmpty())
{
CString tmp=m_listData.GetHead();
m_listData.RemoveHead();
while(tmp.Replace(" "," "));
CString fName;
for(int i=0;i<=8;i++)
AfxExtractSubString(fName,tmp,i,' ');
if(tmp.Left(1)=='d') //Add fold to first;
{
hti = m_fileTreeCtrl.InsertItem(fName,6,6,hItem,TVI_FIRST);
m_fileTreeCtrl.SetItemData(hti,DIR);
DisplayButtonR(hti);
}else //Add files to last;
{
hti = m_fileTreeCtrl.InsertItem(fName,1,1,hItem,TVI_LAST);
m_fileTreeCtrl.SetItemData(hti,DIR);
HideButtonR(hti);
}
}
hti = m_fileTreeCtrl.GetRootItem();
m_fileTreeCtrl.SelectItem(hti);
m_fileTreeCtrl.Expand(hti,TVE_EXPAND);
}
void CFTPClientDlg::OnSelchangedRemoteTree(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
HTREEITEM hti = pNMTreeView->itemNew.hItem;
if(!hti)return;
m_remotePath=GetRemoteSel(hti);
m_remotePath.TrimRight('/');
GetDlgItem(IDC_REMOTE_PATH)->SetWindowText(m_remotePath);
*pResult = 0;
}
CString CFTPClientDlg::GetRemoteSel(HTREEITEM hti)
{
CTreeCtrl &tree=m_treeRemote;
HTREEITEM hParent,hRoot;
CString strPath;
// CString drive,tail;
hRoot=tree.GetRootItem();
hParent = tree.GetParentItem(hti);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -