⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 h20000127-24.htm

📁 对于学习很有帮助
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<html><head><script language="JavaScript">	function reset(){   for ( var i=0; i < document.Form1.elements.length; i++ )     { document.Form1.elements[i].options[0].selected=true }}function going(url){        parent.location=url;        reset();}</script><style><!--A:link {text-decoration: none; color: #000000; font-family: 宋体}A:visited {text-decoration: none; color: #000000; font-family: 宋体}A:active {text-decoration: none; font-family: 宋体}A:hover {text-decoration: underline:#ffffff; color: #ff0000}body,table {font-size: 9pt; font-family: 宋体}.H1 {font-size: 9pt ; line-height:9pt; align=center}.ourfont {font-size: 9pt ; line-height:9pt; }.ourfont1 {font-size: 9pt ; line-height:9pt; }--></style><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><title>用C++Bulider在WIN.INI中保存信息</title></head><body topmargin="0" leftmargin="0"><div align="center"><center><table border="0" cellPadding="0" cellSpacing="0" width="700"><tbody>  <tr>    <td><p class="content4"><span class="font"><img border="0" height="3" src="../../soft/pix.gif" width="700"></span></td>  </tr></tbody></table></center></div><div align="center"><center><table border="0" cellPadding="0" cellSpacing="0" width="700"><tbody>  <tr>    <td bgColor="#3399ff" width="658"><p class="content4"><span class="font">&nbsp; </span><a style="text-decoration: none; color: rgb(255,255,255)" href="../os/H00705-2.htm">操作系统</a><font color="#FFFFFF"><span class="font"><span> |</span></span></font><a style="color: rgb(255,255,255); text-decoration: none" href="../image/H00705-14.htm">图像处理</a><font color="#FFFFFF"><span>|<span class="font"> </span></span></font><a style="color: rgb(255,255,255); text-decoration: none" href="../autoCAD/H00705-19.htm">辅助设计</a><span class="font"><font color="#FFFFFF"><span> | </span></font></span><a style="color: rgb(255,255,255); text-decoration: none" href="../data/H00705-18.htm">数     据 库</a><span class="font"><font color="#FFFFFF"><span> | </span></font></span><a style="color: rgb(255,255,255); text-decoration: none" href="H00706-1.htm">高级编程</a><span class="font"><font color="#FFFFFF"><span> | </span></font></span><a style="color: rgb(255,255,255); text-decoration: none" href="../netuse/H00705-20.htm">网络应用</a><font color="#FFFFFF"><span><span class="font"> | </span></span></font><a style="color: rgb(255,255,255); text-decoration: none" href="../webpage/H00705-21.htm">网页制作</a><font color="#FFFFFF"><span><span class="font"> |</span></span></font> <a style="color: rgb(255,255,255); text-decoration: none" href="../technic/H00706-3.htm">技术交流</a>     &nbsp; <font color="#000000"><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;     <a style="COLOR: rgb(255,255,255); TEXT-DECORATION: none" href="http://202.100.13.77/clslyb/default.asp?clsid=jiaoc">过客留言</a></strong></font></td>    <td bgColor="#3399ff" width="32"><p align="center" class="content4"><a href="http://202.100.13.77/clslyb/default.asp?clsid=jiaoc"><img src="../../soft/email.gif" alt="email.gif (493 字节)" border="0" WIDTH="20" HEIGHT="19"></a></td>  </tr></tbody></table></center></div><div align="center"><center><table border="0" cellPadding="0" cellSpacing="0" width="700"><tbody>  <tr>    <td align="middle" bgColor="#3399ff" vAlign="top" width="130"></td>    <td vAlign="top" width="20"><p class="content4"><img src="../../soft/jiao2.gif" alt="jiao2.gif (176 字节)" WIDTH="14" HEIGHT="14"></td>    <td vAlign="top" width="550"><p class="content4" align="center"><span class="font"></span></td>  </tr></tbody></table></center></div><div align="center"><center><table border="0" cellPadding="0" cellSpacing="0" width="700"><tbody>  <tr>    <td bgColor="#000000" height="1" width="130"><p class="content4"><span class="font"></span></td>    <td height="1" width="20"><p class="content4"><span class="font"></span></td>    <td bgColor="#000000" height="1" width="200"><p class="content4"><span class="font"></span></td>    <td bgColor="#000000" height="1" width="200"><p class="content4"><span class="font"></span></td>    <td height="1" width="10"><p class="content4"><span class="font"></span></td>    <td bgColor="#000000" height="1" width="140"><p class="content4"><span class="font"></span></td>  </tr>  <tr>    <td align="middle" bgColor="#3399FF" vAlign="top" width="130"><p class="content4" align="center"><span class="font"><br>    </span></p>    <p class="content4" align="center"><strong><a style="text-decoration: none; color: rgb(255,255,255)" href="../os/H00705-2.htm">操作系统</a></strong><br>    <br>    <strong><a style="color: rgb(255,255,255); text-decoration: none" href="../image/H00705-14.htm">图像处理</a></strong><br>    <font color="#ff0000"><b><br>    </b></font><strong><a style="color: rgb(255,255,255); text-decoration: none" href="../autoCAD/H00705-19.htm">辅助设计</a></strong><br>    <br>    <strong><a style="color: rgb(255,255,255); text-decoration: none" href="../data/H00705-18.htm">数 据 库</a></strong><br>    <br>    <strong><a style="color: rgb(255,255,255); text-decoration: none" href="H00706-1.htm">高级编程</a></strong><br>    <br>    <strong><a style="color: rgb(255,255,255); text-decoration: none" href="../netuse/H00705-20.htm">网络应用</a></strong><br>    <br>    <strong><a style="color: rgb(255,255,255); text-decoration: none" href="../webpage/H00705-21.htm">网页制作</a></strong><br>    <br>    <strong><a style="color: rgb(255,255,255); text-decoration: none" href="../technic/H00706-3.htm">技术交流</a></strong></p>    <div align="center"><center><table border="0" cellPadding="0" cellSpacing="0" width="100%"><tbody></tbody>    </table>    </center></div><table border="0" cellPadding="0" cellSpacing="0" width="100%"><tbody></tbody>    </table>    </td>    <td align="middle" vAlign="top" width="20"><p align="left" class="content4"><span class="font"><span><br>    </span></span></td>    <td colSpan="2" vAlign="top" width="400"> <p> <font FACE="宋体" SIZE="2"></p>    <p align="center">用C++Bulider在WIN.INI中保存信息</p>    <p>(西安 崔战兵)</p>    <p>  现在许多软件把程序中需要的数据保存在注册表中,这样当用户装的软件越来越多时,致使注册表越来越庞大,容易使系统出错。当然,微软也建议在注册表中保存数据,但当我们需要保存的数据不多时完全可以把数据保存在WIN.INI中,这样可以很方便地维护,实现方法相对来说比较简单。下面我以Borland     C++ Builder为例来说说如何实现。</p>    <p>  原理其实很简单,只需调用API的 WriteProfileString和GetProfileInt函数就可以了。这两个函数的原型是:BOOL     WriteProfileString(LPCTSTR lpAppName,LPCTSTR lpKeyName,LPCTSTR lpString );</p>    <p>  UINT GetProfileInt(LPCTSTR lpAppName,LPCTSTR lpKeyName,INT nDefault);</p>    <p>  其中lpAppName指在WIN.INI中段的名字,即用[]括起来的字符串,lpKeyName指在这个段中每一个项目的名字,lpString指这个项目的值,即</font><font FACE="Times New Roman" SIZE="2">“</font><font FACE="宋体" SIZE="2">=</font><font FACE="Times New Roman" SIZE="2">”</font><font FACE="宋体" SIZE="2">后的数, nDefault为当GetProfileInt没有找到lpAppName和lpKeyName时返回的值,即缺省值,前者返回为布尔值(true     或 false),后者返回为无符号整形值。当在WriteProfileString函数中     lpKeyName 为空(NULL)时,则清除这个段的全部内容,lpString     为空时,则清除这一项目的内容,即这一行将清除掉。</p>    <p>  下面举一例子来说明这两个函数的用法。新建一个应用程序,在Form1上放两个Edit和三个Button,其中Edit的Text为空,三个Button的Caption分别为</font><font FACE="Times New Roman" SIZE="2">“</font><font FACE="宋体" SIZE="2">添加</font><font FACE="Times New Roman" SIZE="2">”</font><font FACE="宋体" SIZE="2">、</font><font FACE="Times New Roman" SIZE="2">“</font><font FACE="宋体" SIZE="2">查看</font><font FACE="Times New Roman" SIZE="2">”</font><font FACE="宋体" SIZE="2">、</font><font FACE="Times New Roman" SIZE="2">“</font><font FACE="宋体" SIZE="2">清除</font><font FACE="Times New Roman" SIZE="2">”</font><font FACE="宋体" SIZE="2">。双击</font><font FACE="Times New Roman" SIZE="2">“</font><font FACE="宋体" SIZE="2">添加</font><font FACE="Times New Roman" SIZE="2">”</font><font FACE="宋体" SIZE="2">按钮加入下面代码:</p>    <p>  WriteProfileString(</font><font FACE="Times New Roman" SIZE="2">“</font><font FACE="宋体" SIZE="2">例子程序</font><font FACE="Times New Roman" SIZE="2">”</font><font FACE="宋体" SIZE="2">,</font><font FACE="Times New Roman" SIZE="2">“</font><font FACE="宋体" SIZE="2">项目</font><font FACE="Times New Roman" SIZE="2">”</font><font FACE="宋体" SIZE="2">,Edit1→Text.c_str());</p>    <p>  双击</font><font FACE="Times New Roman" SIZE="2">“</font><font FACE="宋体" SIZE="2">查看</font><font FACE="Times New Roman" SIZE="2">”</font><font FACE="宋体" SIZE="2">按钮加入如下代码:</p>    <p>  unsigned int Temp;</p>    <p>  Temp=GetProfileInt(</font><font FACE="Times New Roman" SIZE="2">“</font><font FACE="宋体" SIZE="2">例子程序</font><font FACE="Times New Roman" SIZE="2">”</font><font FACE="宋体" SIZE="2">,</font><font FACE="Times New Roman" SIZE="2">“</font><font FACE="宋体" SIZE="2">项目</font><font FACE="Times New Roman" SIZE="2">”</font><font FACE="宋体" SIZE="2">,100);</p>    <p>  Edit2→Text=IntToStr(Temp);</p>    <p>  双击</font><font FACE="Times New Roman" SIZE="2">“</font><font FACE="宋体" SIZE="2">清除</font><font FACE="Times New Roman" SIZE="2">”</font><font FACE="宋体" SIZE="2">按钮加入如下代码:</p>    <p>  WriteProfileString(</font><font FACE="Times New Roman" SIZE="2">“</font><font FACE="宋体" SIZE="2">例子程序</font><font FACE="Times New Roman" SIZE="2">”</font><font FACE="宋体" SIZE="2">,NULL,NULL);</p>    <p>  然后按F9键运行程序。</p>    <p>  下来可以检验一下程序的正确性。在Edit1中输入数字,如</font><font FACE="Times New Roman" SIZE="2">“</font><font FACE="宋体" SIZE="2">3265</font><font FACE="Times New Roman" SIZE="2">”</font><font FACE="宋体" SIZE="2">,按</font><font FACE="Times New Roman" SIZE="2">“</font><font FACE="宋体" SIZE="2">添加</font><font FACE="Times New Roman" SIZE="2">”</font><font FACE="宋体" SIZE="2">按钮,这时运行</font><font FACE="Times New Roman" SIZE="2">“</font><font FACE="宋体" SIZE="2">sysedit</font><font FACE="Times New Roman" SIZE="2">”</font><font FACE="宋体" SIZE="2">来查看</font><font FACE="Times New Roman" SIZE="2">“</font><font FACE="宋体" SIZE="2">WIN.INI</font><font FACE="Times New Roman" SIZE="2">”</font><font FACE="宋体" SIZE="2">文件的最后面,可以看到加入了如下内容:</p>    <p>  [例子程序]</p>    <p>  项目=3265</p>    <p>  其中</font><font FACE="Times New Roman" SIZE="2">“</font><font FACE="宋体" SIZE="2">[]</font><font FACE="Times New Roman" SIZE="2">”</font><font FACE="宋体" SIZE="2">和</font><font FACE="Times New Roman" SIZE="2">“</font><font FACE="宋体" SIZE="2">=</font><font FACE="Times New Roman" SIZE="2">”</font><font FACE="宋体" SIZE="2">是函数自动加上的。按下</font><font FACE="Times New Roman" SIZE="2">“</font><font FACE="宋体" SIZE="2">查看</font><font FACE="Times New Roman" SIZE="2">”</font><font FACE="宋体" SIZE="2">按钮,在Edit2中出现</font><font FACE="Times New Roman" SIZE="2">“</font><font FACE="宋体" SIZE="2">3265</font><font FACE="Times New Roman" SIZE="2">”</font><font FACE="宋体" SIZE="2">,当按下</font><font FACE="Times New Roman" SIZE="2">“</font><font FACE="宋体" SIZE="2">清除</font><font FACE="Times New Roman" SIZE="2">”</font><font FACE="宋体" SIZE="2">按钮可清除添加的部分。经过查看可知程序已达到预期的目的。</p>    <p>  喜爱编程的朋友可以把上述方法应用到自己的程序中去,来达到保存数据信息的作用。当确实要把信息保存到注册表中,可以在C++     Builder中定义一个TRegistry类的对象来进行相关的操作,或者直接调用Windows的API函数,具体如何编程大家可以参阅相关资料或者同我联系,我的E-mail: 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -