📄 322-325.html
字号:
<FONT FACE="Arial,Helvetica" SIZE="-1">
To access the contents, click the chapter and section titles.
</FONT>
<P>
<B>Essential Windows CE Application Programming</B>
<FONT SIZE="-1">
<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=12//-->
<!--PAGES=322-325//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="320-322.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="325-328.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>On the other hand, an application that uses an HTML viewer control is responsible for responding to the fact that a user tapped on a hyperlink. If the link points to another file, the application must load and display it.
</P>
<P>The HTML viewer control sends notifications to its parent window when events such as a hyperlink tap occur. These notifications are sent in the form of WM_NOTIFY messages. This is exactly the same notification mechanism used by the Windows CE common controls.</P>
<P>The notification that is sent when a hyperlink is tapped is NM_HOTSPOT. This notification is also sent in response to the user submitting a form, a subject we will not be covering here. The <I>lParam</I> sent with this notification is a pointer to an NM_HTMLVIEW structure, In fact, many of the notifications sent by the HTML viewer control pass such a structure.</P>
<P>NM_HTMLVIEW is defined as:</P>
<!-- CODE SNIP //-->
<PRE>
typedef struct tagNM_HTMLVIEW
{
NMHDR hdr;
LPSTR szTarget;
LPSTR szData;
DWORD dwCookie;
} NM_HTMLVIEW;
</PRE>
<!-- END CODE SNIP //-->
<P>The first member, as with any control notification, is an NMHDR structure.
</P>
<P><I>szTarget</I> contains a NULL-terminated string whose meaning depends on the notification being sent. For example, in the case of the NM_HOTSPOT notification, <I>szTarget</I> contains the string that follows the HREF field in the line of HTML text defining the link.</P>
<P>For example, consider again the situation illustrated in Figure 12.1 and the sample HTML file that produced that display. If a user taps the “Windows CE” link in that case, the HTML viewer control that contains the text sends an NM_HOTSPOT notification. The <I>szTarget</I> member of the NM_HTMLVIEW structure that is sent with this notification will contains the string “\Windows\wince.htm.”</P>
<P><I>szData</I> and <I>dwCookie</I> contain data specific to the particular notification being sent. For example, in the case of an NM_HOTSPOT, <I>szData</I> contains the query data sent with a form POST submission. The <I>dwCookie</I> value is not used by NM_HOTSPOT.</P>
<P><FONT SIZE="+1"><B>An Example: How HTML.EXE Follows Links</B></FONT></P>
<P>Now let’s see how to respond to the NM_HOTSPOT notification in a real example.
</P>
<P>If you run the HTML.EXE sample application, you will see that it properly follows hyperlinks. As long as the links refer to HTML files that are in the emulator file system or the file system of the Windows CE device running the application, everything works fine.</P>
<P>This behavior is implemented by responding to the NM_HOTSPOT notification described above. The relevant part of main application window’s window procedure is given below:</P>
<!-- CODE //-->
<PRE>
LRESULT CALLBACK WndProc(
HWND hwnd,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
UINT nID;
int nLen;
TCHAR* pszFilename;
switch(message)
{
case WM_NOTIFY:
NM_HTMLVIEW* lpnm;
lpnm = (NM_HTMLVIEW*)lParam;
switch(lpnm->hdr.code)
{
case NM_HOTSPOT:
nLen = strlen(lpnm->szTarget);
pszFilename = (TCHAR*)LocalAlloc(LPTR,
sizeof(TCHAR)*nLen);
AnsiToWide(lpnm->szTarget, pszFilename);
LinkToFile(pszFilename);
LocalFree(pszFilename);
break;
default:
break;
} //End of switch(lpnm->hdr.code) block
return (0);
/* Other message handler code */
...
default:
return (DefWindowProc(hwnd, message, wParam, lParam));
} //End of switch(message) block
}
</PRE>
<!-- END CODE //-->
<P>The NM_HOTSPOT notification handler first extracts the string containing the HREF hyperlink text from the <I>szTarget</I> member of the NM_HTMLVIEW structure. HTML.EXE assumes that this contains the file name of an HTML document. <I>LocalAlloc</I> is called to allocate enough space in the buffer <I>pszFilename</I> to hold the file name.</P>
<P>Then all that is left to do is open, read, and display the contents of the file. This is exactly what the <I>LinkToFile</I> function does, so the NM_HOTSPOT notification handler simply needs to call this function. The HTML viewer control responds by displaying the new HTML file.</P>
<P>An important step of the process was skipped, however. The purpose of these two lines of code is not obvious:</P>
<!-- CODE SNIP //-->
<PRE>
pszFilename = (TCHAR*)LocalAlloc(LPTR,
sizeof(TCHAR)*nLen);
AnsiToWide(lpnm->szTarget, pszFilename);
</PRE>
<!-- END CODE SNIP //-->
<P>The HTML files read by HTML.EXE are assumed to be ANSI text. But the file system API functions called by <I>LinkToFile</I> require Unicode file names. Therefore the ANSI string contained by <I>lpnm</I>-><I>szTarget</I> (the link file name) must be converted to Unicode.</P>
<P>Each Unicode character requires two bytes (the size of a TCHAR), and there are <I>nLen</I> characters in the file name string. So <I>LocalAlloc</I> allocates enough bytes to accommodate the Unicode representation of the string <I>lpnm</I>-><I>szTaget</I>.</P>
<P><I>AnsiToWide</I> is an application-defined function that converts ANSI strings to Unicode:</P>
<!-- CODE SNIP //-->
<PRE>
void AnsiToWide(LPSTR lpStr, TCHAR* lpTChar)
{
while(*lpTChar++ = *lpStr++);
}
</PRE>
<!-- END CODE SNIP //-->
<P>This is a variation of the classic one-line string copy function.<SUP><SMALL><B>1</B></SMALL></SUP> As long as the two string arguments are NULL-terminated, this function copies the ANSI string in <I>lpStr</I> to the Unicode string <I>lpTChar</I>. Since the ++ operator increments a pointer variable by the size of the type it points to, incrementing <I>lpTChar</I> leaves two bytes per character.</P>
<BLOCKQUOTE>
<HR>
<SUP><SMALL><B>1</B></SMALL></SUP><FONT SIZE="-1">For a more complete explanation of how this function works, see B. Stroustrup, <I>The C++ Programming Language</I>, 2nd Ed. (Addison-Wesley, 1991), pp. 92-93.
</FONT>
<HR>
</BLOCKQUOTE>
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="320-322.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="325-328.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 + -