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

📄 305-310.html

📁 WindowsCE.[Essential Windows CE Application Programming].Jon Wiley & Son.zip
💻 HTML
📖 第 1 页 / 共 2 页
字号:

<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=11//-->
<!--PAGES=305-310//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="303-305.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="310-312.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P><FONT SIZE="+1"><B>Using Extra Window Words to Maintain the State of the Control</B></FONT></P>
<P>So far we have ignored the significance of the following line of code that is executed when initializing the custom button control&#146;s window class:
</P>
<!-- CODE SNIP //-->
<PRE>
  wndClass.cbWndExtra = 8;
</PRE>
<!-- END CODE SNIP //-->
<P>As discussed back in Chapter 2, this statement means that every instance of the window class will contain eight extra bytes, or two 32-bit values. An application can use these values to store information about an instance of the window class.
</P>
<P>These extra words are accessed using <I>GetWindowLong</I> and <I>SetWindowLong</I>. Instead of using one of the predefined indices in Table 11.4, the application references a particular 32-bit value by its zero-based offset. Since the number of bytes defined by <I>cbWndExtra</I> must be a multiple of four, and each extra word is 4 bytes, the index of the first word is 0, the second is 4, and so on.</P>
<P>For example, to access the second of these extra window words, an application would do this:</P>
<!-- CODE SNIP //-->
<PRE>
  DWORD dwVal;
  dwVal = GetWindowLong(hwndControl, 4);
</PRE>
<!-- END CODE SNIP //-->
<P>The custom button control implementation uses the two extra words it defines to keep information about the state of each instance of the control class. CONTROL.H defines the following two indices:
</P>
<!-- CODE SNIP //-->
<PRE>
  #define GWL_BUTTONINVERT   0
  #define GWL_BUTTONFONT    4
