在MFC开发过程中,利用Windows内置的文件对话框可以显著提高文件操作的效率。本文总结了使用文件对话框进行文件选择和处理的经验,详细介绍了相关API的调用方法和参数设置,如`CFileDialog`类的使用、结构体`OPENFILENAME`的配置以及如何获取选中的文件路径。通过这些技巧,开发者可以快速实现文件的打开、保存等功能,提升应用程序的用户体验。
代码:
int nStructSize = 76; //Win98 as default
LPARAM lModuleParam = 0;
TV_INSERTSTRUCT tvItem;
CString sPathNameSel;
char lpszCurrentDir[1024];//1K
::GetModuleFileName(NULL, lpszCurrentDir, 1024); //Get
current directory
int n = CString(lpszCurrentDir).ReverseFind(‘\\‘);
CString sTemp = CString(lpszCurrentDir).Left(n);
STRCPY(lpszCurrentDir, sTemp);
CString str("", 1024);
/*if (TRUE == bAMT700PrjModify)
{
bAMT700PrjModify = FALSE;
int nAnswer = MxMessageBox(m_hWnd, IDS_SAVEPROJECTORNOT,
IDS_CAPTION_NEWPROJECT, MB_YESNO);
if (IDYES == nAnswer)
{
OnBnClickedBtnAmt700PrjSave();
}
}
*/
CFileDialog mFileDlg( TRUE, NULL, NULL,
OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, "IRIS Files
(*.ini)|*.ini||", this); //NT does not support
mFileDlg.m_ofn.lpstrFile =
(LPSTR)(LPCTSTR)str;//.GetBuffer(MERGE_MAX_COUNT_OF_FILE_CAN_SELECT);
mFileDlg.m_ofn.lpstrTitle = RESID2LPSTR(IDS_BROWSEFORPROJECT);
mFileDlg.m_ofn.lpstrInitialDir = lpszCurrentDir;
DWORD dwVersion = GetVersion();
if (dwVersion <0x80000000) //Windows
NT/win2000
{
nStructSize = 88;
}
else //Windows95/98
{
nStructSize = 76;
}
mFileDlg.m_ofn.lStructSize = nStructSize;
str.ReleaseBuffer();
if (mFileDlg.DoModal() == IDOK)
{
POSITION pos = mFileDlg.GetStartPosition();
while (NULL != pos)
{
m_sPrjPathName = mFileDlg.GetNextPathName(pos);
sPathNameSel = mFileDlg.GetFileName();
}
CFileFind finder;
DWORD dwFindRet;
dwFindRet = m_sPrjPathName.Find(".ini");
if (0xffffffff == dwFindRet)
{
m_sPrjPathName += ".ini";
}
dwFindRet = sPathNameSel.Find(".ini");
if (0xffffffff == dwFindRet)
{
sPathNameSel += ".ini";
}
if (!finder.FindFile(m_sPrjPathName))
{
MxMessageBox(m_hWnd, IDS_PROJECTNOTEXITS, IDS_CAPTION_OPENPROJECT,
MB_ICONERROR);
m_sPrjPathName.Empty();
return;
}
sPathNameSel = sPathNameSel.Left(sPathNameSel.GetLength() -
strlen(".ini"));
g_prjAMT700.Release();
/*m_treePrj.DeleteAllItems();
m_dlgModule.m_nModuleCount = 0;
m_dlgModule.m_bMethod = 1; //add
m_dlgAddr.ShowWindow(SW_HIDE);
m_dlgModule.ShowWindow(SW_SHOW);*/
//CButton * pBtn = (CButton *)GetDlgItem(IDC_RAD_AMT700_MODULE);
//pBtn->ShowWindow(SW_SHOW); //pBtn->SetCheck(1);
//pBtn = (CButton *)GetDlgItem(IDC_RAD_AMT700_ADDR);
//pBtn->ShowWindow(SW_SHOW);
tvItem.hParent = TVI_ROOT;
tvItem.hInsertAfter = TVI_ROOT;
// tvItem.item.iImage = nImage;
// tvItem.item.iSelectedImage = nImage;
tvItem.item.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_TEXT |
TVIF_PARAM;
tvItem.item.pszText = CSTRING_TO_LPSTR sPathNameSel;
tvItem.item.lParam = MAKELPARAM(g_prjAMT700.GetModuleCount(),
MAKEWORD(1/*PRJ*/, 0));//Prj
//m_hPrj = m_treePrj.InsertItem(&tvItem);
//m_treePrj.SelectItem(m_hPrj);
//if (AMT700_VERSION == iAMTVer)
//{
// g_prjAMT700.LoadProject(m_sPrjPathName);
// for (int i = 0; i
// {
// CModule700 * pTmpModule = g_prjAMT700.GetModuleAt(i);
// lModuleParam = MAKELPARAM(i, MAKEWORD(2/*Module*/,
0));
// HTREEITEM hItem = AddModuleItem(m_hPrj,
*pTmpModule, lModuleParam);
// m_dlgModule.m_nModuleCount++;
// for (int j = 0; j <
pTmpModule->GetAddrCount(); j++)
// { // CAddr700 pTmpAddr;
// pTmpModule->GetAddrAtEx(j, &pTmpAddr);
// AddAddrItem(hItem, pTmpAddr, MAKELPARAM(j, MAKEWORD(3,
0)));
// }
// }
//}
}
工作总结:MFC使用Windows自带打开对话框,布布扣,bubuko.com