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

📄 ch08.htm

📁 VC使用所有细节的逻列
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<TD>

<P>Allows other processes read or write operations on the file</P>

<TR>

<TD>

<pre><font color="#008000">CFile::shareDenyRead</font></pre>

<TD>

<P>Disallows read operations by other processes</P>

<TR>

<TD>

<pre><font color="#008000">CFile::shareDenyWrite</font></pre>

<TD>

<P>Disallows write operations by other processes</P>

<TR>

<TD>

<pre><font color="#008000">CFile::shareExclusive</font></pre>

<TD>

<P>Denies all access to other processes</P>

<TR>

<TD>

<pre><font color="#008000">CFile::typeBinary</font></pre>

<TD>

<P>Sets binary mode for the file</P>

<TR>

<TD>

<pre><font color="#008000">CFile::typeText</font></pre>

<TD>

<P>Sets text mode for the file</P></TABLE>

<P>If you continue to examine the code in Listing 8.12, you will see that after creating the file, <font color="#008000">OnFileSave()</font> gets the length of the current message and writes it out to the file by calling the <font 
color="#008000">CFile</font> object's <font color="#008000">Write()</font> member function. This function requires, as arguments, a pointer to the buffer containing the data to write and the number of bytes to write. Notice the <font 
color="#008000">LPCTSTR</font> casting operator in the call to <font color="#008000">Write()</font>. This operator is defined by the <font color="#008000">CString</font> class and extracts the string from the class.</P>

<P>Finally, the program calls the <font color="#008000">CFile</font> object's <font color="#008000">GetFilePath()</font> and <font color="#008000">GetLength()</font> member functions to get the file's complete path and length so that they can be displayed 
by <font color="#008000">OnDraw()</font>, not shown here. Finally a call to the view class's <font color="#008000">Invalidate()</font> function causes the window to display the new information. Notice that there is no call to <font 
color="#008000">Close()</font>&#151;the <font color="#008000">CFile</font> destructor will close the file automatically.</P>

<P>Reading from a file is not much different from writing to one, as you can see in Listing 8.13, which shows the view class's <font color="#008000">OnFileOpen()</font> member function.</P>

<p><img src="cd_rom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/cd_rom.gif" hspace=10>

<P><I>Listing 8.13&#151;</I>FILE3VIEW.CPP<I>&#151;Reading from the File</I></P>

<pre><font color="#008000">void CFile3View::OnFileOpen()</font></pre>

<pre><font color="#008000">{</font></pre>

<pre><font color="#008000">    // TODO: Add your command handler code here</font></pre>

<pre><font color="#008000">   </font></pre>

<pre><font color="#008000">    // Open the file.</font></pre>

<pre><font color="#008000">    CFile file(&quot;TESTFILE.TXT&quot;, CFile::modeRead);</font></pre>

<pre><font color="#008000">    // Read data from the file.</font></pre>

<pre><font color="#008000">    char s[81];</font></pre>

<pre><font color="#008000">    int bytesRead = file.Read(s, 80);</font></pre>

<pre><font color="#008000">    s[bytesRead] = 0;</font></pre>

<pre><font color="#008000">    m_message = s;</font></pre>

<pre><font color="#008000">    // Get information about the file.</font></pre>

<pre><font color="#008000">    m_filePath = file.GetFilePath();</font></pre>

<pre><font color="#008000">    m_fileLength = file.GetLength();</font></pre>

<pre><font color="#008000">    // Repaint the window.</font></pre>

<pre><font color="#008000">Invalidate();</font></pre>

<pre><font color="#008000">}</font></pre>

<P>This time the file is opened by the <font color="#008000">CFile::modeRead</font> flag, which opens the file for read operations only, after which the program creates a character buffer and calls the file object's <font color="#008000">read()</font> 
member function to read data into the buffer. The <font color="#008000">read()</font> function's two arguments are the address of the buffer and the number of bytes to read. The function returns the number of bytes actually read, which in this case is 
almost always less than the 80 requested. Using the number of bytes read, the program can add a 0 to the end of the character data, thus creating a standard C-style string that can be used to set the <font color="#008000">m_message</font> data member. As 
you can see, the <font color="#008000">OnFileOpen()</font> function calls the file object's <font color="#008000">GetFilePath()</font>, <font color="#008000">GetLength()</font>, and <font color="#008000">Close()</font> member functions exactly as <font 
color="#008000">OnFileSave()</font> did.</P>

