在MFC的CWinApp類的啟動成員函數中,會有一個SetRegistryKey函數,在系統資料庫中為程式設定一個主鍵。配合WriteProfileInt等函數,對程式的配置檔案進行讀寫。
<code>SetRegistryKey(_T(</code><code>""</code><code>));</code><code>//有這句,讀寫将會針對INI檔案進行 </code>
<code>//指定自己的INI檔案名,否則編譯器将以程式名稱命名INI檔案 </code>
<code>free</code><code>((</code><code>LPTSTR</code><code>)theApp.m_pszProfileName); </code>
<code>theApp.m_pszProfileName = (</code><code>LPCTSTR</code><code>)</code><code>malloc</code><code>(256); </code>
<code>_tcscpy ((</code><code>LPTSTR</code><code>)theApp.m_pszProfileName ,_T(</code><code>"ScaleWeight"</code><code>));</code>
寫入配置檔案資訊,如下:
<code>void</code> <code>SecondDig::OnCancel() </code>
<code>{ </code>
<code> </code><code>CRect rect; </code>
<code> </code><code>GetWindowRect(&rect); </code>
<code> </code><code>theApp.WriteProfileInt(_T(</code><code>"InitData"</code><code>),_T(</code><code>"left"</code><code>),rect.left); </code>
<code> </code><code>theApp.WriteProfileInt(_T(</code><code>"InitData"</code><code>),_T(</code><code>"top"</code><code>),rect.top); </code>
<code> </code><code>theApp.WriteProfileInt(_T(</code><code>"InitData"</code><code>),_T(</code><code>"right"</code><code>),rect.right); </code>
<code> </code><code>theApp.WriteProfileInt(_T(</code><code>"InitData"</code><code>),_T(</code><code>"bottom"</code><code>),rect.bottom); </code>
<code> </code><code>//AfxMessageBox(_T("cancel")); </code>
<code> </code>
<code> </code><code>//DestroyWindow(); </code>
<code> </code><code>CDialogEx::OnCancel(); </code>
<code>}</code>
讀取配置檔案資訊,如下:
<code>BOOL</code> <code>SecondDig::OnInitDialog() </code>
<code> </code><code>CDialogEx::OnInitDialog(); </code>
<code> </code><code>int</code> <code>left = theApp.GetProfileInt(_T(</code><code>"InitData"</code><code>),_T(</code><code>"left"</code><code>),0); </code>
<code> </code><code>int</code> <code>top = theApp.GetProfileInt(_T(</code><code>"InitData"</code><code>),_T(</code><code>"top"</code><code>),0); </code>
<code> </code><code>int</code> <code>right = theApp.GetProfileInt(_T(</code><code>"InitData"</code><code>),_T(</code><code>"right"</code><code>),0); </code>
<code> </code><code>int</code> <code>bottom = theApp.GetProfileInt(_T(</code><code>"InitData"</code><code>),_T(</code><code>"bottom"</code><code>),0); </code>
<code> </code><code>rect.left = left; </code>
<code> </code><code>rect.top = top; </code>
<code> </code><code>rect.right = right; </code>
<code> </code><code>rect.bottom = bottom; </code>
<code> </code><code>this</code><code>->MoveWindow(&rect,</code><code>true</code><code>); </code>
<code> </code><code>return</code> <code>TRUE; </code>
<code> </code><code>// return TRUE unless you set the focus to a control </code>
<code> </code><code>// 異常: OCX 屬性頁應傳回 FALSE </code>
<code></code>
本文轉自Chinayu201451CTO部落格,原文連結:http://blog.51cto.com/9233403/1970601 ,如需轉載請自行聯系原作者