FileManageDlg.cpp
上传用户:cjw5120
上传日期:2022-05-11
资源大小:5032k
文件大小:10k
源码类别:

网络截获/分析

开发平台:

Visual C++

  1. // FileManageDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "MyClient.h"
  5. #include "FileManageDlg.h"
  6. #include "TcpTran.h"
  7. #include "comm.h"
  8. #include "shellapi.h"
  9. //#include <direct.h>
  10. #define DIR 0
  11. #define FILE 1
  12. #define ID_FILETREE 96367
  13. HTREEITEM m_hItem;
  14. #ifdef _DEBUG
  15. #define new DEBUG_NEW
  16. #undef THIS_FILE
  17. static char THIS_FILE[] = __FILE__;
  18. #endif
  19. CWinThread *pDriverInfoThread = NULL;
  20. CWinThread *pListDirThread              = NULL;
  21. //CWinThread *pSendFileThread             = NULL;
  22. /////////////////////////////////////////////////////////////////////////////
  23. // CFileManageDlg dialog
  24. CFileManageDlg *m_filemanageDlg = NULL;
  25. //DriverInfo线程
  26. UINT DriverInfoThread(LPVOID lparam)
  27. {
  28. CTcpTran m_tcptran;
  29. CFileManageDlg *pDlg=(CFileManageDlg *)lparam;
  30. if(pDlg->ClientSocket!=INVALID_SOCKET)
  31. {  
  32.     int ret;
  33. DRIVER driver;
  34. COMMAND m_command;
  35. m_command.wCmd = CMD_FILE_MANAGE;
  36. ret=m_tcptran.mysend(pDlg->ClientSocket,(char *)&m_command,sizeof(COMMAND),0,60);//获得盘符信息
  37.     pDlg->m_tree.DeleteAllItems();
  38.     pDlg->m_list.DeleteAllItems();
  39. while(ret>0)
  40. {   HTREEITEM hit;
  41. ret=m_tcptran.myrecv(pDlg->ClientSocket,(char *)&driver,sizeof(DRIVER),0,60,NULL,false);//接收目标数据
  42. if(ret<=0||driver.end)
  43. break;
  44.             //表示接收到数据
  45. if(driver.drivertype==DRIVE_FIXED)
  46. {
  47.             hit=pDlg->m_tree.InsertItem(driver.driver,0,0,TVI_ROOT,TVI_LAST);
  48. pDlg->m_tree.InsertItem(NULL,hit,TVI_LAST);
  49. }
  50. if(driver.drivertype==DRIVE_CDROM)
  51. {
  52.             hit=pDlg->m_tree.InsertItem(driver.driver,1,1,TVI_ROOT,TVI_LAST);
  53. }
  54. if(driver.drivertype==DRIVE_REMOVABLE)
  55. {
  56.             hit=pDlg->m_tree.InsertItem(driver.driver,2,2,TVI_ROOT,TVI_LAST);
  57. pDlg->m_tree.InsertItem(NULL,hit,TVI_LAST);
  58. }
  59. }
  60. }
  61. pDriverInfoThread = NULL;
  62. return 0;
  63. }
  64. UINT ListDirThread(LPVOID lparam)
  65. {
  66. CTcpTran m_tcptran;
  67. CFileManageDlg *pDlg=(CFileManageDlg *)lparam;
  68.     pDlg->DeleteTreeChildItem(pDlg->TreeNode);
  69. pDlg->m_list.DeleteAllItems();
  70. if(pDlg->ClientSocket!=INVALID_SOCKET)
  71. {
  72. pDlg->m_list.SetRedraw(FALSE);
  73.         pDlg->m_list.DrawSearch(TRUE);
  74. COMMAND m_command;
  75. memset(&m_command,0,sizeof(COMMAND));
  76. m_command.wCmd = CMD_FILE_GETSUBFILE;
  77.         strcpy(m_command.szCurDir , pDlg->chrPath );
  78. m_tcptran.mysend(pDlg->ClientSocket,(char *)&m_command,sizeof(COMMAND),0,60);//枚举文件
  79. FILEINFO fileinfo;
  80.     do
  81. {
  82.    m_tcptran.myrecv(pDlg->ClientSocket,(char *)&fileinfo,sizeof(FILEINFO),0,60,NULL,false);//接收目标数据
  83.    if(fileinfo.next ==0)
  84.   break;
  85.    HTREEITEM hit;
  86.    if(fileinfo.isdirectory ==1)
  87.    {
  88.     if(fileinfo.filename [0]=='.')
  89.     continue;
  90.     hit=pDlg->m_tree.InsertItem(fileinfo.filename,3,4,pDlg->TreeNode,TVI_LAST);
  91.     pDlg->m_tree.InsertItem(NULL,hit,TVI_LAST);
  92.    }
  93.    else
  94.    {
  95.    int iIcon;
  96.    CString icon;
  97.    icon.Format("%s",fileinfo.filename);
  98.    pDlg->GetFileIcon(icon,&iIcon);
  99.    int n =pDlg->m_list.GetItemCount();
  100.            pDlg->m_list.InsertItem(n,"",iIcon);
  101.        CString kb;
  102.    kb.Format("%dK",fileinfo.filesize);
  103.            pDlg->m_list.SetItemText(n,0,fileinfo.filename);
  104.    pDlg->m_list.SetItemText(n,1,kb);
  105.    pDlg->m_list.SetItemText(n,2,fileinfo.time);
  106.    }
  107. }while(fileinfo.invalidir!=1);
  108.         pDlg->PostMessage(STOP_MESSAGE);
  109. pDlg->m_list.SetRedraw(TRUE);
  110. pDlg->m_list.Invalidate();
  111. }
  112. pListDirThread = NULL;
  113. return 0;
  114. }
  115. CFileManageDlg::CFileManageDlg(CWnd* pParent /*=NULL*/)
  116. : CDialog(CFileManageDlg::IDD, pParent)
  117. {
  118. //{{AFX_DATA_INIT(CFileManageDlg)
  119. // NOTE: the ClassWizard will add member initialization here
  120. //}}AFX_DATA_INIT
  121. m_filemanageDlg = this ;
  122. }
  123. void CFileManageDlg::DoDataExchange(CDataExchange* pDX)
  124. {
  125. CDialog::DoDataExchange(pDX);
  126. //{{AFX_DATA_MAP(CFileManageDlg)
  127. DDX_Control(pDX, IDC_TREE, m_tree);
  128. DDX_Control(pDX, IDC_LIST, m_list);
  129. //}}AFX_DATA_MAP
  130. }
  131. BEGIN_MESSAGE_MAP(CFileManageDlg, CDialog)
  132. //{{AFX_MSG_MAP(CFileManageDlg)
  133. ON_MESSAGE(BEGIN_MESSAGE, OnBeginMsg)
  134. ON_MESSAGE(STOP_MESSAGE, OnStopMsg)
  135. ON_NOTIFY(NM_CLICK, IDC_TREE, OnClickTreeDir)
  136. ON_NOTIFY(NM_RCLICK, IDC_LIST, OnRclickList)
  137. ON_COMMAND(ID_MENUITEM_FILE_DEL, OnMenuitemFileDel)
  138. //}}AFX_MSG_MAP
  139. END_MESSAGE_MAP()
  140. /////////////////////////////////////////////////////////////////////////////
  141. // CFileManageDlg message handlers
  142. BOOL CFileManageDlg::OnInitDialog()
  143. {
  144. CDialog::OnInitDialog();
  145. m_ImageList1.Create(16,16,ILC_COLOR16|ILC_MASK,2,2);
  146. HICON hIcon0 = ::LoadIcon (::AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_FILE1));
  147. m_ImageList1.Add(hIcon0);
  148. HICON hIcon1 = ::LoadIcon (::AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_FILE2));
  149. m_ImageList1.Add(hIcon1);
  150. HICON hIcon2 = ::LoadIcon (::AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_FILE3));
  151. m_ImageList1.Add(hIcon2);
  152. HICON hIcon3 = ::LoadIcon (::AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_FILE4));
  153. m_ImageList1.Add(hIcon3);
  154. HICON hIcon4 = ::LoadIcon (::AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_FILE5));
  155. m_ImageList1.Add(hIcon4);
  156. m_tree.SetImageList(&m_ImageList1,TVSIL_NORMAL);//添加图标
  157. // TODO: Add extra initialization here
  158. LONG lStyle = m_list.SendMessage(LVM_GETEXTENDEDLISTVIEWSTYLE);
  159. lStyle |= LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_HEADERDRAGDROP;
  160. m_list.SendMessage(LVM_SETEXTENDEDLISTVIEWSTYLE, 0,(LPARAM)lStyle);
  161. LV_COLUMN lvc;
  162. lvc.mask = LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH /*| LVCF_FMT*/;
  163. m_list.DeleteAllItems();
  164.   m_list.InsertColumn(0,"文件名",LVCFMT_LEFT,130);  
  165.   m_list.InsertColumn(1,"文件大小",LVCFMT_LEFT,90);
  166.   m_list.InsertColumn(2,"修改时间",LVCFMT_LEFT,120);
  167. //备注:字符串结尾设置的问题 初始化时要更新
  168. //创建ImageList
  169.     HIMAGELIST hImageList;
  170. //定义文件信息变量
  171. SHFILEINFO shFi;
  172. //获取文件图标信息
  173. char cSysDir[100],dir[4]={0};
  174.     GetSystemDirectory(cSysDir,100);
  175. strncpy(dir,cSysDir,3);
  176.     //得到系统盘符
  177. hImageList = (HIMAGELIST)SHGetFileInfo(dir,0,&shFi,sizeof(shFi),
  178. SHGFI_SYSICONINDEX|SHGFI_SMALLICON);
  179. //将文件图象列表存入m_ctImageList
  180. m_ImageList2.m_hImageList = hImageList;
  181. m_list.SetImageList(&m_ImageList2,LVSIL_SMALL);//添加图标
  182. pDriverInfoThread = AfxBeginThread(DriverInfoThread, (LPVOID)this);
  183. return TRUE;
  184. }
  185. void CFileManageDlg::GetItPath(HTREEITEM hitem, char *path)
  186. {
  187. char tpath[MAX_PATH]="";
  188. char chrtmp[MAX_PATH]="";
  189. do{
  190. strcpy(chrtmp,m_tree.GetItemText(hitem));
  191. strcat(chrtmp,"\");
  192. strcat(chrtmp,tpath);
  193. strcpy(tpath,chrtmp);
  194. hitem=m_tree.GetParentItem(hitem);
  195. }while(hitem!=0);
  196. strcat(tpath,"*");
  197. strcpy(path,tpath);
  198. }
  199. void CFileManageDlg::DeleteTreeChildItem(HTREEITEM hit)
  200. {
  201. HTREEITEM hchild;
  202. hchild=m_tree.GetChildItem(hit);
  203. while(hchild!=0)
  204. {
  205. m_tree.DeleteItem(hchild);
  206. hchild=m_tree.GetChildItem(hit);
  207. }
  208. }
  209. void CFileManageDlg::GetFileIcon(CString &fileName, int *iIcon)
  210. {
  211. CString str=fileName;
  212. SHFILEINFO shfi;
  213. memset(&shfi,0,sizeof(shfi));
  214. if(!SHGetFileInfo(str,FILE_ATTRIBUTE_NORMAL,&shfi,sizeof(shfi),SHGFI_ICON|SHGFI_SMALLICON|SHGFI_USEFILEATTRIBUTES))
  215. {
  216. AfxMessageBox("@@@@");
  217. return;
  218. }
  219. *iIcon=shfi.iIcon;
  220. DestroyIcon(shfi.hIcon);
  221. return ;
  222. }
  223. void CFileManageDlg::OnBeginMsg(WPARAM wParam, LPARAM lParam)
  224. {
  225.      m_list.DrawSearch(TRUE);
  226. }
  227. void CFileManageDlg::OnStopMsg(WPARAM wParam, LPARAM lParam)
  228. {
  229.     m_list.DrawSearch(FALSE);
  230. }
  231. void CFileManageDlg::OnClickTreeDir(NMHDR* pNMHDR, LRESULT* pResult) 
  232. {
  233. // TODO: Add your control notification handler code here
  234. POINT ptMouse;
  235. GetCursorPos(&ptMouse);
  236. m_tree.ScreenToClient(&ptMouse);
  237. HTREEITEM TreeSel=m_tree.HitTest(ptMouse,0);
  238. if(!TreeSel)
  239. return;
  240. GetItPath(TreeSel,chrPath);//得到当前展开的路径
  241. TreeNode=TreeSel;
  242. CString str;
  243. str.Format("%s",chrPath);
  244. str=str.Left(str.ReverseFind('*'));
  245. GetDlgItem(IDC_CURRENT)->SetWindowText(str);
  246. if(pDriverInfoThread != NULL)
  247. {
  248.   ::TerminateThread(pDriverInfoThread->m_hThread, 0);
  249.   ::WaitForSingleObject(pDriverInfoThread->m_hThread, INFINITE);
  250.   CloseHandle(pDriverInfoThread->m_hThread);
  251.   pDriverInfoThread = NULL;
  252. }
  253.     if(pListDirThread != NULL)
  254. {
  255.   ::TerminateThread(pListDirThread->m_hThread, 0);
  256.   ::WaitForSingleObject(pListDirThread->m_hThread, INFINITE);
  257.   CloseHandle(pListDirThread->m_hThread);
  258.   pListDirThread = NULL;
  259. }
  260.     pListDirThread = AfxBeginThread(ListDirThread, (LPVOID)this);
  261. PostMessage(BEGIN_MESSAGE);
  262. *pResult = 0;
  263. }
  264. void CFileManageDlg::OnRclickList(NMHDR* pNMHDR, LRESULT* pResult) 
  265. {
  266. // TODO: Add your control notification handler code here
  267. POINT ptMouse;
  268. POINT ptClientMouse;
  269. GetCursorPos(&ptMouse);
  270. ptClientMouse=ptMouse;
  271. m_list.ScreenToClient(&ptClientMouse);
  272. int ListSel=m_list.HitTest(ptClientMouse,0);
  273. if(ListSel==-1)
  274. return;
  275. UpdateData(TRUE);
  276. CMenu menu;
  277. VERIFY(menu.LoadMenu(IDR_MENU_FILE));
  278. CMenu* pPopup = menu.GetSubMenu(0);
  279. ASSERT(pPopup != NULL);
  280. pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, ptMouse.x, ptMouse.y, this);
  281. *pResult = 0;
  282. }
  283. void CFileManageDlg::OnMenuitemFileDel() 
  284. {
  285.     char FilePath[MAX_PATH];
  286. CString path;
  287. GetDlgItem(IDC_CURRENT)->GetWindowText(path);
  288. strcpy(FilePath,path);
  289. POSITION pos =m_list.GetFirstSelectedItemPosition();
  290. int i=m_list.GetNextSelectedItem(pos);
  291. if(i>=0)//选择某项
  292. strcat(FilePath,m_list.GetItemText(i,0));
  293. // TODO: Add your command handler code here
  294.     CTcpTran m_tcptran;
  295. COMMAND m_command;
  296.     m_command.wCmd = CMD_FILE_DEL;
  297. strcpy(m_command.szCurDir,FilePath); 
  298. m_tcptran.mysend(ClientSocket,(char *)&m_command,sizeof(COMMAND),0,60);
  299. if(pDriverInfoThread != NULL)
  300. {
  301.   ::TerminateThread(pDriverInfoThread->m_hThread, 0);
  302.   ::WaitForSingleObject(pDriverInfoThread->m_hThread, INFINITE);
  303.   CloseHandle(pDriverInfoThread->m_hThread);
  304.   pDriverInfoThread = NULL;
  305. }
  306.    if(pListDirThread != NULL)
  307. {
  308.   ::TerminateThread(pListDirThread->m_hThread, 0);
  309.   ::WaitForSingleObject(pListDirThread->m_hThread, INFINITE);
  310.   CloseHandle(pListDirThread->m_hThread);
  311.   pListDirThread = NULL;
  312. }
  313. pListDirThread = AfxBeginThread(ListDirThread, (LPVOID)this);
  314. }