1. 对话框打开文件
直接上代码:
void CHexMergeToolDlg::OnBnClickedButtonApp()
{
CString sEx;
CString sPath,strTemp;
CFileDialog filedlg(true);
CFile file;
ULONGLONG filelength;
filedlg.m_ofn.lpstrTitle =_T("打开");
filedlg.m_ofn.lpstrFilter = _T("Hex Files(*.hex)\0*.hex\0\0");
if(IDOK == filedlg.DoModal())
{
sPath = filedlg.GetPathName();
sEx = filedlg.GetFileExt();
if(sEx != _T("hex"))
{
m_App = _T("");
SetDlgItemText(IDC_EDIT_App,m_App);
return;
}
file.Open(sPath, CFile::modeRead );
filelength = file.GetLength();
if(filelength > FILE_LENGTH_MAX)
{
m_App = _T("");
SetDlgItemText(IDC_EDIT_App,m_App);
file.Close();
return;
}
m_App = sPath;
path_App = sPath;
strTemp.Format(_T("\r\n文件长度:%d字节"),filelength);
m_App += strTemp;
SetDlgItemText(IDC_EDIT_App,m_App);
file.Close();
}
}
效果如下:
2. 对话框保存文件
直接上代码:
void CHexMergeToolDlg::OnBnClickedButtonPath()
{
BOOL isOpen = FALSE;
CString defaultEx = L"hex";
CString fileName = L"All.hex";
CString filter = L"Hex Files(*.hex)\0*.hex\0\0";
CFileDialog openFileDlg(isOpen, defaultEx, fileName, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, filter, NULL);
m_Merge = _T("");
if(IDOK == openFileDlg.DoModal())
{
m_Merge = openFileDlg.GetPathName();
}
SetDlgItemText(IDC_EDIT_Merge,m_Merge);
}
效果如下: