承接专栏《MFC工控项目实例之四在调试目录下创建指定文件夹》
实时保存输入的iPlotX坐标轴最小值、最大值到CFG.PAR文件,打开界面从CFG.PAR文件中实时读取保存的最小值、最大值在编辑框中显示。
1、SEAL_PRESSURE.h中添加代码
class CSEAL_PRESSUREApp : public CWinApp
{
public:...CString m_CFGFileName;void LoadCFGFile(void);void SaveCFGFile(void);...
}
2、SEAL_PRESSURE.cpp中添加代码
CSEAL_PRESSUREApp::CSEAL_PRESSUREApp()
{TCHAR exeFullPath[MAX_PATH];GetModuleFileName(NULL,exeFullPath,MAX_PATH);m_Path = exeFullPath;for(int i = m_Path.GetLength() - 1; i > 0 ; i --){if(m_Path.GetAt(i) == '\\')break;}SetCurrentDirectory(m_Path);m_Path = m_Path.Left(i);m_CFGFileName = m_Path + "\\CFG.PAR";
} BOOL CSEAL_PRESSUREApp::InitInstance()
{...LoadCFGFile(); CSEAL_PRESSUREDlg dlg;m_pMainWnd = &dlg;int nResponse = dlg.DoModal();...SaveCFGFile();return FALSE;
}void CSEAL_PRESSUREApp::LoadCFGFile()
{CFile file;if(file.Open(m_CFGFileName,CFile::modeRead)){if(!file.Read(&m_sys_data,sizeof(m_sys_data))){memset(&m_sys_data,0,sizeof(m_sys_data));}file.Close();}else{memset(&m_sys_data,0,sizeof(m_sys_data));}
}void CSEAL_PRESSUREApp::SaveCFGFile()
{CFile file;if(file.Open(m_CFGFileName,CFile::modeCreate|CFile::modeWrite)){file.Write(&m_sys_data,sizeof(m_sys_data));file.Close();}
}
3、SYS_DATA.cpp中添加代码
SYS_DATA::SYS_DATA(CWnd* pParent /*=NULL*/): CDialog(SYS_DATA::IDD, pParent)
{//{{AFX_DATA_INIT(SYS_DATA)m_fMaxTime = 0.0;m_fMaxPressure = 0.0;m_fMinPressure = 0.0;//}}AFX_DATA_INITm_fMaxTime = theApp.m_sys_data.m_fMaxTime;m_fMinPressure = theApp.m_sys_data.m_fMinPressure;m_fMaxPressure = theApp.m_sys_data.m_fMaxPressure;
}
运行程序