<H3><A ID="I15" NAME="I15"><B>Creating Your Own </B><B><I>CArchive</I></B><B> Objects</B></A></H3>

<P>Although you can handle files using <font color="#008000">CFile</font> objects, you can go a step further and create your own <font color="#008000">CArchive</font> object that you can use exactly as you use the <font color="#008000">CArchive</font> 
object in the <font color="#008000">Serialize()</font> function. This lets you take advantage of <font color="#008000">Serialize</font> functions already written for other objects, passing them a reference to your own archive object.</P>

<P>To create an archive, you create a <font color="#008000">CFile</font> object and pass that object to the <font color="#008000">CArchive</font> constructor. For example, if you plan to write out objects to a file through an archive, create the archive 
like this:</P>

<pre><font color="#008000">CFile file(&quot;FILENAME.EXT&quot;, CFile::modeWrite);</font></pre>

<pre><font color="#008000">CArchive ar(&amp;file, CArchive::store);</font></pre>

<P>After creating the archive object, you can use it just like the archive objects that MFC creates for you. Since you've created it with the <font color="#008000">CArchive::store</font> flag, any calls to <font color="#008000">IsStoring()</font> return 
<font color="#008000">TRUE</font>, and the code that dumps objects to the archive executes. When you're through with the archive object, you can close both the archive and the file like this:</P>

<pre><font color="#008000">ar.Close();</font></pre>

<pre><font color="#008000">file.Close();</font></pre>

<P>If the objects go out of scope soon after you're finished with them, you can safely omit the calls to <font color="#008000">Close()</font> since both <font color="#008000">CArchive</font> and <font color="#008000">CFile</font> have <font 
color="#008000">Close()</font> calls in the destructor.</P>

<H3><A ID="I16" NAME="I16"><B>The Registry</B></A></H3>

<P>In the early days of Windows programming, applications saved settings and options in initialization files, typically with the INI extension. The days of huge <font color="#008000">WIN.INI</font> files or myriad private INI files are now gone&#151;when 
an application wants to store information about itself, it does so using a centralized system registry. And, although the Registry makes sharing information between processes easier, it can make things more confusing for the programmer. In this section, 
you uncover some of the mysteries of the Registry and learn how to manage it in your applications.</P>

<P><A ID="I17" NAME="I17"><B>How the Registry Is Set Up</B></A></P>

<P>Unlike INI files, which are plain text files that can be edited with any text editor, the Registry contains binary and ASCII information that can be edited only using the Registry Editor or special API function calls specially created for managing the 
Registry. If you've ever used the Registry Editor to browse your system's Registry, you know that it contains a huge amount of information that's organized into a tree structure. Figure 8.9 shows how the Registry appears when you first run the Registry 
Editor. (You can find the Registry Editor, called <font color="#008000">REGEDIT.EXE</font>, in your main Windows folder, or you can run it with the Start menu's Run command by choosing <U>R</U>un, typing <B>regedit</B>, and then clicking OK.)</P>

<A HREF="Jfigs09.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch08/Jfigs09.gif"><b>Fig. 8.9</b></A>

<P><I>The Registry Editor displays the Registry.</I></P>

<P>The left-hand window lists the Registry's predefined keys. The plus marks next to the keys in the tree indicate that you can &quot;open&quot; the keys and view more detailed information associated with them. Keys can have subkeys, and subkeys can 
themselves have subkeys. Any key or subkey may or may not have a value associated with it. If you explore deep enough in the hierarchy, you see a list of values in the right-hand window. In Figure 8.10, you can see the values associated with the current 
user's screen appearance. To see these values yourself, browse from <font color="#008000">HKEY_CURRENT_USER</font> to <font color="#008000">Control Panel</font> to <font color="#008000">Appearance</font> to <font color="#008000">Schemes</font>, and you 
will see the desktop schemes that are installed on your system.</P>

<A HREF="Jfigs10.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch08/Jfigs10.gif"><b>Fig. 8.10</b></A>

<P><I>The Registry is structured as a tree containing a huge amount of information.</I></P>

<P><A ID="I18" NAME="I18"><B>The Predefined Keys</B></A></P>

<P>In order to know where things are stored in the Registry, you need to know about the predefined keys and what they mean. From Figure 8.9, you can see that the six predefined keys are:</P>

<ul>

<li><font color="#008000">HKEY_CLASSES_ROOT</font>

<li><font color="#008000">HKEY_CURRENT_USER</font>

<li><font color="#008000">HKEY_LOCAL_MACHINE</font>

<li><font color="#008000">HKEY_USERS</font>

<li><font color="#008000">HKEY_CURRENT_CONFIG</font>

<li><font color="#008000">HKEY_DYN_DATA</font>

</ul>

<P>The <font color="#008000">HKEY_CLASSES_ROOT</font> key holds document types and properties, as well as class information about the various applications installed on the machine. For example, if you explored this key on your system, you'd probably find 
an entry for the DOC file extension, under which you'd find entries for the applications that can handle this type of document (see Figure 8.11).</P>

<A HREF="Jfigs11.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch08/Jfigs11.gif"><b>Fig. 8.11</b></A>

<P><I>The </I><I>HKEY_CLASSES_ROOT</I><I> key holds document information.</I></P>

<P>The <font color="#008000">HKEY_CURRENT_USER</font> key contains all the system settings the current user has established, including color schemes, printers, and program groups. The <font color="#008000">HKEY_LOCAL_MACHINE</font> key, on the other hand, 
contains status information about the computer, and the <font color="#008000">HKEY_USERS</font> key organizes information about each user of the system, as well as the default configuration. Finally, the <font color="#008000">HKEY_CURRENT_CONFIG</font> key 
holds information about the hardware configuration, and the <font color="#008000">HKEY_DYN_DATA</font> key contains information about dynamic registry data, which is data that changes frequently.</P>

<P><A ID="I19" NAME="I19"><B>Using the Registry in an MFC Application</B></A></P>

<P>Now that you know a little about the Registry, let me say that it would take an entire book to explain how to fully access and use the Registry. As you may imagine, the Win32 API features many functions for manipulating the Registry. And, if you're 
going to use those functions, you sure better know what you're doing! However, you can easily use the Registry with your MFC applications to store information that the application needs from one session to another. To make this task as easy as possible, 
MFC provides the <font color="#008000">CWinApp</font> class with the <font color="#008000">SetRegistryKey()</font> member function, which creates (or opens) a key entry in the Registry for your application. All you have to do is supply a key name (usually 
a company name) for the function to use, like this:</P>

<pre><font color="#008000">SetRegistryKey(&quot;MyCoolCompany&quot;);</font></pre>

<P>You should call <font color="#008000">SetRegistryKey()</font> in the application class's <font color="#008000">InitInstance()</font> member function, which is called once at program startup.</P>

<P>After you've called <font color="#008000">SetRegistryKey()</font>, your application can create the subkeys and values it needs by calling one of two functions. The <font color="#008000">WriteProfileString()</font> function adds string values to the 
Registry, and the <font color="#008000">WriteProfileInt()</font> function adds integer values to the Registry. To get values from the Registry, you can use the <font color="#008000">GetProfileString()</font> and <font color="#008000">GetProfileInt()</font> 
functions. (You also can use <font color="#008000">RegSetValueEx()</font> and <font color="#008000">RegQueryValueEx()</font> to set and retrieve Registry values.)</P>

<blockquote><p><img src="note.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/note.gif">

<P>When they were first written, the <font color="#008000">WriteProfileString()</font>, <font color="#008000">WriteProfileInt()</font>, <font color="#008000">GetProfileString()</font>, and <font color="#008000">GetProfileInt()</font> functions transferred 
information to and from an INI file. Used alone, they still do. But when you call <font color="#008000">SetRegistryKey()</font> first, MFC reroutes these profile functions to the Registry, making using the Registry an almost painless process.</P>

<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>

<P><A ID="I20" NAME="I20"><B>The File Demo 2 Application Revisited</B></A></P>

<P>In this chapter, you have already built an application that used the Registry. Listing 8.14 is an excerpt from <font color="#008000">CFile2App::InitInstance()</font>&#151;this code was generated by AppWizard.</P>

<P><I>Listing 8.14&#151;</I>FILE3VIEW.CPP<I>&#151;Reading from the File</I></P>

<pre><font color="#008000">     // Change the registry key under which our settings are stored.</font></pre>

<pre><font color="#008000">     // You should modify this string to be something appropriate</font></pre>

<pre><font color="#008000">     // such as the name of your company or organization.</font></pre>

<pre><font color="#008000">     SetRegistryKey(_T(&quot;Local AppWizard-Generated Applications&quot;));</font></pre>

<pre><font color="#008000">     LoadStdProfileSettings();  // Load standard INI file options (including MRU)</font></pre>

<P>MRU stands for <I>Most Recently Used</I> and refers to the list of files that appears on the <U>F</U>ile menu after you have opened files with an application. Figure 8.12 shows the Registry Editor displaying the key that stores this information, <font 
color="#008000">HKEY_CURRENT_USER\Software\Local </font><font color="#008000">AppWizard-Generated Applications\File2\Recent File List</font>. In the foreground, File2's <U>F</U>ile menu shows the single entry in the MRU list.</P>

<A HREF="Jfigs12.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch08/Jfigs12.gif"><b>Fig. 8.12</b></A>

<P><I>The most recently used files list is stored in the Registry automatically.</I></P>

<H3><A ID="I21" NAME="I21"><B>From Here</B></A><B>...</B></H3>

<P>When it comes to file handling, Visual C++ and MFC give you a number of options. The easiest way to save and load (serialize) data is to take advantage of the <font color="#008000">CArchive</font> object created for you by MFC and passed to the 
document class's <font color="#008000">Serialize()</font> function. Sometimes, however, you need to create your own persistent objects, by deriving the object's class from MFC's <font color="#008000">CObject</font> class and then adding a default 
constructor, as well as the <font color="#008000">DECLARE_SERIAL()</font> and <font color="#008000">IMPLEMENT_SERIAL()</font> macros. You can then override the <font color="#008000">Serialize()</font> function in your new class. If necessary, you can 
control file handling more directly by creating a <font color="#008000">CFile</font> object and using that object's member functions to save and load data.</P>

<P>Although not a visible component of an application, the Registry is the place where you should store data that your application needs from session to session. Using the Registry can be tricky when relying solely on the Win32 Registry functions. 
However, MFC makes dealing with the Registry as easy as writing values to the old-fashioned INI files.</P>

<P>For more information on related topics, please refer to the following chapters:</P>

<ul>

<li> <A HREF="index02.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index02.htm" target="text">Chapter 2</A>, &quot; Dialog Boxes and Controls,&quot; explains how to use dialog boxes in your applications.</P>

<li> <A HREF="index04.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index04.htm" target="text">Chapter 4</A>, &quot;Messages and Commands,&quot; describes MFC's message-mapping system, which enables you to respond to Windows messages.</P>

<li> <A HREF="index05.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index05.htm" target="text">Chapter 5</A>, &quot;Documents and Views,&quot; discusses how data is handled in an application's document and view classes.</P>

<li> <A HREF="index22.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index22.htm" target="text">Chapter 22</A>, &quot;Database Access,&quot; describes how Visual C++ programs can interact with information stored in database files.</P>

</ul>

<p><hr></p>

<center>

<p><font size=-2>

&copy; 1997, QUE Corporation, an imprint of Macmillan Publishing USA, a

Simon and Schuster Company.</font></p>

</center>

</BODY></HTML>

⌨️ 快捷键说明

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