用MFC實現(xiàn)簡單資源管理器_第1頁
用MFC實現(xiàn)簡單資源管理器_第2頁
用MFC實現(xiàn)簡單資源管理器_第3頁
用MFC實現(xiàn)簡單資源管理器_第4頁
用MFC實現(xiàn)簡單資源管理器_第5頁
已閱讀5頁,還剩14頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、用VisualC+丁造自己的資源管理器Windows的資源管理器想必大家都用過,該程序的窗口一分為二,左邊的窗口顯示本機當前所有驅動器以及驅動器中的所有文件夾,當用戶單擊文件夾后,如果該文件夾下面還有子文件夾,則上層文件夾展開顯示下級的文件夾;否則,右邊的窗口顯示選擇文件夾下的文件。那么這個程序是如何實現(xiàn)的呢?為了說明這個問題,本實例打造了一個簡易的資源管理器,它實現(xiàn)了Windows資源管理器的主要功能,在顯示文件的屬性(如文件的文件名、文件的大小、文件的創(chuàng)建時間)的同時,還可以改變文件顯示的方式,如大小圖標方式、列表方式等。本實例代碼編譯運行后的界面效果如圖一所示:一、實現(xiàn)方法從上面的界面效

2、果圖可以看出,在程序中使用了拆分窗口,在拆分的過程中,左邊窗口為CTreeView類的子類CLeftView,右邊的窗口為CListView類的子類CdriveExplorerView。窗口的拆分和CTreeView、CListView類不是本實例講述的重點,相關的知識在本書的實例中都有介紹,讀者朋友可以參閱上述內容及實例的原代碼,這里主要介紹程序中一些具體的細節(jié)知識。資源管理器中一個重要的問題是如何得到本機中的驅動器信息,微軟提供的有關驅動器的API函數(shù)有GetLogicalDrives(),GetDriveType()。對于喜歡操作位和字節(jié)的匯編語言使用者來說,GetLogicalDriv

3、es()是個很好用的API函數(shù)。它以位掩碼的形式返回邏輯驅動器。即在一個DWORD類型的返回值中,位0(最小的一位)表示驅動器A,位1表示驅動器B,以此類推。每一個位的狀態(tài)如果是on,則表示對應的邏輯驅動器存在;否則狀態(tài)為off,表示對應的邏輯驅動器不存在。大家知道DWORD是一個32位的值,足以包括所有的英文字母,也就是說最多可有26個盤符。為了確定某個邏輯驅動器的類型,必須調用GetDriveType()函數(shù)。它以路徑名作為參數(shù)(如C:/),返回DRIVE_FIXEDDRIVE_REMOVABLE或DRIVE_UNKNOWN下面歹U出了所有可能返回的值:這些值在winbase.h定義:#

4、defineDRIVE_UNKNOWN0/無效路徑名# defineDRIVE_NO_ROOT_DIR1/無效路經(jīng),如無法找到的卷標# defineDRIVE_REMOVABLE2可移動驅動器(如磁盤驅動器,光驅等)# defineDRIVE_FIXED3/固定的驅動器(如通常的硬盤)# defineDRIVE_REMOTE4/網(wǎng)絡馬區(qū)動器# defineDRIVE_CDROM5/CD-ROM# defineDRIVE_RAMDISK6/隨機存取(RAM)磁盤有了驅動器的信息,就可以使用FindFirstFile()、FindNextFile()等函數(shù)來獲取驅動器下面的文件或文件夾信息(這部分

5、),然后分別添加到樹型視圖和列表視圖中。最后要說明的一點是需要根據(jù)不同的狀態(tài)和文件類型在視圖中顯示不同的圖表,這些可以通過設置列表視圖的窗口風格、樹狀視圖的項目條的圖標來實現(xiàn),具體參見代碼部分。二、編程步驟1、啟動VisualC+6.0,生成一個單文檔視圖的應用程序,視圖類的基類選擇CListView,項目命名為"DriveExplorer”,同時在項目中添加圖標資源、菜單和菜單響應函數(shù)(詳細內容參見原代碼);2、 使用ClassWizard為項目添加新類CLeftView類,其基類選擇CtreeView;3、添加代碼,編譯運行程序。三、程序代碼/LeftView.h:interfa

6、ceoftheCLeftViewclass#if!defined(AFX_LEFTVIEW_H_29F66875_4E46_11D6_9693_B383368EF622_INCLUDED_)#defineAFX_LEFTVIEW_H_29F66875_4E46_11D6_9693_B383368EF622_INCLUDED_#if_MSC_VER>1000#pragmaonce#endif/_MSC_VER>1000classCDriveExplorerDoc;classCLeftView:publicCTreeView(protected:/createfromserializa

7、tiononlyCLeftView();DECLARE_DYNCRTE(CLeftView)/Attributespublic:CDriveExplorerDoc*GetDocument();CImageList*m_pImageList;CStringm_LocalPath;/Operationspublic:BOOLHasSubdirectory(CString&strPathName);BOOLIsDriveNode(HTREEITEMhItem);voidSetButtonState(HTREEITEMhItem,CString&strPathName);UINTAdd

8、DirectoryNodes(HTREEITEMhItem,CString&strPathName);BOOLIsMediaValid(CString&strPathName);HTREEITEMGetDriveNode(HTREEITEMhItem);UINTDeleteChildren(HTREEITEMhItem);BOOLIsPathValid(CString&strPathName);CStringGetPathFromItem(HTREEITEMhItem);voidAddDummyNode(HTREEITEMhItem);voidInitTreeView(

9、HTREEITEMhParent);BOOLAddDrives(CStringstrDrive,HTREEITEMhParent);/Overrides/ClassWizardgeneratedvirtualfunctionoverrides/AFX_VIRTUAL(CLeftView)public:virtualvoidOnDraw(CDC*pDC);/overriddentodrawthisviewvirtualBOOLPreCreateWindow(CREATESTRUCT&cs);protected:virtualBOOLOnPreparePrinting(CPrintInfo

10、*pInfo);virtualvoidOnBeginPrinting(CDC*pDC,CPrintInfo*pInfo);virtualvoidOnEndPrinting(CDC*pDC,CPrintInfo*pInfo);virtualvoidOnInitialUpdate();/calledfirsttimeafterconstruct/AFX_VIRTUAL/Implementationpublic:virtualCLeftView();#ifdef_DEBUGvirtualvoidAssertValid()const;virtualvoidDump(CDumpContext&d

11、c)const;#endifprotected:/Generatedmessagemapfunctionsprotected:/AFX_MSG(CLeftView)/NOTE-theClassWizardwilladdandremovememberfunctionshere./DONOTEDITwhatyouseeintheseblocksofgeneratedcode!afx_msgvoidOnDestroy();afx_msgvoidOnItemexpanding(NMHDR*pNMHDR,LRESULT*pResult);afx_msgvoidOnSelchanging(NMHDR*pN

12、MHDR,LRESULT*pResult);/AFX_MSGDECLARE_MESSAGE_MAP();#ifndef_DEBUG/debugversioninLeftView.cppinlineCDriveExplorerDoc*CLeftView:GetDocument()return(CDriveExplorerDoc*)m_pDocument;#endif#endif/CLeftView#include"stda僅.h"#include"DriveExplorer.h"#include"DriveExplorerDoc.h"#

13、include"LeftView.h"#include"DriveExplorerView.h"#ifdef_DEBUG#definenewDEBUG_NEW#undefTHIS_FILEstaticcharTHIS_FILE口=_FILE_;#endif#defineILI_CDDRV0#defineILI_CLSDFLD1#defineILI_DRIVE2#defineILI_FLOPPYDRV3#defineILI_MYCOMP4#defineILI_OPENFLD5#defineILI_TEXTFILE6#defineMYCOMPUTER&quo

14、t;MyComputer"IMPLEMENT_DYNCREATE(CLeftVie,wCTreeView)BEGIN_MESSAGE_MAP(CLeftView,CTreeView)/AFX_MSG_MAP(CLeftView)/NOTE-theClassWizardwilladdandremovemappingmacroshere./DONOTEDITwhatyouseeintheseblocksofgeneratedcode!ON_WM_DESTROY()ON_NOTIFY_REFLECT(TVN_ITEMEXPANDING,OnItemexpanding)ON_NOTIFY_R

15、EFLECT(TVN_SELCHANGING,OnSelchanging)/AFX_MSG_MAP/StandardprintingcommandsON_COMMAND(ID_FILE_PRINT,CTreeView:OnFilePrint)ON_COMMAND(ID_FILE_PRINT_DIRECT,CTreeView:OnFilePrint)ON_COMMAND(ID_FILE_PRINT_PREVIEWCTreeView:OnFilePrintPreview)END_MESSAGE_MAP()/CLeftViewconstruction/destructionCLeftView:CLe

16、ftView()/TODO:addconstructioncodehereCLeftView:CLeftView()BOOLCLeftView:PreCreateWindow(CREATESTRUCT&cs)/TODO:ModifytheWindowclassorstylesherebymodifyingtheCREATESTRUCTcscs.style|=TVS_HASBUTTONS|TVS_LINESATROOT|TVS_HASLINES;returnCTreeView:PreCreateWindow(cs);voidCLeftView:OnDraw(CDC*pDC)(CDrive

17、ExplorerDoc*pDoc=GetDocument();ASSERT_VALID(pDoc);/TODO:adddrawcodefornativedatahereBOOLCLeftView:OnPreparePrinting(CPrintInfo*pInfo)(/defaultpreparationreturnDoPreparePrinting(pInfo);voidCLeftView:OnBeginPrinting(CDC*/*pDC*/,CPrintInfo*/*pInfo*/)(/TODO:addextrainitializationbeforeprintingvoidCLeftV

18、iew:OnEndPrinting(CDC*/*pDC*/,CPrintInfo*/*pInfo*/)(/TODO:addcleanupafterprintingvoidCLeftView:OnInitialUpdate()(CTreeView:OnInitialUpdate();m_pImageList=newCImageList();CWinApp*pApp=AfxGetApp();/ASSERT(m_pImageList!=NULL);/seriousallocationfailurecheckingm_pImageList->Create(16,16,ILC_COLOR8|ILC

19、_MASK,9,9);m_pImageList->Add(pApp->LoadIcon(ICO_CDDRV);m_pImageList->Add(pApp->LoadIcon(ICO_CLSDFLD);m_pImageList->Add(pApp->LoadIcon(ICO_DRIVE);m_pImageList->Add(pApp->LoadIcon(ICO_FLOPPYDRV);m_pImageList->Add(pApp->LoadIcon(ICO_MYCOMP);m_pImageList->Add(pApp->Lo

20、adIcon(ICO_OPENFLD);m_pImageList->Add(pApp->LoadIcon(ICO_TEXTFILE);GetTreeCtrl().SetImageList(m_pImageList,TVSIL_NORMAL);HTREEITEMhParent=GetTreeCtrl().InsertItem(MYCOMPUTER,ILI_MYCOMPLI_MYCOMP);InitTreeView(hParent);GetTreeCtrl().Expand(hParent,TVE_EXPAND);)#ifdef_DEBUGvoidCLeftView:AssertVal

21、id()constCTreeView:AssertValid();)voidCLeftView:Dump(CDumpContext&dc)constCTreeView:Dump(dc);)CDriveExplorerDoc*CLeftView:GetDocument()/non-debugversionisinlineASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDriveExplorerDoc);return(CDriveExplorerDoc*)m_pDocument;)#endif/_DEBUGvoidCLeftView:InitT

22、reeView(HTREEITEMhParent)intnPos=0;UINTnCount=0;CStringstrDrive="?:/"DWORDdwDriveList=:GetLogicalDrives();CStringcTmp;while(dwDriveList)if(dwDriveList&1)cTmp=strDrive;strDrive.SetAt(0,0x41+nPos);if(AddDrives(strDrive,hParent)nCount+;)dwDriveList>>=1;nPos+;)return;)BOOLCLeftView:A

23、ddDrives(CStringstrDrive,HTREEITEMhParent)HTREEITEMhItem;UINTnType=:GetDriveType(LPCTSTR)strDrive);UINTnDrive=(UINT)strDrive0-0x41;switch(nType)caseDRIVE_REMOVABLE:hItem=GetTreeCtrl().InsertItem(strDrive,ILI_FLOPPYDRV,ILI_FLOPPYDRV,hParent);AddDummyNode(hItem);break;caseDRIVE_FIXED:hItem=GetTreeCtrl

24、().InsertItem(strDrive,ILI_DRIVE,ILI_DRIVE,hParent);AddDummyNode(hItem);break;caseDRIVE_REMOTE:hItem=GetTreeCtrl().InsertItem(strDrive,ILI_DRIVE,ILI_DRIVE,hParent);AddDummyNode(hItem);break;caseDRIVE_CDROM:hItem=GetTreeCtrl().InsertItem(strDrive,ILI_CDDRV,ILI_CDDRV,hParent);AddDummyNode(hItem);break

25、;caseDRIVE_RAMDISK:hItem=GetTreeCtrl().InsertItem(strDrive,ILI_CDDRV,ILI_CDDRV,hParent);AddDummyNode(hItem);break;default:returnFALSE;returntrue;voidCLeftView:OnDestroy()CTreeView:OnDestroy();/TODO:Addyourmessagehandlercodehereif(m_pImageList!=NULL)m_pImageList=NULL;deletem_pImageList;voidCLeftView:

26、AddDummyNode(HTREEITEMhItem)GetTreeCtrl().InsertItem("",0,0,hItem);CStringCLeftView:GetPathFromItem(HTREEITEMhItem)(CStringstrPathName;while(hItem!=NULL)(CStringstring=GetTreeCtrl().GetItemText(hItem);if(string.Right(1)!="/")&&!strPathName.IsEmpty()string+="/"st

27、rPathName=string+strPathName;hItem=GetTreeCtrl().GetParentItem(hItem);)if(strPathName.Left(11)=MYCOMPUTER&&strPathName.GetLength()>11)strPathName=strPathName.Mid(12);returnstrPathName;)BOOLCLeftView:IsPathValid(CString&strPathName)(if(strPathName.GetLength()=3)returnTRUE;HANDLEhFind;W

28、IN32_FIND_DATAfd;BOOLbResult=FALSE;if(hFind=:FindFirstFile(LPCTSTR)strPathName,&fd)!=INVALID_HANDLE_VALUE)if(fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)bResult=TRUE;二CloseHandle(hFind);)returnbResult;)BOOLCLeftView:IsMediaValid(CString&strPathName)/ReturnTRUEifthedrivedoesn'tsuppor

29、tremovablemedia.UINTnDriveType=GetDriveType(LPCTSTR)strPathName);if(nDriveType!=DRIVE_REMOVABLE)&&(nDriveType!=DRIVE_CDROM)returnTRUE;)HTREEITEMCLeftView:GetDriveNode(HTREEITEMhItem)HTREEITEMhParent;dohParent=GetTreeCtrl().GetParentItem(hItem);if(hParent!=NULL)hItem=hParent;while(hParent!=NU

30、LL);returnhItem;UINTCLeftView:DeleteChildren(HTREEITEMhItem)UINTnCount=0;HTREEITEMhChild=GetTreeCtrl().GetChildItem(hItem);while(hChild!=NULL)HTREEITEMhNextItem=GetTreeCtrl().GetNextSiblingItem(hChild);GetTreeCtrl().DeleteItem(hChild);hChild=hNextItem;nCount+;returnnCount;UINTCLeftView:AddDirectoryN

