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

📄 174-178.html

📁 WindowsCE.[Essential Windows CE Application Programming].Jon Wiley & Son.zip
💻 HTML
📖 第 1 页 / 共 2 页
字号:
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="">&nbsp;<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=174-178//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="171-174.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="178-182.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P><FONT SIZE="+1"><B>The <I>WriteFile</I> Function
</B></FONT></P>
<P>Writing data to a file in Windows CE is done with the <I>WriteFile</I> function. The syntax and use of this function is very similar to <I>ReadFile</I>:</P>
<!-- CODE SNIP //-->
<PRE>
  WriteFile(hFile, lpBuffer, nNumberOfBytesToWrite,
    lpNumberOfBytesWritten, lpOverlapped);
</PRE>
<!-- END CODE SNIP //-->
<P>The <I>WriteFile</I> parameters have the same meanings as the corresponding <I>ReadFile</I> parameters except for <I>nNumberOfBytesToWrite</I> and <I>lpNumberOfBytesWritten</I>. It isn&#146;t much of a stretch to realize that <I>nNumberOfBytesToWrite</I> specifies the number of bytes of data to write to the file indicated by <I>hFile</I>. Similarly, <I>WriteFile</I> returns the actual number of bytes written through the <I>lpNumberOfBytesWritten</I> parameter. As with <I>ReadFile</I>, the <I>lpOverlapped</I> parameter is ignored and should be set to NULL.</P>
<P>To write data to a file, the file must have been opened with GENERIC_WRITE access.</P>
<P>Data can be written to any position in a random access file much as data can be randomly read from a random access file. The file must be created or opened with the FILE_FLAG_RANDOM_ACCESS flag set. Then <I>SetFilePointer</I> can be used to specify the file location to which data is written.</P>
<P>The File System Explorer example application of this chapter demonstrates <I>ReadFile</I> and <I>WriteFile,</I> and <I>SetFilePointer</I> operations by means of its very rudimentary file editing feature.</P>
<P><FONT SIZE="+1"><B>An Example</B></FONT></P>
<P>To gain further insight into how to use the <I>ReadFile</I> and <I>WriteFile</I> functions, let&#146;s look at how the File System Explorer application implements its rudimentary file editing capabilities. See Figure 6.7 for a look at the basic file editor.</P>
<P>The two user operations which invoke the editor are selecting the Edit File menu option and pressing the enter key after selecting a file. Both of these operations cause the following application-defined <I>OnEdit</I> function to be called:</P>
<!-- CODE //-->
<PRE>
  void OnEdit(HWND hwnd, OIDINFO oidInfo)
  &#123;
    if (OBJTYPE_FILE==oidInfo.wObjType)
    &#123;
     DialogBox(ghInst, MAKEINTRESOURCE(IDD_EDITFILE),
      hwnd, (DLGPROC)EditDlgProc);
    &#125;
    else
    &#123;
     MessageBox(NULL, TEXT("You May Only Edit Files"),
      TEXT("Directories May Not Be Edited"),
       MB_OK|MB_ICONEXCLAMATION);
    &#125;
  &#125;
</PRE>
<!-- END CODE //-->
<P>The parameter <I>hwnd</I> is the parent of the dialog box which acts as the file editor. <I>oidInfo</I> is the CEOIDINFO structure containing information about the currently selected file or directory.</P>
<P><I>OnEdit</I> checks the type of the currently selected object and displays an error message if the object is a directory. It doesn&#146;t make sense to edit a directory.</P>
<P>On the other hand, if the object is a file, the editor is invoked by the <I>DialogBox</I> call. All of the rudimentary file editing functionality is coded in this dialog procedure <I>EditDlgProc</I>. The dialog procedure looks like this:</P>
<!-- CODE //-->
<PRE>
  BOOL CALLBACK EditDlgProc(
    HWND hwndDlg,
    UINT message,
    WPARAM wParam,
    LPARAM lParam)
  &#123;
    TCHAR pszText[MAX_FILE_LENGTH];
    HWND hwndEdit;
    DWORD dwBytes;
    CEOID oid;
    CEOIDINFO oidInfo;
    switch(message)
    &#123;
    case WM_INITDIALOG:
     oid = (CEOID)tviCurSel.lParam;
     CeOidGetInfo(oid, &#38;oidInfo);
     pszText[0] = 0;
     hFile = CreateFile(oidInfo.infFile.szFileName,
      GENERIC_READ|GENERIC_WRITE, 0,
       NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
        NULL);
     if (INVALID_HANDLE_VALUE!=hFile)
     &#123;
      dwBytes = 0;
      ReadFile(hFile, pszText,
       oidInfo.infFile.dwLength,
        &#38;dwBytes, NULL);
      hwndEdit = GetDlgItem(hwndDlg, IDC_FILETEXT);
      SetWindowText(hwndEdit, pszText);
      wsprintf(pszText, TEXT("Edit File %s"),
       oidInfo.infFile.szFileName);
      SetWindowText(hwndDlg, pszText);
     &#125;
     return (TRUE);
    case WM_COMMAND:
     UINT nID;
     nID = LOWORD(wParam);
     switch(nID)
     &#123;
     case IDOK:
      //Save text to file
      DWORD nBytesToWrite;
      hwndEdit = GetDlgItem(hwndDlg, IDC_FILETEXT);
      GetWindowText(hwndEdit, pszText,
       MAX_FILE_LENGTH);
      SetFilePointer(hFile, 0, 0, FILE_BEGIN);
      nBytesToWrite = lstrlen(pszText)*sizeof(TCHAR);
      WriteFile(hFile, pszText, nBytesToWrite,
       &#38;dwBytes, NULL);
      //Deliberate fall-through
     case IDCANCEL:
      CloseHandle(hFile);
      EndDialog(hwndDlg, nID);
      break;
     default:
      break;
     &#125;       //End of switch(nID) statement
     return (FALSE);
    default:
     return (FALSE);
    &#125;        //End of switch(message) statement
  &#125;
</PRE>
<!-- END CODE //-->
<P>When the dialog box is opened, the WM_INITDIALOG message handler is executed. This code gets the CEOIDINFO about the currently selected file via the global variable <I>tviCurSel</I>. As we mentioned above, this always contains the TV_ITEM of the currently selected file or directory in the tree view user interface. The file name is extracted from this CEOIDINFO data, and the application attempts to open the specified file with a <I>CreateFile</I> call.</P>
<P>If the file exists, <I>CreateFile</I> returns the handle of the file. This handle is stored in the global variable <I>hFile</I> so that the rest of the dialog procedure has access to it. The contents of the file are read with the <I>ReadFile</I> call:</P>
<!-- CODE SNIP //-->
<PRE>
  ReadFile(hFile, pszText, oidInfo.infFile.dwLength,
    &#38;dwBytes, NULL);
</PRE>
<!-- END CODE SNIP //-->
<P>The number of bytes that <I>ReadFile</I> attempts to read is equal to the length of the file. This is specified by <I>oidInfo.infFile.dwLength</I>. The file data is read into the string <I>pszText</I>.</P>
<P>Next, the contents of the file are placed in the dialog box edit control by the first <I>SetWindowText</I> call:</P>
<!-- CODE SNIP //-->
<PRE>
  SetWindowText(hwndEdit, pszText);
</PRE>
<!-- END CODE SNIP //-->
<P>Finally, the dialog box caption is changed to show the name of the file being edited.
</P>
<P>The FILESYS.EXE user is now free to edit the file by typing text into the edit control.</P>
<P>To save the new text into the file, the user presses the Save button. This action invokes the IDOK command handler in the <I>EditDlgProc</I> dialog procedure. This handler code does exactly the opposite of the WM_INITDIALOG code. The contents of the editor are copied into <I>pszText</I> by the <I>GetWindowText</I> call. Next, the file pointer is set back to the beginning of the file, and the text is written to the file via <I>WriteFile</I>.</P>
<P>Execution then falls through to the IDCANCEL handler, which closes the file and the dialog box. This is the same code that is executed when the user presses the Cancel button.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="171-174.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="178-182.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>&nbsp;|&nbsp; <a href="/contactus.html"><font color="#006666">Contact Us</font></a>&nbsp;|&nbsp; <a href="/aboutus.html"><font color="#006666">About Us</font></a>&nbsp;|&nbsp; <a href="http://www.earthweb.com/corporate/privacy.html" target="_blank"><font color="#006666">Privacy</font></a> &nbsp;|&nbsp; <a href="http://www.itmarketer.com/" target="_blank"><font color="#006666">Ad Info</font></a> &nbsp;|&nbsp; <a href="/"><font color="#006666">Home</font></a></b>
		<br><br>
		
		Use of this site is subject to certain <a href="/agreement.html">Terms &amp; Conditions</a>, <a href="/copyright.html">Copyright &copy; 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 + -