user.ini
[initialize] ;section name
m_listbox=0 ;key and int value
name=zhiguang ;key and string value
(1) example to initialize the controls on the dialog
BOOL CModalDlg::OnInitDialog() // read values from the .ini file
{
CDialog::OnInitDialog();
CString iniFilePath;
iniFilePath=".//ini//user.ini";
// read ini for listbox
int list_pos;
list_pos=GetPrivateProfileInt("initialize","m_listbox",0,iniFilePath);
m_listbox.AddString(_T("MIT"));
m_listbox.AddString(_T("I.PAC"));
m_listbox.AddString(_T("CASEM"));
m_listbox.AddString(_T("Genric Power"));
m_listbox.SetCurSel(list_pos);
// read ini for edit
CString str_edit;
GetPrivateProfileString("initialize","IDC_EDIT1","",str_edit.GetBuffer(MAX_PATH),MAX_PATH,iniFilePath);
SetDlgItemText(IDC_EDIT1, str_edit);
// read ini for checkbox
int check_val;
check_val=GetPrivateProfileInt("initialize","m_check",0,iniFilePath);
m_check.SetCheck(check_val);
// read ini for radio
int radio_val;
radio_val=GetPrivateProfileInt("initialize","IDC_RADIO1",0,iniFilePath);
if(radio_val==1)
((CButton *)GetDlgItem(IDC_RADIO1))->SetCheck(TRUE);
else
((CButton *)GetDlgItem(IDC_RADIO2))->SetCheck(TRUE);
return TRUE;
}
(2) example to update the control values on the dialog back to the same .ini file.
void CModalDlg::OnBnClickedOk() //save the values to the .ini file
{
CString iniFilePath;
iniFilePath=".//ini//user.ini";
//write ini for listbox
int list_pos=m_listbox.GetCurSel();
CString str_pos;
str_pos.Format("%d",list_pos);
WritePrivateProfileString("initialize","m_listbox",str_pos,iniFilePath);
//write ini for editbox
CString str_edit;
GetDlgItemText(IDC_EDIT1, str_edit);
WritePrivateProfileString("initialize","IDC_EDIT1",str_edit,iniFilePath);
// read ini for checkbox
int check_val;
check_val=m_check.GetCheck();
CString str_check;
str_check.Format("%d",check_val);
WritePrivateProfileString("initialize","m_check",str_check,iniFilePath);
// read ini for radio button
int radio_val;
radio_val=((CButton *)GetDlgItem(IDC_RADIO1))->GetCheck();
CString str_radio;
str_radio.Format("%d",radio_val);
WritePrivateProfileString("initialize","IDC_RADIO1",str_radio,iniFilePath);
OnOK();
}
To read the file, we have GetPrivateProfileString and GetPrivateProfileInt for String and Int values, to write the file , we only have WritePrivateProfileString, so we should convert the format of the value into CString first before writing
No comments:
Post a Comment