⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 explorerlistview.cpp

📁 加密工具
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// ExplorerListView.cpp : implementation file
//

#include "stdafx.h"
#include "CMyExplorer.h"
#include "ExplorerListView.h"
#include "ShellContextMenu.h"
#include "ExplorerTreeView.h"
#include "ShellFileOp.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


CExplorerListView * g_ListView = NULL;
extern CExplorerTreeView * g_TreeView;
/////////////////////////////////////////////////////////////////////////////
// CExplorerListView

IMPLEMENT_DYNCREATE(CExplorerListView, CListView)

CExplorerListView::CExplorerListView()
{
	m_pListCtrl = &GetListCtrl();

	m_dViewType = LVS_LIST;

	g_ListView = this;
}

CExplorerListView::~CExplorerListView()
{
	//m_pListCtrl->SetImageList(NULL,LVSIL_SMALL);
	//m_pListCtrl->SetImageList(NULL,LVSIL_NORMAL);
}


BEGIN_MESSAGE_MAP(CExplorerListView, CListView)
	//{{AFX_MSG_MAP(CExplorerListView)
	ON_WM_CREATE()
	ON_WM_DESTROY()

	
	//ON_COMMAND(ID_EDIT_CUT, OnEditCut)
	//ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)

	ON_NOTIFY_REFLECT(NM_RCLICK, OnRclick)
	ON_NOTIFY_REFLECT(NM_DBLCLK, OnDblclk)
	ON_NOTIFY_REFLECT(LVN_BEGINDRAG, OnBegindrag)
	ON_WM_MENUSELECT()
	ON_NOTIFY_REFLECT(LVN_ENDLABELEDIT, OnEndlabeledit)

	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_REFRESHFILES,OnRefreshList)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CExplorerListView drawing

void CExplorerListView::OnDraw(CDC* pDC)
{
	CDocument* pDoc = GetDocument();
	// TODO: add draw code here
	
}

void CExplorerListView::OnInitialUpdate()
{
	
	GetListCtrl().InsertColumn(0,"文件名称",LVCFMT_CENTER,120);
	GetListCtrl().InsertColumn(1,"文件类型",LVCFMT_LEFT,100);
	GetListCtrl().InsertColumn(2,"保护状态",LVCFMT_CENTER,120);
	GetListCtrl().InsertColumn(3,"文件大小",LVCFMT_RIGHT,100);

	GetListCtrl().SetExtendedStyle(LVS_EX_GRIDLINES |LVS_EX_INFOTIP |LVS_EX_INFOTIP |LVS_EX_FLATSB |LVS_EX_FULLROWSELECT| LVS_EX_HEADERDRAGDROP |LVS_SORTDESCENDING  );

	CListView::OnInitialUpdate();
	g_ListView=this;



// TODO: You may populate your ListView with items by directly accessing
	//  its list control through a call to GetListCtrl().
}

/////////////////////////////////////////////////////////////////////////////
// CExplorerListView diagnostics

#ifdef _DEBUG
void CExplorerListView::AssertValid() const
{
	CListView::AssertValid();
}

void CExplorerListView::Dump(CDumpContext& dc) const
{
	CListView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CExplorerListView message handlers

int CExplorerListView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CListView::OnCreate(lpCreateStruct) == -1)
		return -1;

		// TODO: Add your specialized creation code here
	//修改属性
	ModifyStyle(0,LVS_EDITLABELS|LVS_SHOWSELALWAYS|LVS_AUTOARRANGE);

	//设置菜单
	SetListViewType(LVS_REPORT);

	//注册拖拽
	m_ListTarget.Register(this);

	//初始化系统图标列表
	InitImageList();

	if ( g_TreeView )
	{
		HTREEITEM hSelect = g_TreeView->m_pTreeCtrl->GetSelectedItem();
		LPTREEPARAM ptrItem = (LPTREEPARAM)g_TreeView->m_pTreeCtrl->GetItemData(hSelect);
		SetListContext(ptrItem->lpsf,ptrItem->lpidl);
	}

	
	return 0;
}

