📄 leftview.cpp
字号:
// LeftView.cpp : implementation of the CLeftView class
//
#include "stdafx.h"
#include "TextRead.h"
#include "TextReadDoc.h"
#include "LeftView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
TCHAR* MakeObjectDynamic( LPTSTR szData );
CString WindowName( CString sName );
extern CString m_strCurName;
/////////////////////////////////////////////////////////////////////////////
// CLeftView
IMPLEMENT_DYNCREATE(CLeftView, CTreeView)
BEGIN_MESSAGE_MAP(CLeftView, CTreeView)
//{{AFX_MSG_MAP(CLeftView)
ON_NOTIFY_REFLECT(TVN_ITEMEXPANDED, OnItemexpanded)
ON_NOTIFY_REFLECT(TVN_SELCHANGED, OnSelchanged)
ON_NOTIFY_REFLECT(TVN_ITEMEXPANDING, OnItemexpanding)
ON_WM_TIMER()
//}}AFX_MSG_MAP
// Standard printing commands
ON_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/destruction
CLeftView::CLeftView()
{
// TODO: add construction code here
}
CLeftView::~CLeftView()
{
}
BOOL CLeftView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CTreeView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CLeftView drawing
void CLeftView::OnDraw(CDC* pDC)
{
CTextReadDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CLeftView printing
BOOL CLeftView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CLeftView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CLeftView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
void CLeftView::OnInitialUpdate()
{
CTreeView::OnInitialUpdate();
static bool t=true;
if(!t)return;
t=false;
CTreeCtrl &theTree=GetTreeCtrl();
theTree.ModifyStyle(0,TVS_HASLINES|TVS_HASBUTTONS|TVS_LINESATROOT|TVS_SHOWSELALWAYS);
if( !m_ImageListTree.Create( IDB_FILE_TYPES, 17, 1, RGB( 255, 255, 255 ) ) )
TRACE( _T("Image list creation fault") );
theTree.SetImageList( &m_ImageListTree, TVSIL_NORMAL );
//
//List the local drives
//
for( TCHAR cLetter = _T('A'); cLetter <= _T('Z'); cLetter++ )
{
CString sDrive = cLetter;
sDrive += _T(":");
UINT nType = GetDriveType( sDrive );
if( DRIVE_REMOVABLE <= nType && nType <= DRIVE_RAMDISK )
InsertItem( TVI_ROOT, NULL, sDrive, nType );
}
//Network neighbour hood
m_hNetworkRoot = InsertItem( TVI_ROOT, NULL, _T("Network Neighborhood"), DRIVE_RAMDISK+1 );
}
/////////////////////////////////////////////////////////////////////////////
// CLeftView diagnostics
#ifdef _DEBUG
void CLeftView::AssertValid() const
{
CTreeView::AssertValid();
}
void CLeftView::Dump(CDumpContext& dc) const
{
CTreeView::Dump(dc);
}
CTextReadDoc* CLeftView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTextReadDoc)));
return (CTextReadDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CLeftView message handlers
HTREEITEM CLeftView::InsertItem( HTREEITEM hParent, NETRESOURCE *const pNetResource, CString sText, int iImage, int iImageSelected /* = -1 */ )
{
// TRACE( _T("CDlgGetPath::InsertItem(%p,%p,%s +++ )\n"), hParent, pNetResource, sText );
sText = WindowName( sText );
TVINSERTSTRUCT InsertStruct;
InsertStruct.hParent = hParent;
InsertStruct.hInsertAfter = TVI_LAST;
InsertStruct.itemex.mask = TVIF_IMAGE | TVIF_TEXT |// TVIF_CHILDREN |
TVIF_SELECTEDIMAGE | TVIF_PARAM;
if(sText.Find(":",0)>0)InsertStruct.itemex.mask|=TVIF_CHILDREN;
InsertStruct.itemex.pszText = sText.GetBuffer( sText.GetLength() ); sText.ReleaseBuffer();
InsertStruct.itemex.iImage = iImage;
InsertStruct.itemex.cChildren= 1;
InsertStruct.itemex.lParam = (LPARAM)pNetResource;
if( iImageSelected == -1 )
InsertStruct.itemex.iSelectedImage = iImage;
else
InsertStruct.itemex.iSelectedImage = iImageSelected;
return GetTreeCtrl().InsertItem( &InsertStruct );
}
bool CLeftView::PopulateTree( CString sPath, HTREEITEM hParent )
{
TRACE( _T("CDlgGetPath::PopulateTree( %s )\n"), sPath );
bool bGotChildren = false; //True if a child is added.
//
//Populate Network neighbourhood tree (Entire network and Local-Computers)
//
if( hParent == m_hNetworkRoot )
{
bGotChildren = EnumNetwork( hParent );
}
//
//Network item(Search deep into the network)
//
else if( GetTreeCtrl().GetItemData( hParent ) )
{
bGotChildren = EnumNetwork( hParent );
}
//
//Search for files and populate the CTreeCtrl
//
else
{
CFileFind finder;
BOOL bWorking = finder.FindFile( sPath+_T("*.*") );
while (bWorking)
{
bWorking = finder.FindNextFile();
if( finder.IsDots() )
continue;
CString str=finder.GetFileName();
if( finder.IsDirectory() || str.Find(":",0)>0 )
{
HTREEITEM hti=InsertItem( hParent, NULL, str, DRIVE_NO_ROOT_DIR, DRIVE_UNKNOWN );
bGotChildren = true;
TVITEM item = { 0 };
item.mask = TVIF_HANDLE | TVIF_CHILDREN;
item.hItem = hti;
item.cChildren = 1;
GetTreeCtrl().SetItem( &item );
}
else
{
if(str.Find(".",0)>str.GetLength()-5)
{
HTREEITEM hti=InsertItem( hParent, NULL, str, 15,15);
// bGotChildren = false;
TVITEM item = { 0 };
item.mask = TVIF_HANDLE & ~TVIF_CHILDREN;
item.hItem = hti;
item.cChildren = 0;
GetTreeCtrl().SetItem( &item );
}
}
}
}
//
//Remove the [+] if no children
//
if( !bGotChildren )
{
TVITEM item = { 0 };
item.mask = TVIF_HANDLE | TVIF_CHILDREN;
item.hItem = hParent;
item.cChildren = 0;
GetTreeCtrl().SetItem( &item );
}
return bGotChildren;
}
CString CLeftView::GetItemPath( HTREEITEM hItem )
{
TRACE( _T("CDlgGetPath::GetItemPath(%p)\n"), hItem );
CString sRet;
do
{
//End with a share name.
NETRESOURCE *const pNetResource = (NETRESOURCE *)(GetTreeCtrl().GetItemData( hItem ) );
if( pNetResource )
{
sRet = CString(pNetResource->lpRemoteName) + _T('\\')+ sRet;
break;
}
//Add the directory name to the path.
sRet = GetTreeCtrl().GetItemText( hItem ) + _T('\\')+ sRet;
hItem = GetTreeCtrl().GetParentItem( hItem );
} while( hItem );
return sRet;
}
TCHAR* MakeObjectDynamic( LPTSTR szData )
{
TRACE( _T("MakeObjectDynamic( %s )\n"), szData );
//Assume a NULL empty string
TCHAR * szRet = NULL;
int nLength = 0;
if( szData )
nLength = _tcslen( szData )+1;
if( nLength > 0 )
{
szRet = new TCHAR[nLength];
_tcscpy( szRet, szData );
}
return szRet;
}
CString WindowName( CString sName )
{
CString sRet;
//Scan all charactors to determine if their are any lower case items
for( int n = 0; n < sName.GetLength(); n++ )
{
TCHAR ch = sName[n];
if ((ch >= 'a') && (ch <= 'z') )
return sName;
}
sName.MakeLower();
if( sName.GetLength() > 0 )
{
CString sFirstChar = sName[0];
sFirstChar.MakeUpper();
sName = sFirstChar + sName.Mid( 1 );
}
return sName;
}
bool CLeftView::EnumNetwork( HTREEITEM hParent )
{
/*
TRACE( _T("CDlgGetPath::EnumNetwork( %p )\n"), hParent );
bool bGotChildren = false; //True if a child is added.
//Check if the item already has a network resource and use it.
NETRESOURCE *const pNetResource = (NETRESOURCE *)(GetTreeCtrl().GetItemData( hParent ) );
//
//Setup
//
DWORD dwResult;
HANDLE hEnum;
DWORD cbBuffer = 16384;
DWORD cEntries = 0xFFFFFFFF;
LPNETRESOURCE lpnrDrv;
DWORD i;
dwResult = WNetOpenEnum( pNetResource ? RESOURCE_GLOBALNET : RESOURCE_CONTEXT,
RESOURCETYPE_ANY,//RESOURCETYPE_DISK,
0,
pNetResource ? pNetResource : NULL,
&hEnum );
//Was the read sucessfull
if (dwResult != NO_ERROR)
{
TRACE( _T("*** ERROR %d - Cannot enumerate network drives.\n"), dwResult );
return false;
}
//
//Get items until no more remain.
//
do
{
lpnrDrv = (LPNETRESOURCE) GlobalAlloc( GPTR, cbBuffer );
dwResult = WNetEnumResource( hEnum, &cEntries, lpnrDrv, &cbBuffer );
if (dwResult == NO_ERROR)
{
//Scann through the results
for( i = 0; i < cEntries; i++ )
{
CString sNameRemote = lpnrDrv[i].lpRemoteName;
int nType = 9;
if( sNameRemote.IsEmpty() )
{
sNameRemote = lpnrDrv[i].lpComment;
nType = 8;
}
//
//Remove leading back slashes
//
if( sNameRemote.GetLength() > 0 && sNameRemote[0] == _T('\\') )
sNameRemote = sNameRemote.Mid( 1 );
if( sNameRemote.GetLength() > 0 && sNameRemote[0] == _T('\\') )
sNameRemote = sNameRemote.Mid( 1 );
//
//Display a share or the appropiate icon
//
if( lpnrDrv[i].dwDisplayType == RESOURCEDISPLAYTYPE_SHARE )
{
//Display only the share name
int nPos = sNameRemote.Find( _T('\\') );
if( nPos >= 0 )
sNameRemote = sNameRemote.Mid( nPos+1 );
InsertItem( hParent, NULL, sNameRemote, DRIVE_NO_ROOT_DIR, DRIVE_UNKNOWN );
}
else
{
NETRESOURCE* pResource = new NETRESOURCE;
*pResource = lpnrDrv[i];
pResource->lpLocalName = MakeObjectDynamic( pResource->lpLocalName );
pResource->lpRemoteName = MakeObjectDynamic( pResource->lpRemoteName );
pResource->lpComment = MakeObjectDynamic( pResource->lpComment );
pResource->lpProvider = MakeObjectDynamic( pResource->lpProvider );
InsertItem( hParent, pResource, sNameRemote, pResource->dwDisplayType+7 );
}
bGotChildren = true;
}
}
GlobalFree( (HGLOBAL) lpnrDrv );
if( dwResult != ERROR_NO_MORE_ITEMS )
{
TRACE( _T("*** ERROR %d - Cannot complete network drive enumeration\n"), dwResult );
break;
}
}
while( dwResult != ERROR_NO_MORE_ITEMS );
//
//Let go and go home
//
// WNetCloseEnum(hEnum);
return bGotChildren;
*/
return NULL;
}
void CLeftView::OnItemexpanded(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
// TODO: Add your control notification handler code here
TRACE( _T("CDlgGetPath::OnItemexpandingTree(%p)\n"), pNMHDR );
CWaitCursor CursorWaiting; //Show the wait cursor while expanding
//Only action 2 notifications
if( pNMTreeView->action == 2 )
{
//Update location display
CString sPath = GetItemPath( pNMTreeView->itemNew.hItem );
//Refresh children
if( !GetTreeCtrl().GetChildItem( pNMTreeView->itemNew.hItem ) )
{
PopulateTree( sPath, pNMTreeView->itemNew.hItem );
if( GetTreeCtrl().GetSelectedItem( ) != pNMTreeView->itemNew.hItem )
GetTreeCtrl().SelectItem( pNMTreeView->itemNew.hItem );
}
}
*pResult = 0;
}
void CLeftView::OnSelchanged(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
CTreeCtrl &theTree=GetTreeCtrl();
HTREEITEM hti=theTree.GetSelectedItem();
CString str;
while(hti)
{ str=theTree.GetItemText(hti)+str;
if(hti=theTree.GetParentItem(hti))
str="\\"+str;
}
if( str.Find(".",0)>0
/*
str.Find(".ini",0)>0||
str.Find(".txt",0)>0||
str.Find(".html",0)>0||
str.Find(".htm",0)>0||
str.Find(".sys",0)>0||
str.Find(".dat",0)>0||
str.Find(".bat",0)>0||
str.Find(".pdf",0)>0||
str.Find(".h",0)>0||
str.Find(".c",0)>0||
str.Find(".f",0)>0||
str.Find(".b",0)>0||
str.Find(".m",0)>0||
str.Find(".z",0)>0
*/
)
{
m_strCurName=str;
SetTimer(111,200,0L);
}
*pResult = 0;
}
void CLeftView::OnItemexpanding(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
CWaitCursor CursorWaiting;
CTreeCtrl &theTree=GetTreeCtrl();
//Only action 2 notifications
if( pNMTreeView->action == 2 )
{
//Update location display
CString sPath = GetItemPath( pNMTreeView->itemNew.hItem );
//Refresh children
if( !theTree.GetChildItem( pNMTreeView->itemNew.hItem ) )
{
PopulateTree( sPath, pNMTreeView->itemNew.hItem );
if( theTree.GetSelectedItem( ) != pNMTreeView->itemNew.hItem )
theTree.SelectItem( pNMTreeView->itemNew.hItem );
}
}
*pResult = 0;
}
void CLeftView::OnTimer(UINT nIDEvent)
{
if(nIDEvent==111)
{
CFile file;
if(file.Open(m_strCurName,CFile::modeReadWrite))
{ file.Close();
AfxGetApp()->GetMainWnd()->SetWindowText(m_strCurName);
AfxGetApp()->GetMainWnd()->PostMessage(WM_COMMAND,ID_FILE_OPEN,0L);
}
KillTimer(nIDEvent);
}
CTreeView::OnTimer(nIDEvent);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -