各位朋友,搞了这么久的VC,vc的难真的是不言而语、、、,在这里我要问的是我做的是个交互式的界面,我的这个应用程序类型是基本对话框。而这个界面上有很多组合框,复选框需要设置,比如说这次我选择好了组合框的值,又确定了复选框的状态,我想这些设置都保存在电脑的文件中,下次我打开程序 要使用上次的设置我就直接打开上次我保存的文件,各框就直接恢复我上次设置的状态了就不必再手动去设置了。这就是我难倒自己的地方了 大家不要再给我发下面的程序代码了,我都了解了一些了 我要的是怎样才能去做才能保存组合框 复选框的状态。谢谢大家, 这个问题困扰我好久了。希望有人能真正的给我解决,我一定给分,并希望大家能最好附一些实现这个工程的代码,十分感谢!char szFilters[]= "*.mod|*.mod|All Files (*.*)|*.*||";
CFileDialog fileDlg (FALSE, "*", "*.mod", OFN_FILEMUSTEXIST| OFN_HIDEREADONLY, szFilters, this);
if( IDOK == fileDlg.DoModal() )
{
CString szFileName = fileDlg.GetFileName();
}
CFileDialog fileDlg (FALSE, "*", "*.mod", OFN_FILEMUSTEXIST| OFN_HIDEREADONLY, szFilters, this);
if( IDOK == fileDlg.DoModal() )
{
CString szFileName = fileDlg.GetFileName();
}
UINT8 ucAck = CCU_NULL_BYTE;BOOL bFlg = CCU_FALSE;
UINT32 ulHeadLen = CCU_485CMD_RESPONSE_HEADLENGTH;CHECK;
UCHAR acuData[MAX_485_DATA_LEN] = {0};
UINT32 ulLen = MAX_485_DATA_LEN;
UCHAR ucSendTime = 0;
UCHAR ucQuyCnt = CCU_NULL_BYTE;ulRet;CHECK;
CHAR acPrintBuf[128] = {0};UINT32 ulResId = 0;iCount;i ;
UINT8 ucLoopBuf[SERIAL_TEST_LEN] =
{0x55,0xaa,0x55,0xaa,0x55,0xaa,0x55,0xaa,0x55,0xaa};
UINT8 ucReadbuffer[MAX_485_DATA_LEN] = {0};
UINT32 ulResId = 0;i;iCount;
UINT8 ucLoopData1 = 0x55;
UINT8 ucLoopData2 = 0xaa;
UINT8 ucReadbuffer[MAX_485_DATA_LEN] = {0};
ulRet;UINT32 i = 0;ulRet;
UINT32 ulResId = 0;UINT32 i = 0;ulRet;UINT8 ucReadbuffer[MAX_485_DATA_LEN] = {0};
UINT8 ucLoopBuf[SERIAL_TEST_LEN] = {0x55,0xaa,0x55,0xaa,0x55,0xaa,0x55,0xaa,0x55,0xaa};
UINT32 i = 0;ulRet;
CHECK;
INT32 iCount = 0;UINT32 ulRevCount = 0;
UCHAR ucReadbuffer1[MAX_485_DATA_LEN] = {0};
UCHAR ucReadbuffer2[MAX_485_DATA_LEN] = {0};
COMM_POLL_STATE_ENUM enPollState = POLL_STATE_INIT;
if ( CCU_NULL_PTR == pRecvFunc )
return CCU_ERR;
if ( CCU_NULL_PTR == g_pRevFun)
g_pRevFun = pRecvFunc;
return CCU_OK;
else
return CCU_ERR;
ulRet;UINT32 i = 0;
pthread_t rs485thread0,01,02,03;ulRet;static UCHAR ucChan0 = 0;1 = 1;2 = 2;3 = 3;
ulRet;
COMM_DEBUG_PRINT("\r\nM[%d]--> 0x%02x", ucChanNum, ucByte); return CCU_OK;
http://cid-3ba16e78a53d2d3d.office.live.com/self.aspx/VC/SaveControlCondition.zip
测试代码:
BOOL check1 ;
BOOL check2 ;BOOL radio1 ;
BOOL radio2 ;
BOOL radio3 ;int com1;
int com2;//保存
bool CDemoDlg::write()
{
try
{
CMyFile file(_T("s.txt"),_T("wb")); file.Write(check1);
file.Write(check2); file.Write(radio1);
file.Write(radio2);
file.Write(radio3); file.Write(com1);
file.Write(com2); return true;
}
catch(int ErrorCode)
{
return false;
}
}//读取
bool CDemoDlg::read()
{
try
{
CMyFile file(_T("s.txt"),_T("rb")); file.Read(check1);
file.Read(check2); file.Read(radio1);
file.Read(radio2);
file.Read(radio3); file.Read(com1);
file.Read(com2); return true;
}
catch(int ErrorCode)
{
return false;
}
}void CDemoDlg::OnButton1()
{
check1 = ((CButton*)GetDlgItem(IDC_CHECK1))->GetCheck();
check2 = ((CButton*)GetDlgItem(IDC_CHECK2))->GetCheck(); radio1 = ((CButton*)GetDlgItem(IDC_RADIO1))->GetCheck();
radio2 = ((CButton*)GetDlgItem(IDC_RADIO2))->GetCheck();
radio3 = ((CButton*)GetDlgItem(IDC_RADIO3))->GetCheck(); com1 = ((CComboBox*)GetDlgItem(IDC_COMBO1))->GetCurSel();
com2 = ((CComboBox*)GetDlgItem(IDC_COMBO2))->GetCurSel(); write();
}
void CDemoDlg::OnButton2()
{
if (!read())
return; ((CButton*)GetDlgItem(IDC_CHECK1))->SetCheck(check1);
((CButton*)GetDlgItem(IDC_CHECK2))->SetCheck(check2); ((CButton*)GetDlgItem(IDC_RADIO1))->SetCheck(radio1);
((CButton*)GetDlgItem(IDC_RADIO2))->SetCheck(radio2);
((CButton*)GetDlgItem(IDC_RADIO3))->SetCheck(radio3); ((CComboBox*)GetDlgItem(IDC_COMBO1))->SetCurSel(com1);
((CComboBox*)GetDlgItem(IDC_COMBO2))->SetCurSel(com2);}
/****************************************************************************
读取和保存文件一、CMyFile实现了序列化保存和读取文件。你可以使用它保存和读取常用数据类型,如
1. int、double、shor、等固定大小的变量,
2. 字符串变量,c风格字符串,stl 字符串string,MFC字符串CString
3. 保存stl的vector<T> 类型的变量
4. 保存结构体或结构体数组
5. 支持UNICODE二、更新日期:2011-8-17
请浏览一下网址看是否有所更新:
http://blog.csdn.net/jacky_qiu/archive/2010/12/24/6096722.aspx三、调用示例:
通常你需要写两个函数write()和read()用于读取和保存。
里面还需要用try和catch来捕获保存和读取时可能出现的异常。
格式如下: //保存
bool write2()
{
int a=89;
TCHAR c_str[100]=_T("this is a c style string"); try
{
CMyFile file(_T("s.txt"),_T("wb"));
file.Write(a);
file.Write(c_str);
return true;
}
catch(int ErrorCode)
{
return false;
}
} //读取
bool read2()
{
int a;
TCHAR c_str[100]={0}; try
{
CMyFile file(_T("s.txt"),_T("rb"));
file.Read(a);
file.Read(c_str,sizeof(c_str)/sizeof(TCHAR));
return true;
}
catch(int ErrorCode)
{
return false;
}
}****************************************************************************/#ifndef _MY_FILE_H_
#define _MY_FILE_H_#include <vector>
#include <string>
#include <TCHAR.h>//兼容ANSI和UNICODE
#ifdef _UNICODE
#define __fopen _wfopen
#define __string wstring
#else
#define __fopen fopen
#define __string string
#endif
#define READ_MAX_NUM 100000class CMyFile
{
public:
FILE* fp; CMyFile(const TCHAR* filename, const TCHAR* mode )
{
fp=__fopen(filename,mode);
if (fp==NULL) throw int(0);
} ~CMyFile() { fclose(fp); } /****************************************************************************
重新定位到文件开头
****************************************************************************/
void fpReset() {rewind(fp);}
/****************************************************************************
保存vector
****************************************************************************/
template <class T> void WriteVector(std::vector<T>& vec)
{
UINT count=vec.size(); //保存vector元素的个数
fwrite(&count,sizeof(count),1,fp); //一定要先判断,否则下面用到 &vec[0] 会出错
if (count==0)
return; if (count!=fwrite(&vec[0],sizeof(T),count,fp)) //保存vector的所有元素
{
fclose(fp);
throw int(1);
}
} /****************************************************************************
读取vector
****************************************************************************/
template <class T> void ReadVector(std::vector<T>& vec)
{
UINT count=0; //一定要是UINT,否知有可能会出现负数
fread(&count,sizeof(count),1,fp); //获取vector元素的个数 //判断count是否超过READ_MAX_NUM
if (count>READ_MAX_NUM)
{
fclose(fp);
throw int(2);
} vec.resize(count); //设置Data的长度 //一定要先判断,否则下面用到 &vec[0] 会出错
if (count==0)
return; if ( count!=fread(&vec[0],sizeof(T),count,fp) )
{
fclose(fp);
throw int(3);
}
}
/****************************************************************************
保存结构体,包括int double 之类的固定大小的变量
****************************************************************************/
template <class T> void Write(T& Data)
{
if (1!=fwrite(&Data,sizeof(T),1,fp))
{
fclose(fp);
throw int(4);
}
}
/****************************************************************************
读取结构体,包括int double 之类的固定大小的变量
****************************************************************************/
template <class T> void Read(T& Data)
{
if( 1!=fread(&Data,sizeof(T),1,fp))
{
fclose(fp);
throw int(5);
}
}
/****************************************************************************
保存string字符串
****************************************************************************/
void Write(std::__string& str)
{
UINT count=str.size(); /*trace2("write string",count);*/
fwrite(&count,sizeof(count),1,fp);
if (count!=fwrite(str.c_str(),sizeof(TCHAR),count,fp))
{
fclose(fp);
throw int(6);
}
} /****************************************************************************
读取字符串并放在一个string里
****************************************************************************/
void Read(std::__string& str)
{
UINT count=0;
fread(&count,sizeof(count),1,fp); //判断count是否超过READ_MAX_NUM
if (count>READ_MAX_NUM)
{
fclose(fp);
throw int(7);
} TCHAR* NewStr;
NewStr=new TCHAR[(count+1)*sizeof(TCHAR)];
fread(NewStr,sizeof(TCHAR),count,fp);
NewStr[count]=NULL; str=NewStr;
delete[] NewStr;
}#ifdef __AFXWIN_H__
/****************************************************************************
保存CString字符串
****************************************************************************/
void Write(CString& str)
{
UINT count=str.GetLength(); /*trace2("write string",count);*/
fwrite(&count,sizeof(count),1,fp);
if (count!=fwrite(str,sizeof(TCHAR),count,fp))
{
fclose(fp);
throw int(8);
}
} /****************************************************************************
读取字符串并放在一个CString里
****************************************************************************/
void Read(CString& str)
{
UINT count=0;
fread(&count,sizeof(count),1,fp); //判断count是否超过READ_MAX_NUM
if (count>READ_MAX_NUM)
{
fclose(fp);
throw int(9);
} TCHAR* NewStr;
NewStr=new TCHAR[(count+1)*sizeof(TCHAR)];
fread(NewStr,sizeof(TCHAR),count,fp);
NewStr[count]=NULL; str=NewStr;
delete[] NewStr;
}
#endif
/****************************************************************************
保存字符串
****************************************************************************/
void Write(TCHAR* str)
{
UINT count=_tcslen(str); /*trace2("write string",count);*/
fwrite(&count,sizeof(count),1,fp);
if (count!=fwrite(str,sizeof(TCHAR),count,fp))
{
fclose(fp);
throw int(10);
}
} /****************************************************************************
读取字符串
max包括了空字符,所以拷贝到str的最大字符数为max-1
****************************************************************************/
UINT Read(TCHAR* str,unsigned int max)
{
UINT count=0;
fread(&count,sizeof(count),1,fp); /*trace("read string",count,max);*/ //判断count是否超过READ_MAX_NUM
if (count>READ_MAX_NUM)
{
fclose(fp);
throw int(11);
} if (count>max-1)
{
fclose(fp);
throw int(12);
} if( count!=fread(str,sizeof(TCHAR),count,fp))
{
fclose(fp);
throw int(13);
}
str[count]=_T('\0');
return count;
}
/****************************************************************************
读取字符串(使用完后需手动删除,delete[] str)
当读取的是一个空字符串(Write时只写了一个count=0进入文件),
则Read会new一个字符NULL,所以我们可以安全引用此字符串指针。
****************************************************************************/
TCHAR* Read()
{
UINT count=0;
fread(&count,sizeof(count),1,fp); /*trace2("read string",count);*/
//判断count是否超过READ_MAX_NUM
if (count>READ_MAX_NUM)
{
fclose(fp);
throw int(14);
}
TCHAR* str=new TCHAR[count+1];
if( count!=fread(str,sizeof(TCHAR),count,fp))
{
delete[] str;
fclose(fp);
throw int(15);
}
str[count]=_T('\0');//length=100时,PasteText[length]就是第101个字符,也就是最后一个NULL
return str;
}
/****************************************************************************
保存结构体数组
即使pStruct为NULL,也可以调用此函数
****************************************************************************/
template <class T> void Write(T* pStruct,UINT count)
{
fwrite(&count,sizeof(count),1,fp);
if (count!=fwrite(pStruct,sizeof(T),count,fp))
{
fclose(fp);
throw int(16);
}
}
/****************************************************************************
读取结构体数组
每次调用Read获得的结构体数组,在使用前必须判断获取的数组是否为空
****************************************************************************/
template <class T> void Read(T* &pStruct,UINT& count)
{
fread(&count,sizeof(count),1,fp); /*trace2("read struct",count);*/ //判断count是否超过READ_MAX_NUM
if (count>READ_MAX_NUM)
{
count=0;
fclose(fp);
throw int(17);
}
if (count==0)
{
pStruct=NULL;
}
pStruct=new T[count];
if( count!=fread(pStruct,sizeof(T),count,fp))
{
delete[] pStruct;
fclose(fp);
throw int(18);
}
}
};
#endif // #define _MY_FILE_H_
当然你要根据具体的需要设置文件结构
用int和xml也可以
然后就是读写文件了
CRegKey reg; reg.Create(HKEY_CURRENT_USER,"******");
reg.SetValue((LPCTSTR)m_path_1,"**");
reg.SetValue((LPCTSTR)m_path_2,"**"); reg.Close();读取:
CRegKey rk1;
LPCTSTR lp="****"; if(rk1.Open(HKEY_CURRENT_USER,lp)== ERROR_SUCCESS)
{
char path1[MAX_PATH];
DWORD num = MAX_PATH;
CString path;
if(rk1.QueryValue( path1,"****", &num)==ERROR_SUCCESS)
{
path = path1;
SetDlgItemText(IDC_EDIT1,path);
} UpdateData(); rk1.Close();
}