void CExplorerListView::OnDestroy() 
{
	CListView::OnDestroy();
	
	// TODO: Add your message handler code here
	
}

void CExplorerListView::OnDblclk(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	NMITEMACTIVATE * nmia = (NMITEMACTIVATE*)pNMHDR;

	CPoint point(nmia->ptAction);
	int nCurItem = m_pListCtrl->HitTest(point);
	if ( nCurItem==-1 )
		return;

	LPLISTPARAM pliParam = (LPLISTPARAM)m_pListCtrl->GetItemData(nCurItem);



		//获取文件路径
	    char * szPath = new char[MAX_PATH];
		ZeroMemory(szPath,MAX_PATH);
		SHGetPathFromIDList(pliParam->lpidl,szPath);

	

	if ( pliParam->uType>0 )
	{
		//判断文件是否加密
		if(m_pListCtrl->GetItemText(nCurItem,2)=="加密")
		//文件解密过程
		{
	    CFile File;
	    File.Open(szPath,CFile::modeRead,NULL);
		long lFileLen=File.GetLength();
		File.Seek(4,CFile::begin);
		unsigned char *inBuff;  	
	    inBuff=new unsigned char[lFileLen-4];
	    File.Read(inBuff,lFileLen-4);
		File.Close();

		CFile newfile;
		newfile.Open(szPath,CFile::modeCreate|CFile::modeWrite);
		for(int i=0;i<lFileLen-4;i++)
			inBuff[i]=inBuff[i]^'e';
		newfile.Write(inBuff,lFileLen-4);
		newfile.Close();
		delete []  inBuff;
		delete szPath;
		//更新列表视图
		m_pListCtrl->SetItemText(nCurItem,2,"正常");
		}




		SHELLEXECUTEINFO si;
		ZeroMemory(&si,sizeof(si));
		si.cbSize = sizeof(si);
		si.hwnd = GetSafeHwnd();
		si.nShow = SW_SHOW;
		si.lpIDList = (LPVOID)pliParam->lpidl;
		si.fMask  = SEE_MASK_INVOKEIDLIST;
		ShellExecuteEx(&si);
	}
	else
	{
//		SetListContext(pliParam->lpsf,pliParam->lpidl);
		g_TreeView->SetTreeContext(pliParam->lpsf,pliParam->lpidl);
	}
	*pResult = 0;
}

void CExplorerListView::OnRclick(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	NMITEMACTIVATE * nmia = (NMITEMACTIVATE *) pNMHDR;
	
	POSITION pos = m_pListCtrl->GetFirstSelectedItemPosition();
	if ( pos==NULL )
	{
		//没选中时

		return;
	}

	char * szPath = new char[MAX_PATH];
	CStringArray sPathArray;
	while (pos)
	{
		ZeroMemory(szPath,MAX_PATH);
		int nItem = m_pListCtrl->GetNextSelectedItem(pos);
		LPLISTPARAM pliParam =(LPLISTPARAM) m_pListCtrl->GetItemData(nItem);
		SHGetPathFromIDList(pliParam->lpidl,szPath);
		sPathArray.Add(szPath);
	}
	delete szPath;
	
	CShellContextMenu scm;        
	scm.SetObjects(sPathArray);    
	CPoint point (nmia->ptAction);
	m_pListCtrl->ClientToScreen(&point);
	UINT idCommand = scm.ShowContextMenu(this, point);

	switch( idCommand )
	{
	case 18: //chose delete
		{
			g_TreeView->SendMessage(WM_REFRESHFILES,(WPARAM)g_TreeView->m_hSelItem,0);
			SendMessage(WM_REFRESHFILES,0,0);
			break;
		}
	case 27:  //chose paste
		{
			break;
		}
	}

 	*pResult = 0;
}