</PRE>
<!-- END CODE SNIP //-->
<P>These are the indices used to access the first and second extra window words, respectively, defined by the custom button control&#146;s window class.
</P>
<P>The first extra window word keeps track of whether the particular button is currently painted in the inverted state. This information is used by the control&#146;s window procedure, for example, to determine which state to repaint a toggle-style control in when a user taps it.</P>
<P>The second word stores the font handle of the font used to draw the button text.</P>
<P>As an example of how this state information is used, let&#146;s look at the processing that occurs when a custom button control is created. The following code comes from the control window procedure&#146;s WM_CREATE handler. <I>hwnd</I> is the HWND of the control, and <I>lpcs</I> is a pointer to the CREATESTRUCT. <I>hFont12Pt</I> and <I>hFont18Pt</I> are the handles to the two fonts available to the control:</P>
<!-- CODE //-->
<PRE>
  if (lpcs-&gt;style &#38; CBTN_LARGEFONT)
  &#123;
   SetWindowLong(
    hwnd,
    GWL_BUTTONFONT,
    (LONG)hFont18Pt
    );
  &#125;
  else
  &#123;
    SetWindowLong(
     hwnd,
     GWL_BUTTONFONT,
     (LONG)hFont12Pt
     );
  &#125;
  return (TRUE);
</PRE>
<!-- END CODE //-->
<P>When an instance of the control is created, the appropriate font is assigned depending on whether the CBTN_LARGEFONT style bit is set.
</P>
<P>The control then uses this font when painting itself. The WM_PAINT handler code contains the following line for selecting the font into the control&#146;s device context. <I>hwnd</I> is the window handle of the control:</P>
<!-- CODE SNIP //-->
<PRE>
  hFontOld = (HFONT)SelectObject(hdc,
      (HFONT)GetWindowLong(hwnd, GWL_BUTTONFONT));
</PRE>
<!-- END CODE SNIP //-->
<P>Any text drawn inside the control is thus in the correct font.
</P>
<P><FONT SIZE="+1"><B>Handling Button Presses</B></FONT></P>
<P>Probably the most interesting code in the custom button control implementation is the stylus handling logic. The custom button control class supports the CBTN_PUSH and CBTN_TOGGLE styles, and the stylus code must implement the appropriate behavior for both.
</P>
<P>To make it easier for the control source code to determine which of these styles is assigned to a particular button, the CONTROL.H header file defines the following macro:</P>
<!-- CODE SNIP //-->
<PRE>
  #define IsToggleStyle(hwnd) \
    ((GetWindowLong(hwnd, GWL_STYLE) &#38; CBTN_TOGGLE) \
    ? TRUE : FALSE)
</PRE>
<!-- END CODE SNIP //-->
<P>The control also uses the first extra window word, indexed by GWL_BUTTONINVERT, to keep track of whether a button instance is in the pressed or unpressed state.
</P>
<P><FONT SIZE="+1"><B><I>CBTN_PUSH Style Custom Button Controls</I></B></FONT></P>
<P>Buttons with the CBTN_PUSH style act like regular Windows CE button controls. When pressed, they are painted in the pressed state. Once released, they are repainted in the unpressed state. Also, if a user presses the button and drags the stylus on and off the button without releasing the button, the button changes between the pressed and unpressed states.
</P>
<P>When a user presses a CBTN_PUSH-style button, the following WM_LBUTTONDOWN handler code is invoked:</P>
<!-- CODE SNIP //-->
<PRE>
  SetCapture(hwnd);
  SetWindowLong(hwnd, GWL_BUTTONINVERT, (LONG)TRUE);
</PRE>
<!-- END CODE SNIP //-->
<P>The stylus capture is assigned to the button control window. This forces all subsequent stylus input to be passed to the control. Next, the GWL_BUTTONINVERT extra window word is set to TRUE. This state information will be used during WM_PAINT handling to determine which state to paint the button in. Specifically, the WM_PAINT handler includes the following code. <I>hwnd</I> is the HWND of the control, and <I>hdc</I> is the control&#146;s device context:</P>
<!-- CODE //-->
<PRE>
  RECT rc;
  BOOL bInvert;
  GetClientRect(hwnd, &#38;rc);
  bInvert = (BOOL)GetWindowLong(hwnd, GWL_BUTTONINVERT);
  if (bInvert)
  &#123;
    DrawEdge(hdc, &#38;rc, EDGE_SUNKEN,
     BF_SOFT|BF_RECT);
  &#125;
  else
  &#123;
    DrawEdge(hdc, &#38;rc, EDGE_RAISED,
     BF_SOFT|BF_RECT);
  &#125;
</PRE>
<!-- END CODE //-->
<P>In the pressed state the control is drawn with a sunken edge. In the unpressed state, it is drawn in the raised state.
</P>
<P>When the user releases the button, the WM_LBUTTONUP code shown in Figure 11.3 is invoked.</P>
<P><A NAME="Fig3"></A><A HREF="javascript:displayWindow('images/11-03.jpg',500,470 )"><IMG SRC="images/11-03t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/11-03.jpg',500,470)"><FONT COLOR="#000077"><B>Figure 11.3</B></FONT></A>&nbsp;&nbsp;Custom button control WM_LBUTTONUP message handling code.</P>
<P>Capture is released from the control HWND. The GWL_BUTTONINVERT window word is set to FALSE so that the button is drawn in the unpressed state during the next paint cycle.
</P>
<P>The rest of the WM_LBUTTONUP code determines whether the stylus was in the button when it was released. If so, the button sends a WM_COMMAND message to its parent window. WM_COMMAND messages are not sent if a user presses a button and then drags the stylus off of it without releasing the button.</P>
<P>The <I>PtInRect</I> call determines if the stylus point is in the control&#146;s bounding rectangle. Also note the use of the GWL_ID index in the call to <I>GetWindowLong</I>. This extracts the control identifier of the custom button, which must be sent with the WM_COMMAND message.</P>
<P>Finally, the control must handle stylus move messages. The WM_MOUSEMOVE handler implementation is:</P>
<!-- CODE //-->
<PRE>
 RECT rc;
 BOOL bInvert;if (GetCapture()==hwnd)
 &#123;
   GetClientRect(hwnd, &#38;rc);
   bInvert = (PtInRect(&#38;rc, pt)) ? TRUE : FALSE;
   if (!IsToggleStyle(hwnd))
   &#123;
    SetWindowLong(hwnd, GWL_BUTTONINVERT, (LONG)bInvert);
   &#125;
</PRE>
<!-- END CODE //-->
<P>If the control does not have stylus capture, the WM_MOUSEMOVE handler does nothing. This means that if the stylus moves when a button is not pressed, nothing needs to be done.
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="303-305.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="310-312.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 + -