ClientFtpDlg.cpp
上传用户:tjfeida
上传日期:2013-03-10
资源大小:1917k
文件大小:19k
源码类别:

Ftp客户端

开发平台:

Visual C++

  1. // ClientFtpDlg.cpp : 实现文件
  2. //
  3. #include "stdafx.h"
  4. #include "ClientFtp.h"
  5. #include "ClientFtpDlg.h"
  6. #include ".clientftpdlg.h"
  7. #include "MyFtpClient.h"
  8. #include "MyFtpList.h"
  9. #include "afxinet.h"
  10. #include "wininet.h"
  11. #include "winsock2.h"
  12. #ifdef _DEBUG
  13. #define new DEBUG_NEW
  14. #endif
  15. #ifdef _DEBUG
  16. #define new DEBUG_NEW
  17. #undef THIS_FILE
  18. static char THIS_FILE[] = __FILE__;
  19. #endif
  20. char mLocalDirectory[MAX_PATH];
  21. char mServerDirectory[MAX_PATH];
  22. class CAboutDlg : public CDialog
  23. {
  24. public:
  25. CAboutDlg();
  26. // 对话框数据
  27. enum { IDD = IDD_ABOUTBOX };
  28. protected:
  29. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持
  30. // 实现
  31. protected:
  32. DECLARE_MESSAGE_MAP()
  33. };
  34. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  35. {
  36. }
  37. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  38. {
  39. CDialog::DoDataExchange(pDX);
  40. }
  41. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  42. END_MESSAGE_MAP()
  43. // CClientFtpDlg 对话框
  44. CClientFtpDlg::CClientFtpDlg(CWnd* pParent /*=NULL*/)
  45. : CDialog(CClientFtpDlg::IDD, pParent)
  46. , m_strPassWord(_T(""))
  47. , m_strServerAddress(_T(""))
  48. , m_strUserName(_T(""))
  49. , m_strMessage(_T(""))
  50. , m_nPort(0)
  51. , m_PASV(FALSE)
  52. , m_bHaveDotFlag(FALSE)
  53. , m_pDragImageList(NULL)
  54. , m_fileDirCount(0)
  55. {
  56. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  57. }
  58. void CClientFtpDlg::DoDataExchange(CDataExchange* pDX)
  59. {
  60. CDialog::DoDataExchange(pDX);
  61. DDX_Control(pDX, IDC_LIST_LOCAL_FILES, m_controlLocalFile);
  62. DDX_Control(pDX, IDC_LIST_SERVER_FILES, m_controlServerFile);
  63. DDX_Control(pDX, IDC_COMBO_LOCALDIR, m_controlLocalDir);
  64. DDX_Control(pDX, IDC_COMBO_SERVERDIR, m_controlServerDir);
  65. DDX_Text(pDX, IDC_EDIT_ADDRESS, m_strServerAddress);
  66. DDX_Text(pDX, IDC_EDIT_PASSWORD, m_strPassWord);
  67. DDX_Text(pDX, IDC_EDIT_USERNAME,m_strUserName);
  68. DDX_Text(pDX,IDC_EDIT_PORT,m_nPort);
  69. DDX_Text(pDX,IDC_EDIT_MASSAGE,m_strMessage);
  70.     DDX_Text(pDX,IDC_CHECK_ANONYMOUS,m_PASV);
  71. }
  72. BEGIN_MESSAGE_MAP(CClientFtpDlg, CDialog)
  73. ON_WM_SYSCOMMAND()
  74. ON_WM_PAINT()
  75. ON_WM_QUERYDRAGICON()
  76. ON_BN_CLICKED(IDC_CONNECT, OnButtonConnect)
  77. ON_NOTIFY(NM_DBLCLK, IDC_LIST_LOCAL_FILES, OnNMDblclkListLocalFiles)
  78. ON_NOTIFY(NM_DBLCLK, IDC_LIST_SERVER_FILES, OnNMDblclkListServerFiles)
  79. ON_CBN_SELCHANGE(IDC_COMBO_LOCALDIR, OnCbnSelchangeComboLocaldir)
  80. ON_MESSAGE(WM_SETFILE,CClientFtpDlg::OnInsertLocalFile)
  81. ON_MESSAGE(WM_SETREDRAWFLAG,CClientFtpDlg::OnSetRedrawFlag)
  82. ON_MESSAGE(WM_SETDIRECTORYDISPLAY,CClientFtpDlg::OnSetLocalDirDisplay)
  83. ON_MESSAGE(WM_REDISPLAYFILE,CClientFtpDlg::OnReDisplayLocalFile)
  84. ON_MESSAGE(WM_SETDIR,CClientFtpDlg::OnSetLocalDir)
  85. ON_BN_CLICKED(IDCANCEAL, OnBnClickedCanceal)
  86. END_MESSAGE_MAP()
  87. // CClientFtpDlg 消息处理程序
  88. BOOL CClientFtpDlg::OnInitDialog()
  89. {
  90. CDialog::OnInitDialog();
  91. // 将“关于...”菜单项添加到系统菜单中。
  92. // IDM_ABOUTBOX 必须在系统命令范围内。
  93. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  94. ASSERT(IDM_ABOUTBOX < 0xF000);
  95. CMenu* pSysMenu = GetSystemMenu(FALSE);
  96. if (pSysMenu != NULL)
  97. {
  98. CString strAboutMenu;
  99. strAboutMenu.LoadString(IDS_ABOUTBOX);
  100. if (!strAboutMenu.IsEmpty())
  101. {
  102. pSysMenu->AppendMenu(MF_SEPARATOR);
  103. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  104. }
  105. }
  106. // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
  107. //  执行此操作
  108. SetIcon(m_hIcon, TRUE); // 设置大图标
  109. SetIcon(m_hIcon, FALSE); // 设置小图标
  110. // TODO: 在此添加额外的初始化代码
  111. //创建ImageList
  112. HIMAGELIST himagelist;
  113. //定义文件信息变量
  114.      SHFILEINFO shfileinfo;
  115. himagelist=(HIMAGELIST)SHGetFileInfo("C:\",0,&shfileinfo,sizeof(shfileinfo),SHGFI_SYSICONINDEX|SHGFI_SMALLICON);
  116. //将文件图像列表存入
  117. m_ctImageList.m_hImageList=himagelist;
  118.     //设定客户端文件显示列表框的标题栏
  119. SetFileColumns(&m_controlLocalFile);
  120. //显示服务器端文件显示列表框的标题
  121. SetServerFileColumns(&m_controlServerFile);
  122. //设置文件头标信息
  123. SetImageList(&m_controlLocalFile,&m_controlLocalDir);
  124. SetImageList(&m_controlServerFile,NULL);
  125. m_controlLocalFile.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
  126. //获得本地驱动器有关信息,并显示文件信息
  127.     GetLocalDir();
  128. int i=m_controlLocalDir.FindStringExact(-1,"C:\");
  129. m_controlLocalDir.SetCurSel(i);
  130. ::SetCurrentDirectory("C:\");
  131.     GetLocalCurrentDir();
  132. BeginFindLocalFile();
  133.   //便于调试初始化的登录信息
  134. m_strServerAddress="211.69.196.53";
  135. m_nPort=21;
  136. m_strUserName="book";
  137. m_strPassWord="book";
  138. UpdateData(false);
  139.      return TRUE;  
  140. }
  141. void CClientFtpDlg::OnSysCommand(UINT nID, LPARAM lParam)
  142. {
  143. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  144. {
  145. CAboutDlg dlgAbout;
  146. dlgAbout.DoModal();
  147. }
  148. else
  149. {
  150. CDialog::OnSysCommand(nID, lParam);
  151. }
  152. }
  153. void CClientFtpDlg::OnPaint() 
  154. {
  155. if (IsIconic())
  156. {
  157. CPaintDC dc(this); // 用于绘制的设备上下文
  158. SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
  159. // 使图标在工作矩形中居中
  160. int cxIcon = GetSystemMetrics(SM_CXICON);
  161. int cyIcon = GetSystemMetrics(SM_CYICON);
  162. CRect rect;
  163. GetClientRect(&rect);
  164. int x = (rect.Width() - cxIcon + 1) / 2;
  165. int y = (rect.Height() - cyIcon + 1) / 2;
  166. // 绘制图标
  167. dc.DrawIcon(x, y, m_hIcon);
  168. }
  169. else
  170. {
  171. CDialog::OnPaint();
  172. }
  173. }
  174. //当用户拖动最小化窗口时系统调用此函数取得光标显示。
  175. HCURSOR CClientFtpDlg::OnQueryDragIcon()
  176. {
  177. return static_cast<HCURSOR>(m_hIcon);
  178. }
  179. void CClientFtpDlg::OnButtonConnect()
  180. {
  181. // TODO: Add your control notification handler code here
  182.    WORD wVersionRequested;
  183.    WSADATA wsaData;
  184.    wVersionRequested = MAKEWORD( 2, 2 );
  185.    WSAStartup( wVersionRequested, &wsaData );
  186.    
  187.    
  188. int index=0;
  189. CString str;
  190. UpdateData(TRUE);
  191. m_strMessage=m_strMessage+"连接服务器:"+m_strServerAddress+"rn";
  192. m_strMessage=m_strMessage+"登录用户名:"+m_strUserName+"rn";
  193. UpdateData(false);
  194.    
  195. if(!ftp.LogOntoServer(m_strServerAddress,m_nPort,m_strUserName,m_strPassWord,"","","","",1080,0)) 
  196. {
  197. m_strMessage=m_strMessage+m_strUserName+"登录失败。"+"rn";
  198. UpdateData(false);
  199. return;
  200. }
  201. else
  202. {
  203. ftp.List();
  204. while(ftp.GetLine(index))
  205. {
  206. AddLine(ftp.m_strLine);
  207. str="/";
  208. strcpy(mServerDirectory,str);
  209. SetServerDir(str);
  210. }
  211. ftp.FTPCommand("");
  212. }
  213. }
  214. void CClientFtpDlg::OnNMDblclkListLocalFiles(NMHDR *pNMHDR, LRESULT *pResult)
  215. {
  216. // TODO: Add your control notification handler code here
  217. NMLVDISPINFO* pLocInfo=(NMLVDISPINFO*)pNMHDR;
  218. CString str,str1,fileName,buffer;
  219. int iIcon,iIconSel;
  220. str.Format("%d",pLocInfo->item);
  221. if(str=="-1")
  222. return;
  223. int nItem=atoi(str);
  224. DWORD dwFlag=m_controlLocalFile.GetItemData(nItem);
  225. if(dwFlag==FILEICON)
  226. {
  227. str=mLocalDirectory;
  228.      
  229. m_controlLocalFile.GetItemText(nItem,0,mLocalDirectory,MAX_PATH);
  230. str1=mLocalDirectory;
  231. fileName=str1;
  232. if (str.Right(1)!="\")
  233. str1=str+"\"+str1;
  234. else
  235. str1=str+str1;
  236. strcpy(mLocalDirectory,str);
  237. MessageBox("确定要上载文件"+str1+"吗?");
  238. UpdateData(true);
  239. UpLoadFile(str1,fileName,m_PASV,false);
  240. return;
  241. }
  242. ::GetCurrentDirectory(MAX_PATH,mLocalDirectory);
  243. str=mLocalDirectory;
  244. m_controlLocalFile.GetItemText(nItem,0,mLocalDirectory,MAX_PATH);
  245. buffer=mLocalDirectory;
  246. if(str.Right(1)!="\")
  247. str+="\";
  248. str+=mLocalDirectory;
  249. GetFileIcon(buffer,&iIcon,&iIconSel);
  250. int i=InsertLocalDir(str,iIcon,iIconSel);
  251. m_controlLocalDir.SetCurSel(i);
  252. ::SetCurrentDirectory(str);
  253. ::GetCurrentDirectory(MAX_PATH,mLocalDirectory);
  254. BeginFindLocalFile();
  255. *pResult = 0;
  256. }
  257. void CClientFtpDlg::OnNMDblclkListServerFiles(NMHDR *pNMHDR, LRESULT *pResult)
  258. {
  259. // TODO: Add your control notification handler code here
  260. NMLVDISPINFO* pLocInfo=(NMLVDISPINFO*)pNMHDR;
  261. CString str,str1,str2,buffer;
  262. int pos1=0;
  263. str.Format("%d",pLocInfo->item);
  264. if(str=="-1")
  265. return;
  266. int nItem=atoi(str);
  267. DWORD dwFlag=m_controlServerFile.GetItemData(nItem);
  268. str2=m_controlServerFile.GetItemText(nItem,0);
  269. if(dwFlag==FILEICON)
  270. {
  271. str=mServerDirectory;
  272. if (str.Right (1)!="/")
  273. str+="/"+str2;
  274. else
  275. str+=str2;
  276. AfxMessageBox("下载文件:"+str2+"?");
  277. DownLoadFile(str,str2,m_PASV,true);
  278. return;
  279. }
  280. str=mServerDirectory;
  281. str1=mServerDirectory;
  282. if(str.Right (1)!="/")
  283. str+="/";
  284. str+=m_controlServerFile.GetItemText(nItem,0);
  285. str2=m_controlServerFile.GetItemText(nItem,0);
  286. if(!str2.CompareNoCase(".."))
  287. {
  288. if (str1=="/")
  289. return;
  290. else
  291. {
  292. m_controlServerFile.DeleteAllItems ();
  293. Back();
  294. pos1=str1.GetLength ()-1;
  295. while (pos1>=0)
  296. { if (str1.Mid (pos1,1)!="/")
  297. {
  298. pos1--;
  299. }
  300. else
  301. {
  302. str=str1.Mid (0,pos1+1);
  303. break;
  304. }
  305. }
  306. strcpy(mServerDirectory,str);
  307. SetServerDir(str);
  308. }
  309. }
  310. else
  311. {
  312. m_controlServerFile.DeleteAllItems ();
  313. CD(str);
  314. strcpy(mServerDirectory,str);
  315. SetServerDir(str);
  316. }
  317. *pResult = 0;
  318. }
  319. void CClientFtpDlg::OnCbnSelchangeComboLocaldir()
  320. {
  321. // TODO: Add your control notification handler code here
  322. CString str;
  323.     char lpBuffer[MAX_PATH];
  324. ::GetCurrentDirectory(MAX_PATH,lpBuffer);
  325. int i=m_controlLocalDir.GetCurSel();
  326. m_controlLocalDir.GetLBText(i,(LPTSTR)(LPCTSTR)str);
  327. i=0;
  328. while(1)
  329. {
  330. if(!SetCurrentDirectory(str))
  331. {
  332. i++;
  333. if(i<2)
  334. {
  335. AfxMessageBox("请插入磁盘重试");
  336. continue;
  337. }
  338. str=lpBuffer;
  339. if((i=m_controlLocalDir.FindStringExact(-1,str))!=-1)
  340. m_controlLocalDir.SetCurSel(i);
  341. else
  342. m_controlLocalDir.SetWindowText(str);
  343. }
  344. else
  345. {
  346. ::SetCurrentDirectory(str);
  347. GetLocalCurrentDir();
  348. BeginFindLocalFile();
  349. }
  350. break;
  351. }
  352. }
  353. void CClientFtpDlg::OnInsertFile(CListCtrl* pListCtrl, LPVOID pIn, LPARAM lParam)
  354. {
  355. FTP_FILE_INFO *pInfo=(FTP_FILE_INFO*)pIn;
  356. LVITEM lvItem;
  357. int iIcon,nItem,iIconSel;
  358. if(pInfo->n_Type==DIRECTORYICON)
  359. nItem=0;
  360. else
  361. nItem=pListCtrl->GetItemCount();
  362. CString fileName=pInfo->sFileName;;
  363. if((int)lParam==LOCFILE)
  364. GetFileIcon(fileName,&iIcon,&iIconSel);
  365. else
  366. iIcon=pInfo->n_Type;
  367. lvItem.mask=LVIF_TEXT|LVIF_IMAGE|LVIF_PARAM;
  368. lvItem.iImage=iIcon;
  369. lvItem.lParam=pInfo->n_Type;
  370. lvItem.pszText=pInfo->sFileName;
  371. lvItem.iSubItem=0;
  372. lvItem.iItem=nItem;
  373. int iPos=pListCtrl->InsertItem(&lvItem);
  374. lvItem.mask=LVIF_TEXT;
  375. lvItem.iItem=iPos;
  376. lvItem.pszText=pInfo->sFileDate;
  377. lvItem.iSubItem=1;
  378. pListCtrl->SetItem(&lvItem);
  379. //m_ctrlLocalFile.SetItem(&lvIt);
  380. lvItem.pszText=pInfo->sFileSize;
  381. lvItem.iSubItem=2;
  382. pListCtrl->SetItem(&lvItem);
  383. }
  384. void CClientFtpDlg::GetFileIcon(CString& fileName, int* iIcon, int* iIconSel)
  385. {
  386. CString str=fileName;
  387. if(str.Right(1)!="\")
  388. str+="\";
  389. SHFILEINFO shFi;
  390. if(!SHGetFileInfo(str,0,&shFi,sizeof(shFi),SHGFI_ICON|SHGFI_SMALLICON|SHGFI_DISPLAYNAME))
  391. {
  392. AfxMessageBox("打开文件图标出错");
  393. return;
  394. }
  395. *iIcon=shFi.iIcon;
  396. DestroyIcon(shFi.hIcon);
  397. if(iIconSel)
  398. {
  399. if(!SHGetFileInfo(str,0,&shFi,sizeof(shFi),SHGFI_ICON|SHGFI_SMALLICON|SHGFI_OPENICON|SHGFI_DISPLAYNAME))
  400. {
  401. AfxMessageBox("打开文件夹图标出错");
  402. return;
  403. }
  404. *iIconSel=shFi.iIcon;
  405. DestroyIcon(shFi.hIcon);
  406. }
  407. return ;
  408. }
  409. BOOL CClientFtpDlg::SetImageList(CListCtrl* pListCtrl, CComboBoxEx* pCombo)
  410. {
  411. pListCtrl->SetImageList(&m_ctImageList,LVSIL_SMALL);
  412. if(!pCombo)
  413. return TRUE;
  414. pCombo->SetImageList(&m_ctImageList);
  415. return TRUE;
  416. }
  417. void CClientFtpDlg::SetFileColumns(CListCtrl* pListCtrl)
  418. TCHAR rgtsz[3][10]={_T("文件名"),
  419.                 _T("日期"),
  420. _T("字节数")};
  421. LV_COLUMN lvcolumn;
  422. CRect rect;
  423. pListCtrl->GetWindowRect(&rect);
  424. for(int i=0;i<3;i++)
  425. {
  426. lvcolumn.mask=LVCF_FMT|LVCF_SUBITEM|LVCF_TEXT|LVCF_WIDTH|LVCF_ORDER;
  427. lvcolumn.fmt=LVCFMT_LEFT;
  428. lvcolumn.pszText=rgtsz[i];
  429. lvcolumn.iSubItem=i;
  430. lvcolumn.iOrder=i;
  431. if(i==0)
  432. {lvcolumn.cx=rect.Width()/3;
  433. }
  434. else
  435. lvcolumn.cx=rect.Width()/3;
  436. pListCtrl->InsertColumn(i,&lvcolumn);
  437. }
  438. }
  439. void CClientFtpDlg::GetLocalDir(void)
  440. {
  441. int iIcon,iIconSel;
  442. char LpBuffer[MAX_PATH];
  443. char* ptr;
  444. CString str;
  445. m_fileDirCount=0;
  446. ::GetLogicalDriveStrings(MAX_PATH,LpBuffer);
  447. ptr=LpBuffer;
  448. while(*ptr)
  449. {
  450. m_fileDirCount++;
  451. str=ptr;
  452. GetFileIcon(str,&iIcon,&iIconSel);
  453. InsertLocalDir(str,iIcon,iIconSel);
  454. ptr+=strlen(ptr)+1;
  455. }
  456. }
  457. int CClientFtpDlg::InsertLocalDir(CString& str, int iIcon, int iIconSel)
  458. {
  459. int nItem=m_controlLocalDir.GetCount();
  460. COMBOBOXEXITEM Combo;
  461. Combo.mask=CBEIF_IMAGE|CBEIF_TEXT|CBEIF_SELECTEDIMAGE;
  462. Combo.iImage=iIcon;
  463. Combo.iSelectedImage=iIconSel;
  464. Combo.pszText=(LPTSTR)(LPCTSTR)str;
  465. Combo.iItem=nItem;
  466. m_controlLocalDir.InsertItem(&Combo);
  467. return nItem;
  468. }
  469. void CClientFtpDlg::GetLocalCurrentDir(void)
  470. {
  471. CString str;
  472. m_controlLocalDir.GetWindowText(str);
  473. strcpy(mLocalDirectory,str);
  474. }
  475. BOOL CClientFtpDlg::BeginFindLocalFile(void)
  476. {
  477. m_controlLocalFile.DeleteAllItems();
  478. FindLocalFile();
  479. return TRUE;
  480. }
  481. void CClientFtpDlg::FindLocalFile(void)
  482. {
  483. FTP_FILE_INFO* pInfo=new FTP_FILE_INFO;
  484. CFileFind fileFind;
  485. UINT i=0;
  486. CTime time;
  487. BOOL bContinue;
  488. ::SetCurrentDirectory(mLocalDirectory);
  489. ::PostMessage(this->m_hWnd ,WM_SETDIRECTORYDISPLAY,0,0);
  490. bContinue=fileFind.FindFile("*.*");
  491. CString str;
  492. while(bContinue)
  493. {
  494. bContinue=fileFind.FindNextFile();
  495. if(fileFind.IsHidden())continue;
  496. str=fileFind.GetFileName();
  497. strcpy(pInfo->sFileName,str);
  498. fileFind.GetLastWriteTime(time);
  499. str=time.Format("%x");
  500. strcpy(pInfo->sFileDate,str);
  501. if(fileFind.IsDirectory())
  502. {
  503. strcpy(pInfo->sFileSize,"");
  504. pInfo->n_Type=DIRECTORYICON;
  505. ::SendMessage(this->m_hWnd,WM_SETFILE,(WPARAM)pInfo,
  506. (LPARAM)LOCFILE);
  507. }
  508. else
  509. {
  510. DWORD i=fileFind.GetLength();
  511. if(i>1024)
  512. {
  513. str.Format("%d",i/1024);
  514. str+="KB";
  515. }
  516. else
  517. str.Format("%d",i);
  518. strcpy(pInfo->sFileSize,str);
  519. pInfo->n_Type=FILEICON;
  520. ::SendMessage(this->m_hWnd,WM_SETFILE,(WPARAM)pInfo,
  521. (LPARAM)LOCFILE);
  522. }
  523. }
  524. ::SendMessage(this->m_hWnd,WM_SETREDRAWFLAG,0,0);
  525. delete pInfo;
  526. fileFind.Close();
  527. }
  528. void CClientFtpDlg::SetServerDir(CString str)
  529. {
  530. m_controlServerDir.SetWindowText(str);
  531. m_controlServerDir.Invalidate();
  532. }
  533. void CClientFtpDlg::AddLine(CString strLine)
  534. {
  535. MyFtpList* pItemList=new MyFtpList();
  536. if(!pItemList->ParseLine(strLine))
  537. {
  538. delete pItemList;
  539. return;
  540. }
  541. AddItem(pItemList);
  542. }
  543. BOOL CClientFtpDlg::AddItem(MyFtpList* pItem)
  544. {
  545. LV_ITEM lvitem;//存放所有列表项的变量
  546. lvitem.lParam = (LPARAM)pItem;
  547. lvitem.iImage = pItem->GetImage();
  548. lvitem.mask = LVIF_TEXT|LVIF_IMAGE|LVIF_PARAM;  
  549. int index = m_controlServerFile.GetItemCount ();
  550. lvitem.pszText = pItem->m_strName.GetBuffer(0);
  551. pItem->m_strName.ReleaseBuffer();
  552. lvitem.iItem = index;
  553. lvitem.iSubItem = 0;
  554. index=m_controlServerFile.InsertItem( &lvitem );
  555. lvitem.mask =LVIF_TEXT;
  556. lvitem.pszText = pItem->m_strDate.GetBuffer(0);
  557. pItem->m_strDate.ReleaseBuffer();
  558. lvitem.iItem = index;
  559. lvitem.iSubItem = 1;
  560. m_controlServerFile.SetItem( &lvitem );
  561. lvitem.pszText = pItem->m_strsst.GetBuffer(0);
  562. pItem->m_strsst.ReleaseBuffer();
  563. lvitem.iItem = index;
  564. lvitem.iSubItem = 2;
  565. m_controlServerFile.SetItem( &lvitem );
  566. if (pItem->GetImage()==7)
  567. m_controlServerFile.SetItemData(index,DIRECTORYICON);
  568. else
  569. m_controlServerFile.SetItemData(index,FILEICON);
  570. return (pItem != NULL);
  571. }
  572. void CClientFtpDlg::SetServerFileColumns(CListCtrl* pListCtrl)
  573. {   
  574. TCHAR rgtsz[3][10]={_T("文件名"),
  575.                 _T("日  期"),
  576. _T("权  限")};
  577. LV_COLUMN lvcolumn;
  578. CRect rect;
  579. pListCtrl->GetWindowRect(&rect);
  580. for(int i=0;i<3;i++)
  581. {
  582. lvcolumn.mask=LVCF_FMT|LVCF_SUBITEM|LVCF_TEXT|LVCF_WIDTH|LVCF_ORDER;
  583. lvcolumn.fmt=LVCFMT_LEFT;
  584. lvcolumn.pszText=rgtsz[i];
  585. lvcolumn.iSubItem=i;
  586. lvcolumn.iOrder=i;
  587. if(i==0)
  588. {lvcolumn.cx=rect.Width()/3;
  589. }
  590. else
  591. lvcolumn.cx=rect.Width()/3;
  592. pListCtrl->InsertColumn(i,&lvcolumn);
  593. }
  594. }
  595. void CClientFtpDlg::CD(CString dir)
  596. {
  597. int index=0;
  598. dir.TrimLeft();
  599. dir.TrimRight();
  600. CString cmd = "CWD " + dir;
  601. ftp.FTPCommand(cmd);
  602. ftp.FTPCommand("PWD");
  603. ftp.List();
  604. while(ftp.GetLine(index))
  605. {
  606. UpdateData(false);
  607. AddLine(ftp.m_strLine );
  608. }
  609. ftp.FTPCommand(""); 
  610. }
  611. void CClientFtpDlg::Back(void)
  612. {
  613. int index=0;
  614. ftp.FTPCommand("CDUP");
  615. ftp.List();
  616. while(ftp.GetLine(index))
  617. {
  618. UpdateData(false);
  619. AddLine(ftp.m_strLine );
  620. }
  621. ftp.FTPCommand(""); 
  622. }
  623. void CClientFtpDlg::UpLoadFile(CString LocalFilePath, CString LocalFileName, bool pasv, bool get)
  624. {
  625.     CString str;
  626. str=mServerDirectory;
  627. if (str.Right(1)!="/")
  628. str+="/"+LocalFileName;
  629. else
  630. str+=LocalFileName;
  631. ftp.MoveFile (str,LocalFilePath,pasv,get);
  632. }
  633. void CClientFtpDlg::DownLoadFile(CString ServerFilePath, CString ServerFileName, bool pasv, bool get)
  634. {
  635. CString str;
  636. str=mLocalDirectory;
  637. if (str.Right(1)!="\")
  638. str+="\"+ServerFileName;
  639. else
  640. str+=ServerFileName;
  641. ftp.MoveFile (ServerFilePath,str,pasv,get);
  642. }
  643. LRESULT CClientFtpDlg::OnReDisplayLocalFile(WPARAM wParam, LPARAM lParam)
  644. {
  645. CString str=(LPCTSTR)wParam;
  646. m_controlLocalDir.SetWindowText(str);
  647. m_controlLocalDir.Invalidate();
  648. GetLocalCurrentDir();
  649. if(!BeginFindLocalFile())
  650. AfxMessageBox("连接失败");
  651. return 0L;
  652. }
  653.  LRESULT CClientFtpDlg::OnSetLocalDirDisplay(WPARAM wParam, LPARAM lParam)
  654. {
  655. CString str;
  656. str=mLocalDirectory;
  657. m_controlLocalDir.SetWindowText(str);
  658. m_controlLocalDir.Invalidate();
  659. return 0L;
  660. }
  661. LRESULT CClientFtpDlg::OnInsertLocalFile(WPARAM wParam,LPARAM lParam)
  662. {
  663. FTP_FILE_INFO* pInfo=(FTP_FILE_INFO*)wParam;
  664. CString str=pInfo->sFileName;
  665. if(str=="."||str=="..")
  666. {
  667. m_bHaveDotFlag=TRUE;
  668. return 0L;
  669. }
  670. OnInsertFile(&m_controlLocalFile,pInfo,lParam);
  671. return 0L;
  672. }
  673.  LRESULT CClientFtpDlg::OnSetRedrawFlag(WPARAM wParam, LPARAM lParam)
  674. {  
  675. if(m_bHaveDotFlag)
  676. {
  677. CString str="..";
  678. int iIcon=DIRECTORYICON;
  679. m_controlLocalFile.InsertItem(0,str,iIcon);
  680. m_controlLocalFile.SetItemData(0,DIRECTORYICON);
  681. m_bHaveDotFlag=FALSE;
  682. }
  683. m_controlLocalFile.Invalidate();
  684. m_controlLocalFile.SetRedraw();
  685. return 0L;
  686. }
  687. LRESULT CClientFtpDlg::OnSetLocalDir(WPARAM wParam, LPARAM lParam)
  688. {
  689. CString str=(LPCTSTR)lParam;
  690. m_controlLocalDir.SetWindowText(str);
  691. m_controlLocalFile.SetRedraw(FALSE);
  692. m_controlLocalFile.DeleteAllItems();
  693. m_controlLocalFile.SetRedraw();
  694. return 0L;
  695. }
  696. void CClientFtpDlg::OnBnClickedCanceal()
  697. {
  698. // TODO: Add your control notification handler code here
  699.    ftp.CloseControlChannel();
  700.    UpdateData(TRUE);
  701.    m_strMessage="断开和服务器之间的连接!";
  702.    UpdateData(FALSE);
  703.    m_controlServerDir.SetWindowText("");
  704.     m_controlServerFile.DeleteAllItems();
  705.    
  706. }