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

📄 mainfrm.cpp

📁 加密工具
💻 CPP
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//

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

extern CExplorerListView * g_ListView;
extern CExplorerTreeView * g_TreeView;

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNAMIC(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_WM_SETFOCUS()
	ON_COMMAND(ID_VIEW_SMALLICON, OnViewSmallicon)
	ON_COMMAND(ID_VIEW_LIST, OnViewList)
	ON_COMMAND(ID_VIEW_LARGEICON, OnViewLargeicon)
	ON_COMMAND(ID_VIEW_REPORT, OnViewReport)
	ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
	ON_COMMAND(ID_EDIT_CUT, OnEditCut)
	ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
	ON_COMMAND(ID_ENCRYPT, Onfile_encrypt)
	ON_COMMAND(ID_DECRYPT, Onfile_decrypt)

	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	// TODO: Delete these three lines if you don't want the toolbar to
	//  be dockable
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	cs.dwExStyle &= ~WS_EX_CLIENTEDGE;
	cs.lpszClass = AfxRegisterWndClass(0);
	SetTitle( "透明加密器TrueCrypt" );
	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers
void CMainFrame::OnSetFocus(CWnd* pOldWnd)
{
	// forward focus to the view window
//	m_wndView.SetFocus();
}

BOOL CMainFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
{
	// let the view have first crack at the command
//	if (m_wndView.OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
//		return TRUE;

	// otherwise, do default handling
	return CFrameWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}


BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext) 
{
	// TODO: Add your specialized code here and/or call the base class
	CRect rect;
	GetClientRect(&rect);

	if ( !m_wndSplitter.CreateStatic(this,1,2) )
	{
		TRACE0("Failed to CreateStaticSplitter\n");
		return FALSE;
	}

	// add the first splitter pane - the default view in column 0
	if (!m_wndSplitter.CreateView(0, 0, RUNTIME_CLASS(CExplorerTreeView), 
		CSize(240,rect.bottom), pContext))
	{
		TRACE0("Failed to create first pane\n");
		return FALSE;
	}

	// add the second splitter pane - an input view in column 1
	if (!m_wndSplitter.CreateView(0, 1, RUNTIME_CLASS(CExplorerListView), 
		CSize(rect.right,rect.bottom-350), pContext))
	{
		TRACE0("Failed to create second pane\n");
		return FALSE;
	}

	m_pTreeView = (CExplorerTreeView*)m_wndSplitter.GetPane(0,0);
	m_pListView = (CExplorerListView*)m_wndSplitter.GetPane(0,1);

	return CFrameWnd::OnCreateClient(lpcs, pContext);
}

///////////////////////////////////////////

void CMainFrame::OnViewSmallicon() 
{
	// TODO: Add your command handler code here
	g_ListView->SetListViewType(LVS_SMALLICON);
}

void CMainFrame::OnViewList() 
{
	// TODO: Add your command handler code here
	g_ListView->SetListViewType(LVS_LIST);
}

void CMainFrame::OnViewLargeicon() 
{
	// TODO: Add your command handler code here
	g_ListView->SetListViewType(LVS_LIST);
}

void CMainFrame::OnViewReport() 
{
	// TODO: Add your command handler code here
	g_ListView->SetListViewType(LVS_REPORT);
}
void CMainFrame::OnEditCopy() 
{
	// TODO: Add your command handler code here
	UINT uDropEffect = RegisterClipboardFormat("Preferred DropEffect");
	HGLOBAL  hGblEffect = GlobalAlloc(GMEM_ZEROINIT|GMEM_MOVEABLE|GMEM_DDESHARE,sizeof(DWORD));
	DWORD * dwDropEffect =(DWORD*)GlobalLock(hGblEffect);
	*dwDropEffect        = DROPEFFECT_COPY;
	GlobalUnlock(hGblEffect);
	
	POSITION pos = m_pListView->m_pListCtrl->GetFirstSelectedItemPosition();
	if ( pos==NULL )
		return;
				
	char * szSrc = new char[MAX_PATH];
	CStringArray sDragArray;
	while(pos)
	{
		int nItem = m_pListView->m_pListCtrl->GetNextSelectedItem(pos);
		LPLISTPARAM pliItem = (LPLISTPARAM)m_pListView->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);
	
	if( ::OpenClipboard(NULL) )
	{
		::EmptyClipboard();
		::SetClipboardData(uDropEffect,hGblEffect);
		::SetClipboardData( CF_HDROP, hgblData );
		::CloseClipboard();
	}
}

void CMainFrame::OnEditCut() 
{
	// TODO: Add your command handler code here
	
	UINT uDropEffect = RegisterClipboardFormat("Preferred DropEffect");
	HGLOBAL  hGblEffect = ::GlobalAlloc(GHND, (DWORD)sizeof(DWORD)); 
	DWORD * dwDropEffect = (DWORD*)GlobalLock(hGblEffect);
	*dwDropEffect = DROPEFFECT_MOVE;
	GlobalUnlock(hGblEffect);

	POSITION pos = m_pListView->m_pListCtrl->GetFirstSelectedItemPosition();
	if ( pos==NULL )
		return;
				
	char * szSrc = new char[MAX_PATH];
	CStringArray sDragArray;
	while(pos)
	{
		int nItem = m_pListView->m_pListCtrl->GetNextSelectedItem(pos);
		LPLISTPARAM pliItem = (LPLISTPARAM)m_pListView->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(GHND,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);
	
	if( ::OpenClipboard(NULL) )
	{
		::EmptyClipboard();
		::SetClipboardData(uDropEffect,hGblEffect);
		::SetClipboardData( CF_HDROP, hgblData );
		::CloseClipboard();

		g_TreeView->m_hMoveSrc = g_TreeView->m_hSelItem;
	}
}

void CMainFrame::OnEditPaste() 
{
	// TODO: Add your command handler code here
	if ( !IsClipboardFormatAvailable(CF_HDROP) )
		return;
	UINT uDropEffect = RegisterClipboardFormat("Preferred DropEffect");
	if ( !IsClipboardFormatAvailable(uDropEffect) )
		return;
	
	if ( !::OpenClipboard(NULL) ) 
		return; 
	DWORD dwEffect = *(DWORD*)::GetClipboardData(uDropEffect);
	HGLOBAL hglobal = ::GetClipboardData(CF_HDROP);
	::CloseClipboard();

	if( hglobal != NULL )
	{
		CShellFileOp op;
		if ( dwEffect & DROPEFFECT_MOVE )
			op.SetOperationFlags(FO_MOVE, this, FOF_ALLOWUNDO|FOF_NOCONFIRMMKDIR);
		else
			op.SetOperationFlags(FO_COPY, this, FOF_ALLOWUNDO|FOF_NOCONFIRMMKDIR);

		char * szFileName;
		szFileName = new char[MAX_PATH];
		HDROP hDrop = (HDROP)GlobalLock(hglobal);
		int FileNum = DragQueryFile(hDrop,-1,NULL,0);
		
		for ( int i =0 ; i < FileNum ; i++ )
		{
			ZeroMemory(szFileName,MAX_PATH);
			DragQueryFile(hDrop,i,szFileName,255);
			op.AddSourceFile(szFileName);
		}
		delete szFileName;
		
		//设置粘贴目标
		char * szDest = new char[MAX_PATH];
		ZeroMemory(szDest,MAX_PATH);
		
		LPTREEPARAM ptrItem = (LPTREEPARAM)g_TreeView->GetSelTreeData();
		SHGetPathFromIDList(ptrItem->lpidl,szDest);
		op.AddDestFile(szDest);
		BOOL bSuccess, bAPICalled = FALSE, bAborted = FALSE;
		int  nAPIReturn = 0;
		
		bSuccess = op.Go (&bAPICalled, &nAPIReturn, &bAborted);
		
		GlobalFree(hglobal);
		delete szDest;
		
		//refresh
		if ( bSuccess && !bAborted )
		{
			g_TreeView->SendMessage(WM_REFRESHFILES,0,0);
			g_TreeView->SendMessage(WM_REFRESHFILES,(WPARAM)g_TreeView->m_hMoveSrc,0);
			g_ListView->SendMessage(WM_REFRESHFILES,0,0);
		}
	}
}

//加密文件函数
void CMainFrame::Onfile_encrypt() 
{
	int count=0;
	POSITION pos = m_pListView->m_pListCtrl->GetFirstSelectedItemPosition();
	if ( pos==NULL )
		return;
		while(pos)	
		{
	    char * szPath = new char[MAX_PATH];

		//获取文件路径
		int nItem = m_pListView->m_pListCtrl->GetNextSelectedItem(pos);
		LPLISTPARAM pliItem = (LPLISTPARAM)m_pListView->m_pListCtrl->GetItemData(nItem);		
		ZeroMemory(szPath,MAX_PATH);
		SHGetPathFromIDList(pliItem->lpidl,szPath);

        //加密过程,仅添加标志位
		CFile File;
	    File.Open(szPath,CFile::modeRead,NULL);
		
	    unsigned char *inBuff;
		unsigned char *outBuff;
		CString flag="####";
		LPTSTR  eflag=flag.GetBuffer(5);
		flag.ReleaseBuffer();

    	long lFileLen=File.GetLength();
	    inBuff=new unsigned char[lFileLen];
		outBuff=new unsigned char[lFileLen+4];
	    File.Read(inBuff,lFileLen);

		for(int i=0;i<4;i++)
			outBuff[i]=eflag[i];
		for(i=0;i<lFileLen;i++)
			outBuff[i+4]=(inBuff[i]^'e');

		File.Close();
		//删除源文件
		CFile::Remove(szPath);
		
		//生成密文文件
		CFile newfile;
		newfile.Open(szPath,CFile::modeCreate|CFile::modeWrite);
		newfile.Write(outBuff,lFileLen+4);
		newfile.Close();
		delete []  inBuff;
		delete []  outBuff;
		//更新列表视图
		m_pListView->m_pListCtrl->SetItemText(nItem,2,"加密");
	    delete szPath;	
		count++;
		}

		CString s;
		s.Format("%d",count);
		s+="个文件";
        AfxMessageBox("成功加密"+s);
}



void CMainFrame::Onfile_decrypt() 
{
	int count=0;
	POSITION pos = m_pListView->m_pListCtrl->GetFirstSelectedItemPosition();
	if ( pos==NULL )
		return;
		while(pos)	
		{
	    char * szPath = new char[MAX_PATH];

		//获取文件路径
		int nItem = m_pListView->m_pListCtrl->GetNextSelectedItem(pos);
		LPLISTPARAM pliItem = (LPLISTPARAM)m_pListView->m_pListCtrl->GetItemData(nItem);		
		ZeroMemory(szPath,MAX_PATH);
		SHGetPathFromIDList(pliItem->lpidl,szPath);

		//文件解密过程
	    CFile File;
	    File.Open(szPath,CFile::modeRead,NULL);
	     
	    char temp[4];
		long lFileLen=File.GetLength();
		File.Read(temp,4);
		if(temp[0]=='#'&&temp[1]=='#'&&temp[2]=='#'&&temp[3]=='#')
		{
		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;
		//更新列表视图
		m_pListView->m_pListCtrl->SetItemText(nItem,2,"正常");
		count++;
		}
		else
		{
			AfxMessageBox("文件没有加密!");
			return;
		}
			delete szPath;
		}
		CString s;
		s.Format("%d",count);
		s+="个文件";
        AfxMessageBox("成功解密"+s);
		
}

⌨️ 快捷键说明

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