31、odes(HTREEITEMhItem,CString&strPathName)HANDLEhFind;WIN32_FIND_DATAfd;UINTnCount=0;CStringstrFileSpec=strPathName;if(strFileSpec.Right(1)!="/")strFileSpec+="/"strFileSpec+="*.*"if(hFind=:FindFirstFile(LPCTSTR)strFileSpec,&fd)=INVALID_HANDLE_VALUE)if(IsDriveNode(

32、hItem)AddDummyNode(hItem);return0;CWaitCursorwait;CDriveExplorerDoc*pDoc=GetDocument();pDoc->m_ExplorerView->DeleteAllItems();doif(fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)CStringstrFileName=(LPCTSTR)&fd.cFileName;if(strFileName!=".")&&(strFileName!=".")

33、&&(fd.dwFileAttributes!=22)HTREEITEMhChild=GetTreeCtrl().InsertItem(LPCTSTR)&fd.cFileName,ILI_CLSDFLD,ILI_OPENFLD,hItem,TVI_SORT);CStringstrNewPathName=strPathName;if(strNewPathName.Right(1)!="/")strNewPathName+="/"strNewPathName+=(LPCTSTR)&fd.cFileName;SetButtonS

34、tate(hChild,strNewPathName);nCount+;)else(pDoc->m_ExplorerView->AddToListView(&fd);)while(二FindNextFile(hFind,&fd);:FindClose(hFind);returnnCount;voidCLeftView:SetButtonState(HTREEITEMhItem,CString&strPathName)(if(HasSubdirectory(strPathName)AddDummyNode(hItem);BOOLCLeftView:HasSub

35、directory(CString&strPathName)(HANDLEhFind;WIN32_FIND_DATAfd;BOOLbResult=FALSE;CStringstrFileSpec=strPathName;if(strFileSpec.Right(1)!="/")strFileSpec+="/"strFileSpec+="*.*"if(hFind=:FindFirstFile(LPCTSTR)strFileSpec,&fd)!=INVALID_HANDLE_VALUE)(doif(fd.dwFileAtt

36、ributes&FILE_ATTRIBUTE_DIRECTORY)CStringstrFileName=(LPCTSTR)&fd.cFileName;if(strFileName!=".")&&(strFileName!=".")bResult=TRUE;while(二FindNextFile(hFind,&fd)&&!bResult);:FindClose(hFind);)returnbResult;)BOOLCLeftView:IsDriveNode(HTREEITEMhItem)(return

37、(GetTreeCtrl().GetParentItem(hItem)=NULL)?TRUE:FALSE;)voidCLeftView:OnItemexpanding(NMHDR*pNMHDR,LRESULT*pResult)(NM_TREEVIEW*pNMTreeView=(NM_TREEVIEW*)pNMHDR;HTREEITEMhItem=pNMTreeView->itemNew.hItem;CStringstrPathName=GetPathFromItem(hItem);if(!IsMediaValid(strPathName)(HTREEITEMhRoot=GetDriveN

38、ode(hItem);GetTreeCtrl().Expand(hRoot,TVE_COLLAPSE);DeleteChildren(hRoot);AddDummyNode(hRoot);*pResult=TRUE;return;)/DeletetheitemifstrPathNamenolongerspecifiesavalidpath.if(!IsPathValid(strPathName)(if(strPathName!=MYCOMPUTER&&strPathName!="")(GetTreeCtrl().DeleteItem(hItem);*pRes

39、ult=TRUE;return;)CWaitCursorwait;if(pNMTreeView->action=TVE_EXPAND)(if(strPathName!=MYCOMPUTER)(DeleteChildren(hItem);if(!AddDirectoryNodes(hItem,strPathName)*pResult=TRUE;)elseif(strPathName!=MYCOMPUTER)DeleteChildren(hItem);if(IsDriveNode(hItem)AddDummyNode(hItem);elseSetButtonState(hItem,strPa

40、thName);m_LocalPath=strPathName;*pResult=0;voidCLeftView:OnSelchanging(NMHDR*pNMHDR,LRESULT*pResult)NM_TREEVIEW*pNMTreeView=(NM_TREEVIEW*)pNMHDR;/TODO:AddyourcontrolnotificationhandlercodehereHTREEITEMhItem=pNMTreeView->itemNew.hItem;CStringstrPathName=GetPathFromItem(hItem);*pResult=FALSE;if(str

41、PathName=MYCOMPUTER)return;CWaitCursorwait;if(!AddDirectoryNodes(hItem,strPathName)*pResult=TRUE;m_LocalPath=strPathName;*pResult=0;/DriveExplorerView.h:interfaceoftheCDriveExplorerViewclass#if!defined(AFX_DRIVEEXPLORERVIEW_H_29F66873_4E46_11D6_9693_B383368EF622_INCLUDED_)#defineAFX_DRIVEEXPLORERVIE

42、W_H_29F66873_4E46_11D6_9693_B383368EF622_INCLUDED_#if_MSC_VER>1000#pragmaonce#endif/_MSC_VER>1000classCDriveExplorerView:publicCListViewprotected:/createfromserializationonlyCDriveExplorerView();DECLARE_DYNCRETAE(CDriveExplorerView)/Attributespublic:CDriveExplorerDoc*GetDocument();CImageList*m_pImageList;CImageList*m_pImageListL;/Operationspublic:/Overrides/ClassWizardgeneratedvirtualfunctionoverridesAFX_VIRTUAL(C

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論