用MFC实现简单资源管理器.doc_第1页
用MFC实现简单资源管理器.doc_第2页
用MFC实现简单资源管理器.doc_第3页
用MFC实现简单资源管理器.doc_第4页
用MFC实现简单资源管理器.doc_第5页
已阅读5页,还剩20页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

用Visual C+打造自己的资源管理器Windows的资源管理器想必大家都用过,该程序的窗口一分为二,左边的窗口显示本机当前所有驱动器以及驱动器中的所有文件夹,当用户单击文件夹后,如果该文件夹下面还有子文件夹,则上层文件夹展开显示下级的文件夹;否则,右边的窗口显示选择文件夹下的文件。那么这个程序是如何实现的呢?为了说明这个问题,本实例打造了一个简易的资源管理器,它实现了Windows资源管理器的主要功能,在显示文件的属性(如文件的文件名、文件的大小、文件的创建时间)的同时,还可以改变文件显示的方式,如大小图标方式、列表方式等。本实例代码编译运行后的界面效果如图一所示:一、 实现方法从上面的界面效果图可以看出,在程序中使用了拆分窗口,在拆分的过程中,左边窗口为CTreeView 类的子类CLeftView,右边的窗口为CListView类的子类CdriveExplorerView。窗口的拆分和CTreeView、CListView类不是本实例讲述的重点,相关的知识在本书的实例中都有介绍,读者朋友可以参阅上述内容及实例的原代码,这里主要介绍程序中一些具体的细节知识。 资源管理器中一个重要的问题是如何得到本机中的驱动器信息,微软提供的有关驱动器的API函数有GetLogicalDrives(),GetDriveType()。对于喜欢操作位和字节的汇编语言使用者来说,GetLogicalDrives()是个很好用的API函数。它以位掩码的形式返回逻辑驱动器。即在一个DWORD类型的返回值中,位0(最小的一位)表示驱动器A,位1表示驱动器B,以此类推。每一个位的状态如果是on,则表示对应的逻辑驱动器存在;否则状态为off,表示对应的逻辑驱动器不存在。大家知道DWORD是一个32位的值,足以包括所有的英文字母,也就是说最多可有26个盘符。 为了确定某个逻辑驱动器的类型,必须调用GetDriveType()函数。它以路径名作为参数(如C:/),返回DRIVE_FIXED,DRIVE_REMOVABLE,或DRIVE_UNKNOWN。下面列出了所有可能返回的值:这些值在winbase.h定义: #define DRIVE_UNKNOWN 0 / 无效路径名 #define DRIVE_NO_ROOT_DIR 1 / 无效路经,如无法找到的卷标 #define DRIVE_REMOVABLE 2 / 可移动驱动器(如磁盘驱动器,光驱等) #define DRIVE_FIXED 3 / 固定的驱动器 (如 通常的硬盘) #define DRIVE_REMOTE 4 / 网络驱动器 #define DRIVE_CDROM 5 / CD-ROM #define DRIVE_RAMDISK 6 / 随机存取(RAM) 磁盘 有了驱动器的信息,就可以使用FindFirstFile()、FindNextFile()等函数来获取驱动器下面的文件或文件夹信息(这部分),然后分别添加到树型视图和列表视图中。最后要说明的一点是需要根据不同的状态和文件类型在视图中显示不同的图表,这些可以通过设置列表视图的窗口风格、树状视图的项目条的图标来实现,具体参见代码部分。二、编程步骤1、 启动Visual C+6.0,生成一个单文档视图的应用程序,视图类的基类选择CListView,项目命名为DriveExplorer,同时在项目中添加图标资源、菜单和菜单响应函数(详细内容参见原代码);2、 使用Class Wizard为项目添加新类CLeftView类,其基类选择CtreeView;3、 添加代码,编译运行程序。三、程序代码/ LeftView.h : interface of the CLeftView class#if !defined(AFX_LEFTVIEW_H_29F66875_4E46_11D6_9693_B383368EF622_INCLUDED_)#define AFX_LEFTVIEW_H_29F66875_4E46_11D6_9693_B383368EF622_INCLUDED_#if _MSC_VER 1000#pragma once#endif / _MSC_VER 1000 class CDriveExplorerDoc;class CLeftView : public CTreeViewprotected: / create from serialization onlyCLeftView();DECLARE_DYNCREATE(CLeftView)/ Attributespublic:CDriveExplorerDoc* GetDocument();CImageList* m_pImageList;CString m_LocalPath;/ Operationspublic:BOOL HasSubdirectory(CString &strPathName);BOOL IsDriveNode(HTREEITEM hItem);void SetButtonState(HTREEITEM hItem, CString &strPathName);UINT AddDirectoryNodes(HTREEITEM hItem, CString &strPathName);BOOL IsMediaValid(CString &strPathName);HTREEITEM GetDriveNode(HTREEITEM hItem);UINT DeleteChildren(HTREEITEM hItem);BOOL IsPathValid(CString &strPathName);CString GetPathFromItem(HTREEITEM hItem);void AddDummyNode(HTREEITEM hItem);void InitTreeView(HTREEITEM hParent);BOOL AddDrives(CString strDrive, HTREEITEM hParent);/ Overrides/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CLeftView)public:virtual void OnDraw(CDC* pDC); / overridden to draw this viewvirtual BOOL PreCreateWindow(CREATESTRUCT& cs);protected:virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);virtual void OnInitialUpdate(); / called first time after construct/AFX_VIRTUAL/ Implementationpublic:virtual CLeftView();#ifdef _DEBUGvirtual void AssertValid() const;virtual void Dump(CDumpContext& dc) const;#endifprotected:/ Generated message map functionsprotected:/AFX_MSG(CLeftView)/ NOTE - the ClassWizard will add and remove member functions here./ DO NOT EDIT what you see in these blocks of generated code !afx_msg void OnDestroy();afx_msg void OnItemexpanding(NMHDR* pNMHDR, LRESULT* pResult);afx_msg void OnSelchanging(NMHDR* pNMHDR, LRESULT* pResult);/AFX_MSGDECLARE_MESSAGE_MAP();#ifndef _DEBUG / debug version in LeftView.cppinline CDriveExplorerDoc* CLeftView:GetDocument() return (CDriveExplorerDoc*)m_pDocument; #endif#endif/ CLeftView#include stdafx.h#include DriveExplorer.h#include DriveExplorerDoc.h#include LeftView.h#include DriveExplorerView.h#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif#define ILI_CDDRV 0#define ILI_CLSDFLD 1#define ILI_DRIVE 2 #define ILI_FLOPPYDRV 3 #define ILI_MYCOMP 4#define ILI_OPENFLD 5 #define ILI_TEXTFILE 6 #define MYCOMPUTER My ComputerIMPLEMENT_DYNCREATE(CLeftView, CTreeView)BEGIN_MESSAGE_MAP(CLeftView, CTreeView)/AFX_MSG_MAP(CLeftView)/ NOTE - the ClassWizard will add and remove mapping macros here./ DO NOT EDIT what you see in these blocks of generated code!ON_WM_DESTROY()ON_NOTIFY_REFLECT(TVN_ITEMEXPANDING, OnItemexpanding)ON_NOTIFY_REFLECT(TVN_SELCHANGING, OnSelchanging)/AFX_MSG_MAP/ Standard printing commandsON_COMMAND(ID_FILE_PRINT, CTreeView:OnFilePrint)ON_COMMAND(ID_FILE_PRINT_DIRECT, CTreeView:OnFilePrint)ON_COMMAND(ID_FILE_PRINT_PREVIEW, CTreeView:OnFilePrintPreview)END_MESSAGE_MAP()/ CLeftView construction/destructionCLeftView:CLeftView()/ TODO: add construction code hereCLeftView:CLeftView()BOOL CLeftView:PreCreateWindow(CREATESTRUCT& cs)/ TODO: Modify the Window class or styles here by modifying the CREATESTRUCT cscs.style |= TVS_HASBUTTONS | TVS_LINESATROOT | TVS_HASLINES;return CTreeView:PreCreateWindow(cs);void CLeftView:OnDraw(CDC* pDC)CDriveExplorerDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);/ TODO: add draw code for native data hereBOOL CLeftView:OnPreparePrinting(CPrintInfo* pInfo)/ default preparationreturn DoPreparePrinting(pInfo);void CLeftView:OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)/ TODO: add extra initialization before printingvoid CLeftView:OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)/ TODO: add cleanup after printingvoid CLeftView:OnInitialUpdate()CTreeView:OnInitialUpdate();m_pImageList = new CImageList();CWinApp* pApp = AfxGetApp();/ ASSERT(m_pImageList != NULL); / serious allocation failure checkingm_pImageList-Create(16, 16, ILC_COLOR8 | ILC_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-LoadIcon(ICO_OPENFLD);m_pImageList-Add(pApp-LoadIcon(ICO_TEXTFILE);GetTreeCtrl().SetImageList(m_pImageList , TVSIL_NORMAL);HTREEITEM hParent = GetTreeCtrl().InsertItem(MYCOMPUTER,ILI_MYCOMP, ILI_MYCOMP);InitTreeView(hParent);GetTreeCtrl().Expand(hParent, TVE_EXPAND); #ifdef _DEBUGvoid CLeftView:AssertValid() constCTreeView:AssertValid();void CLeftView:Dump(CDumpContext& dc) constCTreeView:Dump(dc);CDriveExplorerDoc* CLeftView:GetDocument() / non-debug version is inlineASSERT(m_pDocument-IsKindOf(RUNTIME_CLASS(CDriveExplorerDoc);return (CDriveExplorerDoc*)m_pDocument;#endif /_DEBUGvoid CLeftView:InitTreeView(HTREEITEM hParent)int nPos = 0;UINT nCount = 0;CString strDrive = ?:/;DWORD dwDriveList = :GetLogicalDrives ();CString cTmp;while (dwDriveList) if (dwDriveList & 1) cTmp = strDrive;strDrive.SetAt (0, 0x41 + nPos);if (AddDrives(strDrive , hParent)nCount+;dwDriveList = 1;nPos+;return;BOOL CLeftView:AddDrives(CString strDrive, HTREEITEM hParent)HTREEITEM hItem;UINT nType = :GetDriveType (LPCTSTR) strDrive);UINT nDrive = (UINT) strDrive0 - 0x41;switch (nType) case DRIVE_REMOVABLE:hItem = GetTreeCtrl().InsertItem(strDrive, ILI_FLOPPYDRV, ILI_FLOPPYDRV, hParent);AddDummyNode(hItem);break;case DRIVE_FIXED:hItem = GetTreeCtrl().InsertItem(strDrive, ILI_DRIVE, ILI_DRIVE, hParent);AddDummyNode(hItem);break;case DRIVE_REMOTE:hItem = GetTreeCtrl().InsertItem(strDrive, ILI_DRIVE, ILI_DRIVE, hParent);AddDummyNode(hItem);break;case DRIVE_CDROM:hItem = GetTreeCtrl().InsertItem(strDrive, ILI_CDDRV, ILI_CDDRV, hParent);AddDummyNode(hItem);break;case DRIVE_RAMDISK:hItem = GetTreeCtrl().InsertItem(strDrive, ILI_CDDRV, ILI_CDDRV, hParent);AddDummyNode(hItem);break;default:return FALSE;return true;void CLeftView:OnDestroy() CTreeView:OnDestroy();/ TODO: Add your message handler code hereif(m_pImageList != NULL)m_pImageList = NULL;delete m_pImageList;void CLeftView:AddDummyNode(HTREEITEM hItem)GetTreeCtrl().InsertItem (, 0, 0, hItem);CString CLeftView:GetPathFromItem(HTREEITEM hItem)CString strPathName;while (hItem != NULL) CString string = GetTreeCtrl().GetItemText (hItem);if (string.Right (1) != /) & !strPathName.IsEmpty ()string += /;strPathName = string + strPathName;hItem = GetTreeCtrl().GetParentItem (hItem);if(strPathName.Left(11) = MYCOMPUTER & strPathName.GetLength() 11)strPathName = strPathName.Mid(12);return strPathName;BOOL CLeftView:IsPathValid(CString &strPathName)if (strPathName.GetLength () = 3)return TRUE;HANDLE hFind;WIN32_FIND_DATA fd;BOOL bResult = FALSE;if (hFind = :FindFirstFile (LPCTSTR) strPathName, &fd) !=INVALID_HANDLE_VALUE) if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)bResult = TRUE;:CloseHandle (hFind);return bResult;BOOL CLeftView:IsMediaValid(CString &strPathName)/ Return TRUE if the drive doesnt support removable media.UINT nDriveType = GetDriveType (LPCTSTR) strPathName);if (nDriveType != DRIVE_REMOVABLE) & (nDriveType != DRIVE_CDROM)return TRUE;HTREEITEM CLeftView:GetDriveNode(HTREEITEM hItem)HTREEITEM hParent;do hParent = GetTreeCtrl().GetParentItem (hItem);if (hParent != NULL)hItem = hParent; while (hParent != NULL);return hItem;UINT CLeftView:DeleteChildren(HTREEITEM hItem)UINT nCount = 0;HTREEITEM hChild = GetTreeCtrl().GetChildItem (hItem);while (hChild != NULL) HTREEITEM hNextItem = GetTreeCtrl().GetNextSiblingItem (hChild);GetTreeCtrl().DeleteItem (hChild);hChild = hNextItem;nCount+;return nCount;UINT CLeftView:AddDirectoryNodes(HTREEITEM hItem, CString &strPathName)HANDLE hFind;WIN32_FIND_DATA fd;UINT nCount = 0;CString strFileSpec = strPathName;if (strFileSpec.Right (1) != /)strFileSpec += /;strFileSpec += *.*;if (hFind = :FindFirstFile (LPCTSTR) strFileSpec, &fd) =INVALID_HANDLE_VALUE) if (IsDriveNode (hItem)AddDummyNode (hItem);return 0;CWaitCursor wait;CDriveExplorerDoc* pDoc = GetDocument();pDoc-m_ExplorerView-DeleteAllItems();do if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) CString strFileName = (LPCTSTR) &fd.cFileName;if (strFileName != .) & (strFileName != .) & (fd.dwFileAttributes != 22)HTREEITEM hChild =GetTreeCtrl().InsertItem (LPCTSTR) &fd.cFileName,ILI_CLSDFLD , ILI_OPENFLD , hItem , TVI_SORT);CString strNewPathName = strPathName;if (strNewPathName.Right (1) != /)strNewPathName += /;strNewPathName += (LPCTSTR) &fd.cFileName;SetButtonState (hChild, strNewPathName);nCount+;elsepDoc-m_ExplorerView-AddToListView(&fd); while (:FindNextFile (hFind, &fd);:FindClose (hFind);return nCount;void CLeftView:SetButtonState(HTREEITEM hItem, CString &strPathName)if (HasSubdirectory (strPathName)AddDummyNode (hItem);BOOL CLeftView:HasSubdirectory(CString &strPathName)HANDLE hFind;WIN32_FIND_DATA fd;BOOL bResult = FALSE;CString strFileSpec = strPathName;if (strFileSpec.Right (1) != /)strFileSpec += /;strFileSpec += *.*;if (hFind = :FindFirstFile (LPCTSTR) strFileSpec, &fd) !=INVALID_HANDLE_VALUE)do if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)CString strFileName = (LPCTSTR) &fd.cFileName;if (strFileName != .) & (strFileName != .)bResult = TRUE; while (:FindNextFile (hFind, &fd) & !bResult);:FindClose (hFind);return bResult;BOOL CLeftView:IsDriveNode(HTREEITEM hItem)return (GetTreeCtrl().GetParentItem (hItem) = NULL) ? TRUE : FALSE;void CLeftView:OnItemexpanding(NMHDR* pNMHDR, LRESULT* pResult) NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;HTREEITEM hItem = pNMTreeView-itemNew.hItem;CString strPathName = GetPathFromItem (hItem);if (!IsMediaValid (strPathName) HTREEITEM hRoot = GetDriveNode (hItem);GetTreeCtrl().Expand (hRoot, TVE_COLLAPSE);DeleteChildren (hRoot);AddDummyNode (hRoot);*pResult = TRUE;return;/ Delete the item if strPathName no longer specifies a valid path.if (!IsPathValid (strPathName) if(strPathName != MYCOMPUTER & strPathName != )GetTreeCtrl().DeleteItem (hItem);*pResult = TRUE;return;CWaitCursor wait;if (pNMTreeView-action = TVE_EXPAND) if(strPathName != MYCOMPUTER)DeleteChildren (hItem);if (!AddDirectoryNodes (hItem, strPathName)*pResult = TRUE;else if(strPathName != MYCOMPUTER)DeleteChildren (hItem);if (IsDriveNode (hItem)AddDummyNode (hItem);elseSetButtonState (hItem, strPathName);m_LocalPath = strPathName;*pResult = 0;void CLeftView:OnSelchanging(NMHDR* pNMHDR, LRESULT* pResult) NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;/ TODO: Add your control notification handler code hereHTREEITEM hItem = pNMTreeView-itemNew.hItem;CString strPathName = GetPathFromItem (hItem);*pResult = FALSE;if(strPathName = MYCOMPUTER)return;CWaitCursor wait;if (!AddDirectoryNodes (hItem, strPathName)*pResult = TRUE;m_LocalPath = strPathName;*pResult = 0;/ DriveExplorerView.h : interface of the CDriveExplorerView class#if !defined(AFX_DRIVEEXPLORERVIEW_H_29F66873_4E46_11D6_9693_B383368EF622_INCLUDED_)#define AFX_DRIVEEXPLORERVIEW_H_29F66873_4E46_11D6_9693_B383368EF622_INCLUDED_#if _MSC_VER 1000#pragma once#endif / _MSC_VER 1000class CDriveExplorerView : public CListViewprotected: / create from serialization onlyCDriveExplorerView();DECLARE_DYNCREATE(CDriveExplorerView)/ Attributespublic:CDriveExplorerDoc* GetDocument();CImageList* m_pImageList;CImageList* m_pImageListL;/ Operationspublic:/ Overrides/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CDriveExplorerView)public:virtual void OnDraw(CDC* pDC); / overridden to draw this viewvirtual BOOL PreCreateWindow(CREATESTRUCT& cs);protected:virtual void OnInitialUpdate(); / called first time after constructvirtual BOOL OnPreparePrinting(CPrintInfo* pInfo);virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);/AFX_VIRTUAL/ Implementationpublic:void SetupImages(CImageList* mImageList, int iSize);UINT GetListViewIcon(CString s);CString GetFileType(CString s);LPTSTR GetNTS(CString cString);void AddToListView(WIN32_FIND_DATA* fd);void DeleteAllItems();virtual CDriveExplorerView();#ifdef _DEBUGvirtual void AssertValid() const;virtual void Dump(CDumpContext& dc) const;#endifprotected:/ Generated message map functionsprotected:/AFX_MSG(CDriveExplorerView)afx_msg void OnDestroy();afx_msg void OnSize(UINT nType, int cx, int cy);/AFX_MSGafx_msg void OnStyleChanged(int nStyleType, LPSTYLESTRUCT lpStyleStruct);DECLARE_MESSAGE_MAP();#ifndef _DEBUG / debug version in DriveExplorerView.cppinline CDriveExplorerDoc* CDriveExplorerView:GetDocument() return (CDriveExplorerDoc*)m_pDocument; #endif#endif/ DriveExplorerView.cpp : implementation of the CDriveExplorerView class#include stdafx.h#include DriveExplorer.h#include DriveExplorerDoc.h#include DriveExplorerView.h#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif#define ICI_ACCESSFILE 0#define ICI_C_SOURCE 1#define ICI_CDDRV 2#define ICI_CLSDFLD 3#define ICI_CURSORFILE 4#define ICI_DRIVE 5#define ICI_DRIVERSFILE 6#define ICI_ERROR 7#define ICI_EXCELFILE 8#define ICI_EXCLAMATION 9#define ICI_EXEFILE 10#define ICI_FLOPPYDRV 11#define ICI_FONTFILE 12#define ICI_FOXPROFILE 13#define ICI_GENERALFILE 14#define ICI_HEADERFILE 15#define ICI_HELPFILE 16#define ICI_HTMLDOC 17#define ICI_HTMLHELP 18#define ICI_IMAGEFILE 19#define ICI_INFO 20#define ICI_JAVABEAN 21#define ICI_JAVACLASSES 22#define ICI_JAVASOURCE 23#define ICI_MYCOMPUTER 24#define ICI_OPENFLD 25#define ICI_PDFFILE 26#define ICI_QUESTION 27#define ICI_REGISTRYFILE 28#define ICI_SETUPFILE 29#define ICI_SOUNDFILE 30#define ICI_TEXTFILE 31#define ICI_TRASHFILE 32#define ICI_UNINSTALLFILE 33#define ICI_VIDEOFILE 34#define ICI_WINDOWSFILE 35#define ICI_WORDDOC 36#define ICI_ZIPFILE 37#define ICI_CDUP 38/ CDriveExplorerViewIMPLEMENT_DYNCREATE(CDriveExplorerView, CListView)BEGIN_MESSAGE_MAP(CDriveExplorerView, CListView)/AFX_MSG_MAP(CDriveExplorerView)ON_WM_DESTROY()ON_WM_SIZE()/AFX_MSG_MAPEND_MESSAGE_MAP()CDriveExplorerView

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论