void CExplorerListView::OnBegindrag(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
	// TODO: Add your control notification handler code here
	g_TreeView->m_hMoveSrc = g_TreeView->m_hSelItem;

	POSITION pos = m_pListCtrl->GetFirstSelectedItemPosition();
	if ( pos==NULL )
		return;
	
	char * szSrc = new char[MAX_PATH];
	CStringArray sDragArray;
	while(pos)
	{
		int nItem = m_pListCtrl->GetNextSelectedItem(pos);
		LPLISTPARAM pliItem = (LPLISTPARAM)m_pListCtrl->GetItemData(nItem);
		ZeroMemory(szSrc,MAX_PATH);
		SHGetPathFromIDList(pliItem->lpidl,szSrc);
		
		sDragArray.Add(szSrc);
	}
	delete szSrc;

	//创建拖拽数据源
	int hgblSize = sizeof(DROPFILES);
	for ( int i=0 ; i<sDragArray.GetSize() ; i++ )
	{
		hgblSize += (sDragArray.GetAt(i).GetLength()+1) * sizeof(_TCHAR);
	}
	hgblSize += sizeof(_TCHAR);

	HGLOBAL hgblData = ::GlobalAlloc(GMEM_ZEROINIT|GMEM_MOVEABLE|GMEM_DDESHARE,hgblSize);
	if (hgblData == NULL)
		return;	
	
	LPDROPFILES pDropFiles = (LPDROPFILES)GlobalLock(hgblData);
	
	pDropFiles->pFiles = sizeof(DROPFILES);
	pDropFiles->fWide = FALSE;
	LPSTR pGlobalString = (LPSTR)(pDropFiles) + sizeof(DROPFILES);
	
	for ( i=0 ; i<sDragArray.GetSize() ; i++ )
	{
		strcpy((char*)(pGlobalString), sDragArray.GetAt(i) );
		pGlobalString += ( sDragArray.GetAt(i).GetLength()+1 ) * sizeof(_TCHAR);
	}
	pGlobalString[0] = _T('\0');
	
	GlobalUnlock(hgblData);
	
	COleDataSource *poleSourceObj = new COleDataSource;
	poleSourceObj->CacheGlobalData(CF_HDROP,hgblData);
	DROPEFFECT dropeffect = poleSourceObj->DoDragDrop();
	
	*pResult = 0;
}

void CExplorerListView::OnEndlabeledit(NMHDR* pNMHDR, LRESULT* pResult) 
{
	LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pNMHDR;
	// TODO: Add your control notification handler code here
	LV_ITEM lvItem = pDispInfo->item;
	LPLISTPARAM pliParam = (LPLISTPARAM)lvItem.lParam;
	char * szSource = new char[MAX_PATH];
	SHGetPathFromIDList(pliParam->lpidl,szSource);
	
	LPTREEPARAM ptrParam = (LPTREEPARAM)g_TreeView->GetSelTreeData();
	char * szDest = new char[MAX_PATH];
	SHGetPathFromIDList(ptrParam->lpidl,szDest);
	strcat(szDest,lvItem.pszText);
	
	CShellFileOp op;
	op.SetOperationFlags(FO_MOVE, this, FOF_NOCONFIRMMKDIR);
	op.AddDestFile(szDest);
	op.AddSourceFile(szSource);
	
	BOOL bSuccess, bAPICalled = FALSE, bAborted = FALSE;
	int  nAPIReturn = 0;
	bSuccess = op.Go ( &bAPICalled, &nAPIReturn, &bAborted );
	if ( bSuccess && !bAborted )
	{
		g_TreeView->SendMessage(WM_REFRESHFILES,(WPARAM)g_TreeView->m_hSelItem,0);
		*pResult = 1;
	}
	else
	{
		*pResult = 0;
	}
}

long CExplorerListView::OnRefreshList(WPARAM wParam, LPARAM lParam)
{
	if ( g_TreeView )
	{
		LPTREEPARAM ltrParam = (LPTREEPARAM)g_TreeView->GetSelTreeData();
		SetListContext(ltrParam->lpsf,ltrParam->lpidl);
	}
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -