explorerView.cpp
资源名称:explorer.rar [点击查看]
上传用户:jorden
上传日期:2022-08-09
资源大小:83k
文件大小:14k
源码类别:
TreeView控件
开发平台:
Visual C++
- // explorerView.cpp : implementation of the CExplorerView class
- //
- #include "stdafx.h"
- #include "explorer.h"
- #include "explorerDoc.h"
- #include "explorerView.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- ////////////////////////////////////////////////////////////////////////////
- // define my own data
- #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
- // define my own data
- /////////////////////////////////////////////////////////////////////////////
- /////////////////////////////////////////////////////////////////////////////
- // CExplorerView
- IMPLEMENT_DYNCREATE(CExplorerView, CListView)
- BEGIN_MESSAGE_MAP(CExplorerView, CListView)
- //{{AFX_MSG_MAP(CExplorerView)
- ON_WM_DESTROY()
- ON_WM_SIZE()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CExplorerView construction/destruction
- CExplorerView::CExplorerView()
- {
- // TODO: add construction code here
- }
- CExplorerView::~CExplorerView()
- {
- }
- BOOL CExplorerView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- cs.style |= LVS_REPORT;
- return CListView::PreCreateWindow(cs);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CExplorerView drawing
- void CExplorerView::OnDraw(CDC* pDC)
- {
- CExplorerDoc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
- CListCtrl& refCtrl = GetListCtrl();
- refCtrl.InsertItem(0, "Item!");
- // TODO: add draw code for native data here
- }
- void CExplorerView::OnInitialUpdate()
- {
- CListView::OnInitialUpdate();
- CExplorerDoc *pDoc = GetDocument();
- pDoc->m_ExplorerView = this;
- CRect rect;
- GetClientRect(&rect);
- GetListCtrl().InsertColumn(0, "File Name", LVCFMT_LEFT , rect.right / 2 , -1);
- GetListCtrl().InsertColumn(1, "Size", LVCFMT_LEFT , rect.right / 4 , -1);
- GetListCtrl().InsertColumn(2, "Date", LVCFMT_LEFT , rect.right / 4 , -1);
- m_pImageList = new CImageList();
- m_pImageListL = new CImageList();
- SetupImages(m_pImageList, 16);
- SetupImages(m_pImageListL, 32);
- GetListCtrl().SetImageList(m_pImageList, LVSIL_SMALL);
- GetListCtrl().SetImageList(m_pImageListL, LVSIL_NORMAL);
- // TODO: You may populate your ListView with items by directly accessing
- // its list control through a call to GetListCtrl().
- }
- /////////////////////////////////////////////////////////////////////////////
- // CDriveExplorerView printing
- BOOL CExplorerView::OnPreparePrinting(CPrintInfo* pInfo)
- {
- // default preparation
- return DoPreparePrinting(pInfo);
- }
- void CExplorerView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add extra initialization before printing
- }
- void CExplorerView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add cleanup after printing
- }
- /////////////////////////////////////////////////////////////////////////////
- // CExplorerView diagnostics
- #ifdef _DEBUG
- void CExplorerView::AssertValid() const
- {
- CListView::AssertValid();
- }
- void CExplorerView::Dump(CDumpContext& dc) const
- {
- CListView::Dump(dc);
- }
- CExplorerDoc* CExplorerView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CExplorerDoc)));
- return (CExplorerDoc*)m_pDocument;
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CExplorerView message handlers
- void CExplorerView::OnStyleChanged(int nStyleType, LPSTYLESTRUCT lpStyleStruct)
- {
- //TODO: add code to react to the user changing the view style of your window
- }
- void CExplorerView::DeleteAllItems()
- {
- GetListCtrl().DeleteAllItems();
- }
- void CExplorerView::AddToListView(WIN32_FIND_DATA *fd)
- {
- LV_ITEM lvitem;
- char sNumBuff[100];
- int iActualItem;
- CString sText;
- lvitem.mask = LVIF_TEXT | LVIF_IMAGE;
- lvitem.iItem = 0;
- lvitem.iSubItem = 0;
- lvitem.pszText = GetNTS(fd->cFileName); // fd.cFileName;
- lvitem.iImage = GetListViewIcon(fd->cFileName); //SetFileIcon(fd->GetFileName());
- iActualItem = GetListCtrl().InsertItem(&lvitem);
- // Add Attribute column
- lvitem.mask = LVIF_TEXT;
- lvitem.iItem = iActualItem;
- lvitem.iSubItem = 1;
- lvitem.pszText = GetNTS(GetFileType(fd->cFileName));
- GetListCtrl().SetItem(&lvitem);
- // Add Size column
- if(fd->nFileSizeLow != 0)
- ltoa((long)fd->nFileSizeLow,sNumBuff,10);
- else
- strcpy(sNumBuff,"");
- lvitem.mask = LVIF_TEXT;
- lvitem.iItem = iActualItem;
- lvitem.iSubItem = 2;
- lvitem.pszText = sNumBuff;
- GetListCtrl().SetItem(&lvitem);
- // Add Time column
- CTime refTime;
- refTime = fd->ftCreationTime;
- sText = refTime.Format( "%b-%d-%Y" );
- lvitem.mask = LVIF_TEXT;
- lvitem.iItem = iActualItem;
- lvitem.iSubItem = 3;
- lvitem.pszText = sText.GetBuffer(sText.GetLength());
- GetListCtrl().SetItem(&lvitem);
- }
- LPTSTR CExplorerView::GetNTS(CString cString)
- {
- LPTSTR lpsz = new TCHAR[cString.GetLength()+1];
- _tcscpy(lpsz, cString);
- return lpsz;
- }
- CString CExplorerView::GetFileType(CString s)
- {
- CString cResult;
- int nFoundAt = s.Find("." , 0);
- s.MakeUpper();
- if(nFoundAt == 0)
- return s + " File";
- CString ext = s.Mid(nFoundAt);
- if(ext == ".PDF") return "PDF File";
- if(ext == ".CPP") return "VC Source";
- if(ext == ".H") return "Header File";
- if(ext == ".C") return "C Source";
- else if(s == "SETUP.EXE") return "SETUP File";
- else if(s == "ISUNINST.EXE") return "UNINSTALL File";
- else if(ext == ".TXT" || ext == ".LOG") return "TEXT File";
- else if(ext == ".ZIP" || ext == ".ARJ" || ext == ".TGZ" ||
- ext == ".RAR" || ext == ".TAR" || ext == ".LZH" ||
- ext == ".LZA" || ext == ".CAB")
- return "ZIP File";
- else if(ext == ".EXE" || ext == ".COM" || ext == ".BAT")
- return "Application";
- else if(ext == ".AU" || ext == ".WAV" || ext == ".MP3")
- return "SOUND File";
- else if(ext == ".HTM" || ext == ".HTML") return "HTML Document";
- else if(ext == ".DOC") return "WORD Document";
- else if(ext == ".MPG" || ext == ".MPEG" || ext == ".AVI")
- return ICI_VIDEOFILE;
- else if(ext == ".HLP") return "HELP File";
- else if(ext == ".BMP") return "Bitmap File";
- else if(ext == ".ICO") return "Icon File";
- else if(ext == ".DBF" || ext == ".DBT" || ext == ".MEM")
- return "FOXPRO File";
- else if(ext == ".DLL" || ext == ".SYS" || ext == ".OCX" ||
- ext == ".VXD" || ext == ".INI" ||
- ext == ".DRV")
- return "DRIVERS File";
- else if(ext == ".JPG" || ext == ".GIF" || ext == ".JPEG" ||
- ext == ".AIS" || ext == ".EMF" || ext == ".IFF" ||
- ext == ".KDC" || ext == ".PCD" || ext == ".PCX" ||
- ext == ".PIC" || ext == ".PIX" || ext == ".PNG" ||
- ext == ".PSD" || ext == ".SGI" || ext == ".TIF" ||
- ext == ".TIFF" || ext == ".WMF")
- return "Picture File";
- else if(ext == ".TSK" || ext == ".CPX" || ext == ".ROM" ||
- ext == ".CFG" || ext == ".STF" || ext == ".LST" ||
- ext == ".INF")
- return "WINDOWS File";
- else if(ext == ".CUR" || ext == ".ANI") return "CURSOR File";
- else if(ext == ".FON") return "FONT File";
- else if(ext == ".BAK") return "Backup File";
- else if(ext == ".JAVA") return "JAVA Source";
- else if(ext == ".CLASS") return "JAVA Class";
- else if(ext == ".BEAN") return "JAVA Bean";
- else if(ext == ".REG") return "REGISTRY File";
- else if(ext == ".MDB") return "ACCESS Database";
- else if(ext == ".XLS") return "EXCEL File";
- else if(ext == ".CHM") return "HTML Help";
- else
- {
- ext = ext.Mid( (ext.Find('.',0) != -1 ? 1 : 0));
- return ext + " File";
- }
- return cResult;
- }
- UINT CExplorerView::GetListViewIcon(CString s)
- {
- int nFoundAt = s.Find("." , 1);
- s.MakeUpper();
- if(nFoundAt == 0)
- return ICI_GENERALFILE;
- CString ext = s.Mid(nFoundAt);
- // Set some standard icon types
- if(ext == ".PDF") return ICI_PDFFILE;
- else if(s == "SETUP.EXE") return ICI_SETUPFILE;
- else if(s == "ISUNINST.EXE") return ICI_UNINSTALLFILE;
- else if(ext == ".TXT" || ext == ".LOG") return ICI_TEXTFILE;
- else if(ext == ".ZIP" || ext == ".ARJ" || ext == ".TGZ" ||
- ext == ".RAR" || ext == ".TAR" || ext == ".LZH" ||
- ext == ".LZA" || ext == ".CAB")
- return ICI_ZIPFILE;
- else if(ext == ".EXE" || ext == ".COM" || ext == ".BAT")
- return ICI_EXEFILE;
- else if(ext == ".AU" || ext == ".WAV" || ext == ".MP3")
- return ICI_SOUNDFILE;
- else if(ext == ".HTM" || ext == ".HTML") return ICI_HTMLDOC;
- else if(ext == ".DOC") return ICI_WORDDOC;
- else if(ext == ".MPG" || ext == ".MPEG" || ext == ".AVI")
- return ICI_VIDEOFILE;
- else if(ext == ".HLP") return ICI_HELPFILE;
- else if(ext == ".BMP") return ICI_IMAGEFILE;
- else if(ext == ".ICO") return ICI_IMAGEFILE;
- else if(ext == ".DBF" || ext == ".DBT" || ext == ".MEM")
- return ICI_FOXPROFILE;
- else if(ext == ".DLL" || ext == ".SYS" || ext == ".OCX" ||
- ext == ".VXD" || ext == ".INI" ||
- ext == ".DRV")
- return ICI_DRIVERSFILE;
- else if(ext == ".JPG" || ext == ".GIF" || ext == ".JPEG" ||
- ext == ".AIS" || ext == ".EMF" || ext == ".IFF" ||
- ext == ".KDC" || ext == ".PCD" || ext == ".PCX" ||
- ext == ".PIC" || ext == ".PIX" || ext == ".PNG" ||
- ext == ".PSD" || ext == ".SGI" || ext == ".TIF" ||
- ext == ".TIFF" || ext == ".WMF")
- return ICI_IMAGEFILE;
- else if(ext == ".TSK" || ext == ".CPX" || ext == ".ROM" ||
- ext == ".CFG" || ext == ".STF" || ext == ".LST" ||
- ext == ".INF")
- return ICI_WINDOWSFILE;
- else if(ext == ".CUR" || ext == ".ANI") return ICI_CURSORFILE;
- else if(ext == ".FON") return ICI_FONTFILE;
- else if(ext == ".BAK") return ICI_TRASHFILE;
- else if(ext == ".JAVA") return ICI_JAVASOURCE;
- else if(ext == ".CLASS") return ICI_JAVACLASSES;
- else if(ext == ".BEAN") return ICI_JAVABEAN;
- else if(ext == ".REG") return ICI_REGISTRYFILE;
- else if(ext == ".MDB") return ICI_ACCESSFILE;
- else if(ext == ".XLS") return ICI_EXCELFILE;
- else if(ext == ".CHM") return ICI_HTMLHELP;
- else
- return ICI_GENERALFILE;
- }
- void CExplorerView::SetupImages(CImageList* mImageList, int iSize)
- {
- CWinApp* pApp = AfxGetApp();
- // ASSERT(m_pImageList != NULL); // serious allocation failure checking
- mImageList->Create(iSize, iSize, ILC_COLOR8 | ILC_MASK, 9, 9);
- mImageList->Add(pApp->LoadIcon(ICO_ACCESSFILE));
- mImageList->Add(pApp->LoadIcon(ICO_C_SOURCE));
- mImageList->Add(pApp->LoadIcon(ICO_CDDRV));
- mImageList->Add(pApp->LoadIcon(ICO_CLSDFLD));
- mImageList->Add(pApp->LoadIcon(ICO_CURSORFILE));
- mImageList->Add(pApp->LoadIcon(ICO_DRIVE));
- mImageList->Add(pApp->LoadIcon(ICO_DRIVERSFILE));
- mImageList->Add(pApp->LoadIcon(ICO_ERROR));
- mImageList->Add(pApp->LoadIcon(ICO_EXCELFILE));
- mImageList->Add(pApp->LoadIcon(ICO_EXCLAMATION));
- mImageList->Add(pApp->LoadIcon(ICO_EXEFILE));
- mImageList->Add(pApp->LoadIcon(ICO_FLOPPYDRV));
- mImageList->Add(pApp->LoadIcon(ICO_FONTFILE));
- mImageList->Add(pApp->LoadIcon(ICO_FOXPROFILE));
- mImageList->Add(pApp->LoadIcon(ICO_GENERALFILE));
- mImageList->Add(pApp->LoadIcon(ICO_HEADERFILE));
- mImageList->Add(pApp->LoadIcon(ICO_HELPFILE));
- mImageList->Add(pApp->LoadIcon(ICO_HTMLDOC));
- mImageList->Add(pApp->LoadIcon(ICO_HTMLHELP));
- mImageList->Add(pApp->LoadIcon(ICO_IMAGEFILE));
- mImageList->Add(pApp->LoadIcon(ICO_INFO));
- mImageList->Add(pApp->LoadIcon(ICO_JAVABEAN));
- mImageList->Add(pApp->LoadIcon(ICO_JAVACLASSES));
- mImageList->Add(pApp->LoadIcon(ICO_JAVASOURCE));
- mImageList->Add(pApp->LoadIcon(ICO_MYCOMP));
- mImageList->Add(pApp->LoadIcon(ICO_OPENFLD));
- mImageList->Add(pApp->LoadIcon(ICO_PDFFILE));
- mImageList->Add(pApp->LoadIcon(ICO_QUESTION));
- mImageList->Add(pApp->LoadIcon(ICO_REGISTRYFILE));
- mImageList->Add(pApp->LoadIcon(ICO_SETUPFILE));
- mImageList->Add(pApp->LoadIcon(ICO_SOUNDFILE));
- mImageList->Add(pApp->LoadIcon(ICO_TEXTFILE));
- mImageList->Add(pApp->LoadIcon(ICO_TRASHFILE));
- mImageList->Add(pApp->LoadIcon(ICO_UNINSTALLFILE));
- mImageList->Add(pApp->LoadIcon(ICO_VIDEOFILE));
- mImageList->Add(pApp->LoadIcon(ICO_WINDOWSFILE));
- mImageList->Add(pApp->LoadIcon(ICO_WORDDOC));
- mImageList->Add(pApp->LoadIcon(ICO_ZIPFILE));
- mImageList->Add(pApp->LoadIcon(ICO_CDUP));
- return ;
- }
- void CExplorerView::OnDestroy()
- {
- CListView::OnDestroy();
- // TODO: Add your message handler code here
- if(m_pImageList != NULL)
- m_pImageList = NULL;
- if(m_pImageListL != NULL)
- m_pImageListL = NULL;
- delete m_pImageList;
- delete m_pImageListL;
- }
- void CExplorerView::OnSize(UINT nType, int cx, int cy)
- {
- CListView::OnSize(nType, cx, cy);
- // TODO: Add your message handler code here
- if(GetListCtrl().GetSafeHwnd())
- {
- GetListCtrl().SetColumnWidth(0, cx / 2);
- GetListCtrl().SetColumnWidth(1, cx / 4);
- GetListCtrl().SetColumnWidth(2, cx / 4);
- }
- }