📄 ch07.htm
字号:
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">CFile::modeNoInherit</TD>
<TD ALIGN="LEFT">Disallows inheritance by a child process</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">CFile::modeNoTruncate</TD>
<TD ALIGN="LEFT">When creating the file, doesn't truncate the file if it already exists</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">CFile::modeRead</TD>
<TD ALIGN="LEFT">Allows read operations only</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">CFile::modeReadWrite</TD>
<TD ALIGN="LEFT">Allows both read and write operations</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">CFile::modeWrite</TD>
<TD ALIGN="LEFT">Allows write operations only</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">CFile::shareCompat</TD>
<TD ALIGN="LEFT">Allows other processes to open the file</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">CFile::shareDenyNone</TD>
<TD ALIGN="LEFT">Allows other processes read or write operations on the file</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">CFile::shareDenyRead</TD>
<TD ALIGN="LEFT">Disallows read operations by other processes</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">CFile::shareDenyWrite</TD>
<TD ALIGN="LEFT">Disallows write operations by other processes</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">CFile::shareExclusive</TD>
<TD ALIGN="LEFT">Denies all access to other processes</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">CFile::typeBinary</TD>
<TD ALIGN="LEFT">Sets binary mode for the file</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">CFile::typeText</TD>
<TD ALIGN="LEFT">Sets text mode for the file</TD>
</TR>
</TABLE>
</P>
<P>CFile::Write() takes a pointer to the buffer containing the data to write and
the number of bytes to write. Notice the LPCTSTR casting operator in the call to
Write(). This operator is defined by the CString class and extracts the string from
the class.</P>
<P>One other thing about the code snippet: There is no call to Close()--the CFile
destructor closes the file automatically when file goes out of scope.</P>
<P>Reading from a file isn't much different from writing to one:</P>
<P>
<PRE> // Open the file.
CFile file("TESTFILE.TXT", CFile::modeRead);
// Read data from the file.
char s[81];
int bytesRead = file.Read(s, 80);
s[bytesRead] = 0;
CString message = s;
</PRE>
<P>This time the file is opened by the CFile::modeRead flag, which opens the file
for read operations only, after which the code creates a character buffer and calls
the file object's Read() member function to read data into the buffer. The Read()
function's two arguments are the buffer's address 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. By 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 a CString variable.</P>
<P>The code snippets you've just seen use a hard-coded filename. To get filenames
from your user with little effort, be sure to look up the MFC class CFileDialog in
the online help. It's simple to use and adds a very nice touch to your programs.</P>
<P>
<H2><A NAME="Heading11"></A>Creating Your Own CArchive Objects</H2>
<P>Although you can use CFile objects to read from and write to files, you can also
go a step farther and create your own CArchive object and use it exactly as you use
the CArchive object in the Serialize() function. This lets you take advantage of
Serialize functions already written for other objects, passing them a reference to
your own archive object.</P>
<P>To create an archive, create a CFile object and pass it to the CArchive constructor.
For example, if you plan to write out objects to a file through an archive, create
the archive like this:</P>
<P>
<PRE>CFile file("FILENAME.EXT", CFile::modeWrite);
CArchive ar(&file, CArchive::store);
</PRE>
<P>After creating the archive object, you can use it just like the archive objects
that MFC creates for you, for example, calling Serialize() yourself and passing the
archive to it. Because you created the archive with the CArchive::store flag, any
calls to IsStoring() return TRUE, and the code that dumps objects to the archive
executes. When you're through with the archive object, you can close the archive
and the file like this:</P>
<P>
<PRE>ar.Close();
file.Close();
</PRE>
<P>If the objects go out of scope soon after you're finished with them, you can safely
omit the calls to Close() because both CArchive and CFile have Close() calls in the
destructor.</P>
<P>
<H2><A NAME="Heading12"></A>Using the Registry</H2>
<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 WIN.INI
files or myriad private .INI files are now gone--when an application wants to store
information about itself, it does so by using a centralized system Registry. Although
the Registry makes sharing information between processes easier, it can make things
more confusing for programmers. In this section, you uncover some of the mysteries
of the Registry and learn how to manage it in your applications.</P>
<P>
<H3><A NAME="Heading13"></A>How the Registry Is Set Up</H3>
<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
by using the Registry Editor or special API function calls created specifically 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 7.4 shows how the Registry appears when you first run
the Registry Editor. (On Windows 95, you can find the Registry Editor, REGEDIT.EXE,
in your main Windows folder, or you can run it from the Start menu by choosing Run,
typing <B>regedit</B>, and then clicking OK. Under Windows NT, it's REGEDT32.EXE.)</P>
<P>The far left window lists the Registry's predefined keys. The plus marks next
to the keys in the tree indicate that you can open the keys and view more detailed
information associated with them. Keys can have subkeys, and subkeys themselves can
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 far right
window. In Figure 7.5, you can see the values associated with the current user's
screen appearance. To see these values yourself, browse from HKEY_CURRENT_USER to
Control Panel to Appearance to Schemes, and you'll see the desktop schemes installed
on your system.</P>
<P><A HREF="javascript:popUp('07uvc04.gif')"><B>FIG. 7.4</B></A><B> </B><I>The Registry
Editor displays the Registry.</I></P>
<P><A HREF="javascript:popUp('07uvc05.gif')"><B>FIG. 7.5</B></A><B> </B><I>The Registry
is structured as a tree containing a huge amount of information.</I></P>
<P>
<H3><BR>
<B>The Predefined Keys</B></H3>
<H3><A NAME="Heading14"></A>The Predefined Keys</H3>
<P>To know where things are stored in the Registry, you need to know about the predefined
keys and what they mean. From Figure 7.4, you can see that the six predefined keys
are</P>
<UL>
<LI>HKEY_CLASSES_ROOT
<P>
<LI>HKEY_CURRENT_USER
<P>
<LI>HKEY_LOCAL_MACHINE
<P>
<LI>HKEY_USERS
<P>
<LI>HKEY_CURRENT_CONFIG
<P>
<LI>HKEY_DYN_DATA
</UL>
<P>The HKEY_CLASSES_ROOT 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 7.6).</P>
<P><A HREF="javascript:popUp('07uvc06.gif')"><B>FIG. 7.6</B></A><B> </B><I>The HKEY_CLASSES_ROOT
key holds document information.</I></P>
<P>The HKEY_CURRENT_USER key contains all the system settings the current user has
established, including color schemes, printers, and program groups. The HKEY_LOCAL_MACHINE
key, on the other hand, contains status information about the computer, and the HKEY_USERS
key organizes information about each user of the system, as well as the default configuration.
Finally, the HKEY_CURRENT_CONFIG key holds information about the hardware configuration,
and the HKEY_DYN_DATA key contains information about dynamic Registry data, which
is data that changes frequently. (You may not always see this key on your system.)</P>
<P>
<H3><A NAME="Heading15"></A>Using the Registry in an MFC Application</H3>
<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 it. As you may imagine, the Win32
API features many functions for manipulating the Registry. If you're going to use
those functions, you had better know what you're doing! Invalid Registry settings
can crash your machine, make it unbootable, and perhaps force you to reinstall Windows
to recover.</P>
<P>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 CWinApp class with the SetRegistryKey() 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>
<P>
<PRE>SetRegistryKey("MyCoolCompany");
</PRE>
<P>You should call SetRegistryKey() in the application class's InitInstance() member
function, which is called once at program startup.</P>
<P>After you call SetRegistryKey(), your application can create the subkeys and values
it needs by calling one of two functions. The WriteProfileString() function adds
string values to the Registry, and the WriteProfileInt() function adds integer values
to the Registry. To get values from the Registry, you can use the GetProfileString()
and GetProfileInt() functions. (You also can use RegSetValueEx() and RegQueryValueEx()
to set and retrieve Registry values.)</P>
<BLOCKQUOTE>
<P>
<HR>
<strong>NOTE:</strong> When they were first written, the WriteProfileString(), WriteProfileInt(),
GetProfileString(), and GetProfileInt() functions transferred information to and
from an .INI file. Used alone, they still do. But when you call SetRegistryKey()
first, MFC reroutes these profile functions to the Registry, making using the Registry
an almost painless process. 
<HR>
</BLOCKQUOTE>
<H3><A NAME="Heading16"></A>The Sample Applications Revisited</H3>
<P>In this chapter, you've already built applications that used the Registry. Here's
an excerpt from CMultiStringApp::InitInstance()--this code was generated by AppWizard
and is also in CFileDemoApp::InitInstance().</P>
<P>
<PRE>// Change the registry key under which our settings are stored.
// You should modify this string to be something appropriate
// such as the name of your company or organization.
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
LoadStdProfileSettings(); // Load standard INI file options (including MRU)
</PRE>
<P>MRU stands for <I>Most Recently Used</I> and refers to the list of files that
appears on the File menu after you open files with an application. Figure 7.7 shows
the Registry Editor displaying the key that stores this information, HKEY_CURRENT_USER\Software\Local
AppWizard-Generated Applications\MultiString\Recent File List. In the foreground,
MultiString's File menu shows the single entry in the MRU list.</P>
<P><A HREF="javascript:popUp('07uvc07.gif')"><B>FIG. 7.7</B></A><B> </B><I>The most
recently used files list is stored in the Registry automatically.</I></P>
<H1><I></I></H1>
<CENTER>
<P>
<HR>
<A HREF="ch06.htm" tppabs="http://www.fintech.ru/library/prog/SEUsingVC6/ch06/ch06.htm"><IMG SRC="previous.gif" tppabs="http://www.fintech.ru/library/prog/SEUsingVC6/button/previous.gif" WIDTH="128" HEIGHT="28"
ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="ch08.htm" tppabs="http://www.fintech.ru/library/prog/SEUsingVC6/ch08/ch08.htm"><IMG
SRC="next.gif" tppabs="http://www.fintech.ru/library/prog/SEUsingVC6/button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"
BORDER="0"></A><A HREF="index.htm" tppabs="http://www.fintech.ru/library/prog/SEUsingVC6/index.htm"><IMG SRC="contents.gif" tppabs="http://www.fintech.ru/library/prog/SEUsingVC6/button/contents.gif" WIDTH="128"
HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A> <BR>
</P>
<P>© <A HREF="copy.htm" tppabs="http://www.fintech.ru/library/prog/SEUsingVC6/copy.htm">Copyright</A>, Macmillan Computer Publishing. All
rights reserved.
</CENTER>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -