📄 168-171.html
字号:
<BR>
<I>(Publisher: John Wiley & Sons, Inc.)</I>
<BR>
Author(s): Robert Burdick
<BR>
ISBN: 0471327476
<BR>
Publication Date: 03/01/99
</FONT>
<P>
<form name="Search" method="GET" action="http://search.earthweb.com/search97/search_redir.cgi">
<INPUT TYPE="hidden" NAME="Action" VALUE="Search">
<INPUT TYPE="hidden" NAME="SearchPage" VALUE="http://search.earthweb.com/search97/samples/forms/srchdemo.htm">
<INPUT TYPE="hidden" NAME="Collection" VALUE="ITK">
<INPUT TYPE="hidden" NAME="ResultTemplate" VALUE="itk-simple-intrabook.hts">
<INPUT TYPE="hidden" NAME="ViewTemplate" VALUE="view.hts">
<font face="arial, helvetica" size=2><b>Search this book:</b></font><br>
<INPUT NAME="queryText" size=50 VALUE=""> <input type="submit" name="submitbutton" value="Go!">
<INPUT type=hidden NAME="section_on" VALUE="on">
<INPUT type=hidden NAME="section" VALUE="http://www.itknowledge.com/reference/standard/0471327476/">
</form>
<!-- Empty Reference Subhead -->
<!--ISBN=0471327476//-->
<!--TITLE=Essential Windows CE Application Programming//-->
<!--AUTHOR=Robert Burdick//-->
<!--PUBLISHER=John Wiley & Sons, Inc.//-->
<!--IMPRINT=Wiley Computer Publishing//-->
<!--CHAPTER=6//-->
<!--PAGES=168-171//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="165-168.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="171-174.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P><FONT SIZE="+1"><B>An Example</B></FONT></P>
<P>As an example, let’s take a look at how the File System Explorer application creates new files and directories. These features are triggered by the New Directory and New File menu options, so the first code to look at is the command handlers in the main window procedure for these two menu options (see Figure 6.2).
</P>
<P>The pertinent sections of the window procedure are shown below. Note that <I>tviCurSel</I> contains the currently selected tree view item TV_ITEM structure. The <I>lParam</I> member of this structure always contains the CEOID object identifier of the file or directory corresponding to the currently selected tree view item.</P>
<!-- CODE //-->
<PRE>
LRESULT CALLBACK WndProc(
HWND hwnd,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
CEOID oid;
CEOIDINFO oidInfo;
TCHAR *pszFileName, *pszDirectoryName;
switch (message)
{
case WM_COMMAND:
UINT nID;
nID = LOWORD(wParam);
switch(nID)
{
case IDC_NEWDIRECTORY:
//Create a new directory
oid = (CEOID)tviCurSel.lParam;
CeOidGetInfo(oid, &oidInfo);
if (OBJTYPE_DIRECTORY==oidInfo.wObjType)
{
pszFileName = NULL;
pszDirectoryName = TEXT("Empty Folder");
}
else
{
MessageBox(NULL, TEXT("Files cannot have children"),
TEXT("New Folder Error"),MB_OK|MB_ICONEXCLAMATION);
return (0);
}
OnNew(pszFileName, pszDirectoryName, tviCurSel.hItem,
oidInfo, TRUE);
break;
case IDC_NEWFILE:
/Create a new file
oid = (CEOID)tviCurSel.lParam;
CeOidGetInfo(oid, &oidInfo);
if (OBJTYPE_DIRECTORY==oidInfo.wObjType)
{
pszDirectoryName = oidInfo.infDirectory.szDirName;
pszFileName = TEXT("Empty File");
}
else
{
MessageBox(NULL, TEXT("Files cannot have children"),
TEXT("New Folder Error"),MB_OK|MB_ICONEXCLAMATION);
return (0);
}
OnNew(pszFileName, pszDirectoryName,
tviCurSel.hItem, oidInfo, FALSE);
break;
</PRE>
<!-- END CODE //-->
<P>In both the case of creating a new file and creating a new directory, the application first extracts the CEOIDINFO for the currently selected file or directory. A request to create a new file or directory will force the application to try and create the file or directory with the currently selected item as its parent.
</P>
<P>Obviously, this only makes sense if the currently selected object is a directory. Files cannot contain other files. Only directories can contain other files or directories. For this reason, both the IDC_NEWDIRECTORY and IDC_NEWFILE case statement code blocks check the <I>wObjType</I> member of the object information structure. In either of these cases, if the currently selected file system object is not a directory, a warning message is displayed and the operation is aborted.</P>
<P>If the user is trying to create a new file or directory under an existing directory, however, the appropriate default name is assigned to <I>pszFileName</I> or <I>pszDirectoryName</I>, and the application defined <I>OnNew</I> function is called. In the case of a request to create a new directory, the value “Empty Folder” is assigned to <I>pszDirectory</I>. In the case of a new file creation request, the name “Empty File” is assigned to <I>pszFileName</I>.</P>
<P>The <I>OnNew</I> function contains a lot of code for adding new items to the tree view control in response to new file and directory creations. This code is left out so that we can concentrate on the parts of the function that relate directly to the file system API. Also only the part of this function which creates new files is shown. Since the section that creates new directories is very similar, it was left out for the sake of brevity.</P>
<!-- CODE //-->
<PRE>
BOOL OnNew(TCHAR* pszFileName,
TCHAR* pszDirectoryName,
HTREEITEM hParent,
CEOIDINFO oidInfo,
BOOL bIsDirectory)
{
TCHAR pszFullName[MAX_PATH];
HANDLE hFile;
wsprintf(pszFullName, TEXT("%s\\%s"),
pszDirectoryName, pszFileName);
hFile = FindFirstFile(pszFullName, &fd);
if (INVALID_HANDLE_VALUE==hFile)
{
/File is new
FindClose(hFile);
hFile = CreateFile(pszFullName,
GENERIC_READ|GENERIC_WRITE, 0, NULL,
CREATE_NEW, FILE_ATTRIBUTE_ARCHIVE, NULL);
}
else
{
/File already exists
MessageBox(NULL,
TEXT("File \"Empty File\" Already Exists"),
TEXT("Create New File Error"),
MB_ICONEXCLAMATION|MB_OK);
return (FALSE);
}
return (TRUE);
}
</PRE>
<!-- END CODE //-->
<P>The arguments <I>pszFileName</I> and <I>pszDirectoryName</I> are the name of the file to be created and the parent directory name, respectively. <I>hParent</I> is the tree view item corresponding to the parent directory in the user interface. <I>oidInfo</I> is the CEOIDINFO structure containing information about the parent directory. <I>bIsDirectory</I> indicates whether a new file or a new directory is to be created by the function.</P>
<P>To create a directory or file, <I>CreateFile</I> must be passed the complete path name of the directory or file to be created. <I>OnNew,</I> therefore, first constructs the full path name in the variable <I>pszFullName</I>. To do this, <I>OnNew</I> only needs to concatenate the directory name contained in <I>pszDirectoryName</I> with the file name in <I>pszFileName.</I> This is the purpose of the <I>wsprintf</I> call at the beginning of the function. A “\” character is inserted between the parent directory name and the file name.</P>
<P>After the complete new file path name has been constructed, <I>OnNew</I> checks to see if the specified file already exists. It does so by calling <I>FindFirstFile</I>:</P>
<!-- CODE SNIP //-->
<PRE>
hFile = FindFirstFile(pszFullName, &fd);
</PRE>
<!-- END CODE SNIP //-->
<P><I>pszFullName</I> contains the full path name of the file to be created. If this file does not exist, <I>FindFirstFile</I> will return INVALID_HANDLE_ VALUE. Otherwise it returns the handle of the existing file.</P>
<P>If the file does not exist (i.e., if <I>hFile</I> equals INVALID_HANDLE_ VALUE,) <I>OnNew</I> closes the search handle <I>hFile</I> and creates the new file. If the file already exists, a message to this effect is displayed for the user and the function <I>OnNew</I> returns without creating a new file.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="165-168.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="171-174.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<!-- all of the reference materials (books) have the footer and subfoot reveresed -->
<!-- reference_subfoot = footer -->
<!-- reference_footer = subfoot -->
<!-- BEGIN SUB FOOTER -->
<br><br>
</TD>
</TR>
</TABLE>
<table width="640" border=0 cellpadding=0 cellspacing=0>
<tr>
<td align="left" width=135><img src="/images/white.gif" width=100 height="1" alt="" border="0"></td>
<!-- END SUB FOOTER -->
<!-- all of the books have the footer and subfoot reveresed -->
<!-- reference_subfoot = footer -->
<!-- reference_footer = subfoot -->
<!-- FOOTER -->
<td width="515" align="left" bgcolor="#FFFFFF">
<font face="arial, helvetica" size="1"><b><a href="/products.html"><font color="#006666">Products</font></a> | <a href="/contactus.html"><font color="#006666">Contact Us</font></a> | <a href="/aboutus.html"><font color="#006666">About Us</font></a> | <a href="http://www.earthweb.com/corporate/privacy.html" target="_blank"><font color="#006666">Privacy</font></a> | <a href="http://www.itmarketer.com/" target="_blank"><font color="#006666">Ad Info</font></a> | <a href="/"><font color="#006666">Home</font></a></b>
<br><br>
Use of this site is subject to certain <a href="/agreement.html">Terms & Conditions</a>, <a href="/copyright.html">Copyright © 1996-1999 EarthWeb Inc.</a><br>
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.</font><p>
</td>
</tr>
</table>
</BODY>
</HTML>
<!-- END FOOTER -->
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -