📄 filedlg.cpp
字号:
// TODO: Add your control notification handler code here
UpdateData(true);
CString str;
int Nk,Nr,sel;
if(p_parent->m_key.IsEmpty()||m_cfile.IsEmpty()||m_pfile.IsEmpty())
{
MessageBox("所提供的信息不全,无法执行此操作!","错误",MB_OK|MB_ICONWARNING);
return;
}
if(m_FileLength<16)
{
MessageBox("所给定的文件的长度必须大于等于16个字节!","错误",MB_OK|MB_ICONWARNING);
return;
}
if(CheckSpace(m_pfile)==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_cfile,m_pfile)==false)
{
MessageBox(aes.ErrorMessage,"错误",MB_OK||MB_ICONWARNING);
return;
}
m_state.Format("正在解密文件,这根据文件的大小所需要的时间长度不一。请耐心等待!");
str.Format("正在解密文件 “%s”!",m_cfile);//状态条信息
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=1;
/* pMyThread->ResumeThread();
if(pMyThread!=NULL)
{
pMyThread->PostThreadMessage(WM_QUIT,0,0);
::WaitForSingleObject(pMyThread->m_hThread,INFINITE);
pMyThread->ExitInstance();
pMyThread=NULL;
}
*/ pMyThread=AfxBeginThread(MyThreadProc,this);//创建线程
}
void CFileDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if(nIDEvent==IDC_TIMER)
{
m_progress.SetPos(aes.GetCurPos());
}
CDialog::OnTimer(nIDEvent);
}
UINT CFileDlg::MyThreadProc(LPVOID pParam)//工作线程处理函数
{
CFileDlg *me=(CFileDlg *)pParam;
long StarTime,EndTime;
CString str;
MSG msg;
while(!PeekMessage(&msg,NULL,0,0,PM_NOREMOVE))//检查是否有消息到达
{
if(me->EnOrDecrypt==2)//SuspendThread(pMyThread)//::PostQuitMessage()
{
me->Out();
break;
}
else
{
StarTime=GetTickCount();//获取开始时间
if(me->EnOrDecrypt==0)//当EnOrDecrypt为0时加密文件
{
if(me->aes.EncrypFile()==false)
{
AfxMessageBox(me->aes.ErrorMessage,MB_ICONINFORMATION|MB_OK);
me->KillTimer(IDC_TIMER);
me->m_state="加密过程出错,程序自动中继退出加密过程!";
me->m_UsedTime.SetWindowText("<未完成>");
me->EnOrDecrypt=2;
continue;
}
}
else//当EnOrDecrypt为1时解密文件
{
if(me->aes.DecrypFile()==false)
{
AfxMessageBox(me->aes.ErrorMessage,MB_ICONINFORMATION|MB_OK);
me->KillTimer(IDC_TIMER);
me->m_state="加密过程出错,程序自动中继退出加密过程!";
me->m_UsedTime.SetWindowText("<未完成>");
me->EnOrDecrypt=2;
continue;
}
}
me->KillTimer(IDC_TIMER);
me->m_progress.SetPos(100);
EndTime=GetTickCount();//获取完成时间
long diftime=EndTime-StarTime;
int msecond,second,minute,hour;
msecond=second=minute=hour=0;
msecond=diftime%1000;//毫秒
if(diftime>=1000)
{
second=(diftime-msecond)/1000;//秒
if(second>=60)
{
minute=second/60;//分
second=second%60;
if(second>=60)
{
hour=minute/60;//小时
minute=minute%60;//分
}
}
}
me->m_time.Format("%ld毫秒",diftime);
if(msecond||second||minute||hour)
{
CString str;
me->m_time=me->m_time+"\t亦即:";
if(hour!=0)
{
str.Format("%d时",hour);
me->m_time=me->m_time+str;
}
if(minute!=0)
{
str.Format("%d分",minute);
me->m_time=me->m_time+str;
}
if(second!=0)
{
str.Format("%d秒",second);
me->m_time=me->m_time+str;
}
if(msecond!=0)
{
str.Format("%d毫秒",msecond);
me->m_time=me->m_time+str;
}
}
me->m_UsedTime.SetWindowText(me->m_time);
if(me->EnOrDecrypt==0)//当EnOrDecrypt为True时加密文件
{
str.Format("“%s” 文件已经成功加密完成!",me->m_sfilename);
AfxMessageBox("加密成功完成!",MB_ICONINFORMATION|MB_OK);
}
else//当EnOrDecrypt为flase时解密文件
{
str.Format("“%s” 文件已经成功解密完成!",me->m_cfile);
AfxMessageBox("解密成功完成!",MB_ICONINFORMATION|MB_OK);
}
me->m_MsgState.SetWindowText(str);
p_parent->m_StatusBar.SetText("梦幻组合--魏嘉银、张科、许孝勇",0,1);
}
me->EnOrDecrypt=2;
me->GetDlgItem(IDC_DECRYP)->EnableWindow(true);//若加密或解密完成,则使解密按钮可用
me->GetDlgItem(IDC_ENCRYP)->EnableWindow(true);//若加密或解密完成,则使加密按钮可用
me->GetDlgItem(IDC_EXT)->EnableWindow(true);//正在执行加密,则先禁用后缀名按钮
}
return 0;
}
BOOL CFileDlg::DestroyWindow()
{
// TODO: Add your specialized code here and/or call the base class
if(pMyThread!=NULL)
{
pMyThread->PostThreadMessage(WM_QUIT,0,0);
::WaitForSingleObject(pMyThread->m_hThread,INFINITE);
pMyThread=NULL;
}
return CDialog::DestroyWindow();
}
void CFileDlg::Out()
{
pMyThread->PostThreadMessage(WM_QUIT,0,0);
pMyThread=NULL;
}
void CFileDlg::OnChangeExt()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
CString str;
str=m_ext;
int pos;
UpdateData(true);
if(!m_pfile.IsEmpty())
{
pos=m_pfile.ReverseFind('.');
char c=m_pfile.GetAt(pos);
str.Format("%s.%s",m_pfile.Left(pos),m_ext);
m_pfile=str;
}
if(m_ext.IsEmpty())
this->GetDlgItem(IDC_DECRYP)->EnableWindow(false);//若修改后的后缀名为空,则先禁用解密按钮
else
this->GetDlgItem(IDC_DECRYP)->EnableWindow(true);//若修改后的后缀名不为空,则使解密按钮可用
UpdateData(false);
}
bool CFileDlg::CheckSpace(CString path)
{
if(path.GetAt(1)!=':'||path.GetAt(2)!='\\')
{
MessageBox("您所给定的保存路径不合法!请重新指定一个保存路径!","错误",MB_OK|MB_ICONWARNING);
return false;
}
size_t alldriver=::GetLogicalDriveStrings(0,NULL);
_TCHAR *driverstr;
driverstr=new _TCHAR[alldriver+sizeof(" ")];
if(::GetLogicalDriveStrings(alldriver,driverstr)!=alldriver-1)
exit(0);
_TCHAR *pdriverstr=driverstr;
size_t driversize=strlen(pdriverstr);
while(driversize>0)
{
ULARGE_INTEGER AvailableToCaller,TotalNumberOfBytes,TotalNumberOfFreeBytes;
if(pdriverstr==path.Left(3))//为硬盘
{
if(GetDiskFreeSpaceEx(pdriverstr,&AvailableToCaller,&TotalNumberOfBytes,&TotalNumberOfFreeBytes))
{
if(TotalNumberOfFreeBytes.QuadPart<m_FileLength)//所需磁盘的空间大于所选磁盘的剩余空间
{
CString msg;
msg.Format("错误:%s的剩余空间为:%d 字节少于所需的磁盘空间(%d 字节)!请重新选择一个磁盘!",path.Left(3),TotalNumberOfFreeBytes.QuadPart,m_FileLength);
MessageBox(msg,"错误",MB_OK|MB_ICONWARNING);
return false;
}
else
break;
}
else
{
MessageBox("读取磁盘资料出错.","错误",MB_OK|MB_ICONWARNING);
return false;
}
}
pdriverstr+=driversize+1;
driversize=strlen(pdriverstr);
}
if(driversize<=0)
{
MessageBox("没有所选择的磁盘!请确认后重新选择一个磁盘!","错误",MB_OK|MB_ICONWARNING);
return false;
}
return true;
}
void CFileDlg::OnButClear()
{
// TODO: Add your control notification handler code here
m_ext.Format("txt");
this->GetDlgItem(IDC_ENFBROWSE)->EnableWindow(false);//先禁用浏览文件夹按钮
this->GetDlgItem(IDC_PFBROWSE)->EnableWindow(false);//先禁用浏览文件夹按钮
m_cfile=_T("");
m_sfilename=_T("");
m_enfile=_T("");
m_ext=_T("txt");
m_pfile=_T("");
m_state.Format("<无任务>");
m_filelen.Format("<无文件>");
m_time.Format("0秒");
m_progress.SetPos(0);
UpdateData(false);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -