?? filedlg.cpp
字號:
// FileDlg.cpp : implementation file
//
#include "stdafx.h"
#include "AES.h"
#include "FileDlg.h"
#include "AESDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CAESDlg *p_parent;
/////////////////////////////////////////////////////////////////////////////
// CFileDlg dialog
CFileDlg::CFileDlg(CWnd* pParent /*=NULL*/)
: CDialog(CFileDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CFileDlg)
m_sfilename = _T("");
m_cfile = _T("");
m_enfile = _T("");
m_ext = _T("");
m_filelen = _T("");
m_pfile = _T("");
m_time = _T("");
m_state = _T("");
//}}AFX_DATA_INIT
}
void CFileDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CFileDlg)
DDX_Control(pDX, IDC_STATE, m_MsgState);
DDX_Control(pDX, IDC_TIME, m_UsedTime);
DDX_Control(pDX, IDC_PROGRESS, m_progress);
DDX_Text(pDX, IDC_SOURCEFILE, m_sfilename);
DDX_Text(pDX, IDC_CFILE, m_cfile);
DDX_Text(pDX, IDC_ENFILE, m_enfile);
DDX_Text(pDX, IDC_EXT, m_ext);
DDX_Text(pDX, IDC_FILELEN, m_filelen);
DDX_Text(pDX, IDC_PFILE, m_pfile);
DDX_Text(pDX, IDC_TIME, m_time);
DDX_Text(pDX, IDC_STATE, m_state);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CFileDlg, CDialog)
//{{AFX_MSG_MAP(CFileDlg)
ON_BN_CLICKED(IDC_SFBROWSE, OnSFBrowse)
ON_BN_CLICKED(IDC_ENFBROWSE, OnEnFBrowse)
ON_BN_CLICKED(IDC_MFBROWSE, OnMFBrowse)
ON_BN_CLICKED(IDC_PFBROWSE, OnPFBrowse)
ON_BN_CLICKED(IDC_ENCRYP, OnEncryp)
ON_BN_CLICKED(IDC_DECRYP, OnDecryp)
ON_WM_TIMER()
ON_EN_CHANGE(IDC_EXT, OnChangeExt)
ON_BN_CLICKED(IDC_BUTCLEAR, OnButClear)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFileDlg message handlers
void CFileDlg::OnSFBrowse()
{
// TODO: Add your control notification handler code here
UpdateData(true);
CString lpszFilter;
double length;
CFileStatus status;//創建文件狀態類的對象
lpszFilter.Format("所有文件(*.*)|*.*||");
CFileDialog filedlg(true,NULL,NULL,OFN_HIDEREADONLY,lpszFilter,NULL);
if(filedlg.DoModal()==IDOK)
{
m_sfilename=filedlg.GetPathName();
FileName=filedlg.GetFileName();
CString ext=filedlg.GetFileExt();
FileName=FileName.Left(FileName.GetLength()-ext.GetLength()-1);
this->GetDlgItem(IDC_ENFBROWSE)->EnableWindow(true);//使瀏覽文件夾按鈕可用
CFile::GetStatus(m_sfilename,status);
length=status.m_size;
m_FileLength=long(length);
if(length>=1024)
{
length=length/1024.0;
if(length>=1024)
{
length=length/1024.0;
m_filelen.Format("%ld字節\t亦即%.3f兆",status.m_size,length);//文件長度
}
else
m_filelen.Format("%ld字節\t亦即%ld千字節",status.m_size,length);//文件長度
}
else
m_filelen.Format("%ld字節",status.m_size);//文件長度
}
UpdateData(false);
}
void CFileDlg::OnEnFBrowse()
{
// TODO: Add your control notification handler code here
UpdateData(true);
LPBROWSEINFO lpbi=new BROWSEINFO;
lpbi->hwndOwner=GetSafeHwnd();//NULL;
lpbi->pidlRoot=NULL;
lpbi->pszDisplayName=NULL;
lpbi->lpszTitle="請選擇所要存放的目錄";
lpbi->ulFlags=BIF_BROWSEFORCOMPUTER|BIF_EDITBOX;
lpbi->lpfn=NULL;
LPITEMIDLIST lpitemidlist=SHBrowseForFolder(lpbi);
if(lpitemidlist==NULL)
{
delete lpbi;
lpbi=NULL;
return;
}
char path[MAX_PATH];
//轉換項目標志符列表為一個文件路徑
SHGetPathFromIDList(lpitemidlist,path);
delete lpbi;
m_enfile.Format("%s\\%s.cryp",path,FileName);
m_enfile.Replace("\\\\","\\");
if(CheckSpace(m_enfile)==false)//檢查磁盤的剩余空間是否足夠
m_enfile=_T("");
UpdateData(false);
}
void CFileDlg::OnMFBrowse()
{
// TODO: Add your control notification handler code here
UpdateData(true);
CString lpszFilter;
double length;
CFileStatus status;//創建文件狀態類的對象
lpszFilter.Format("加密文件(*.cryp)|*.cryp||");
CFileDialog filedlg(true,NULL,NULL,OFN_HIDEREADONLY,lpszFilter,NULL);
if(filedlg.DoModal()==IDOK)
{
m_cfile=filedlg.GetPathName();
FileName=filedlg.GetFileName();
CString ext=filedlg.GetFileExt();
FileName=FileName.Left(FileName.GetLength()-ext.GetLength()-1);
this->GetDlgItem(IDC_PFBROWSE)->EnableWindow(true);//使瀏覽文件夾按鈕可用
CFile::GetStatus(m_cfile,status);
length=status.m_size;
m_FileLength=long(length);
if(length>=1024)
{
length=length/1024.0;
if(length>=1024)
{
length=length/1024.0;
m_filelen.Format("%ld字節\t亦即%.3f兆",status.m_size,length);//文件長度
}
else
m_filelen.Format("%ld字節\t亦即%ld千字節",status.m_size,length);//文件長度
}
else
m_filelen.Format("%ld字節",status.m_size);//文件長度
}
UpdateData(false);
}
void CFileDlg::OnPFBrowse()
{
// TODO: Add your control notification handler code here
UpdateData(true);
LPBROWSEINFO lpbi=new BROWSEINFO;
lpbi->hwndOwner=GetSafeHwnd();//NULL;
lpbi->pidlRoot=NULL;
lpbi->pszDisplayName=NULL;
lpbi->lpszTitle="請選擇所要存放的目錄";
lpbi->ulFlags=BIF_BROWSEFORCOMPUTER|BIF_EDITBOX;
lpbi->lpfn=NULL;
LPITEMIDLIST lpitemidlist=SHBrowseForFolder(lpbi);
if(lpitemidlist==NULL)
{
delete lpbi;
lpbi=NULL;
return;
}
char path[MAX_PATH];
//轉換項目標志符列表為一個文件路徑
SHGetPathFromIDList(lpitemidlist,path);
delete lpbi;
if(m_ext.IsEmpty())
{
MessageBox("加密后文件的后綴名尚未給定!不能執行此操作!","警告",MB_OK|MB_ICONWARNING);
return;
}
m_pfile.Format("%s\\%s.%s",path,FileName,m_ext);
m_pfile.Replace("\\\\","\\");
if(CheckSpace(m_pfile)==false)//檢查磁盤的剩余空間是否足夠
m_pfile=_T("");
UpdateData(false);
}
BOOL CFileDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
p_parent=(CAESDlg *)this->GetParentOwner();
m_ext.Format("txt");
this->GetDlgItem(IDC_ENFBROWSE)->EnableWindow(false);//先禁用瀏覽文件夾按鈕
this->GetDlgItem(IDC_PFBROWSE)->EnableWindow(false);//先禁用瀏覽文件夾按鈕
m_state.Format("<無任務>");
m_filelen.Format("<無文件>");
m_time.Format("0秒");
m_progress.SetRange(0,100);
m_progress.SetPos(0);
pMyThread=NULL;
EnOrDecrypt=2;
// pMyThread=AfxBeginThread(MyThreadProc,this,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED,NULL);//創建線程
// pMyThread=AfxBeginThread(MyThreadProc,this);//創建線程
UpdateData(false);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CFileDlg::OnEncryp()
{
// TODO: Add your control notification handler code here
UpdateData(true);
int Nk,Nr,sel;
CString str;
if(p_parent->m_key.IsEmpty()||m_sfilename.IsEmpty()||m_enfile.IsEmpty())
{
MessageBox("所提供的信息不全,無法執行此操作!","錯誤",MB_OK|MB_ICONWARNING);
return;
}
if(m_FileLength<16)
{
MessageBox("所給定的文件的長度必須大于等于16個字節!","錯誤",MB_OK|MB_ICONWARNING);
return;
}
if(CheckSpace(m_enfile)==false)//檢查磁盤的剩余空間是否足夠
return;
sel=p_parent->m_lentype.GetCurSel();//取得密鑰長度的類型
switch(sel)
{
case 0:Nk=4;Nr=10;break;
case 1:Nk=6;Nr=12;break;
case 2:Nk=8;Nr=14;break;
}
if(p_parent->m_key.GetLength()<Nk*4)
m_key=p_parent->m_keytemp;
else
m_key=p_parent->m_key;
if(aes.SetVariable(Nk,Nr,m_key)==false)
{
MessageBox(aes.ErrorMessage,"錯誤",MB_OK||MB_ICONWARNING);
return;
}
if(aes.SetFileName(m_sfilename,m_enfile)==false)
{
MessageBox(aes.ErrorMessage,"錯誤",MB_OK||MB_ICONWARNING);
return;
}
m_state.Format("正在加密文件,這根據文件的大小所需要的時間長度不一。請耐心等待!");
str.Format("正在加密文件 “%s”!",m_sfilename);//狀態條信息
p_parent->m_StatusBar.SetText(str,0,1);
m_progress.SetPos(0);//設置進度條的位置
SetTimer(IDC_TIMER,10,NULL);
UpdateData(false);
m_UsedTime.SetWindowText("正在計算運行時間...");
this->GetDlgItem(IDC_DECRYP)->EnableWindow(false);//正在執行加密,則先禁用解密按鈕
this->GetDlgItem(IDC_ENCRYP)->EnableWindow(false);//正在執行加密,則先禁用加密按鈕
this->GetDlgItem(IDC_EXT)->EnableWindow(false);//正在執行加密,則先禁用后綴名按鈕
EnOrDecrypt=0;
/* pMyThread->ResumeThread();
if(pMyThread!=NULL)
{
pMyThread->PostThreadMessage(WM_QUIT,0,0);
pMyThread->ExitInstance();
::WaitForSingleObject(pMyThread->m_hThread,INFINITE);
pMyThread=NULL;
}
*/ pMyThread=AfxBeginThread(MyThreadProc,this);//創建線程
}
void CFileDlg::OnDecryp()
{
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -