📄 ch24.htm
字号:
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>COleEditCtrl</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><TT>COleEditCtrl</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><TT>WM_CHAR</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><TT>OnChar</TT></TD>
</TR>
</TABLE>
</P>
<P>The source code for the <TT>COleEditCtrl::OnChar</TT> function is provided in
Listing 24.5.
<H4><FONT COLOR="#000077">TYPE: Listing 24.5. Handling the WM_CHAR message in COleEditCtrl::OnChar.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>void COleEditCtrl::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)</TT>
<TT>{</TT>
<TT> if(_istdigit(nChar) )</TT>
<TT> {</TT>
<TT> if( m_fNumbersAllowed == FALSE )</TT>
<TT> {</TT>
<TT> FireError( CTL_E_INVALIDPROPERTYVALUE,</TT>
<TT> _T("Numbers not allowed") );</TT>
<TT> }</TT>
<TT> else</TT>
<TT> {</TT>
<TT> COleControl::OnChar(nChar, nRepCnt, nFlags);</TT>
<TT> }</TT>
<TT> }</TT>
<TT> else if( _istalpha(nChar) )</TT>
<TT> {</TT>
<TT> if( m_fTextAllowed == FALSE )</TT>
<TT> {</TT>
<TT> FireError( CTL_E_INVALIDPROPERTYVALUE,</TT>
<TT> _T("Characters not allowed") );</TT>
<TT> }</TT>
<TT> else</TT>
<TT> {</TT>
<TT> COleControl::OnChar(nChar, nRepCnt, nFlags);</TT>
<TT> }</TT>
<TT> }</TT>
<TT> else</TT>
<TT> COleControl::OnChar (nChar, nRepCnt, nFlags);</TT>
<TT>}</TT>
</FONT></PRE>
<P>The <TT>OnChar</TT> handler tests for valid characters based on the property flags
<TT>m_fTextAllowed</TT> and <TT>m_fNumbersAllowed</TT>. Valid characters are passed
to <TT>COleControl::OnChar</TT>, the base class handler for <TT>WM_CHAR</TT>. If
an invalid character is detected, an <TT>Error</TT> event is fired to the control's
container.
<H3><FONT COLOR="#000077"><B>Modifying the Control's Bitmap</B></FONT></H3>
<P>When an ActiveX control is used in a tool such as Developer Studio, Visual Basic,
or the ActiveX control test container, a bitmap associated with the control is displayed
to the user. In Developer Studio, the bitmap is added to the control palette used
to design dialog box resources. In the test container, a toolbar button displaying
the bitmap is added to the container's toolbar.</P>
<P>Open the <TT>IDB_OLEEDIT</TT> bitmap resource and edit the bitmap image as shown
in Figure 24.5. Save the bitmap and compile the OleEdit project.</P>
<P><A NAME="05"></A><A HREF="05.htm"><B>Figure 24.5.</B> </A><BR>
<I>The <TT>IDB_OLEEDIT</TT> bitmap resource.</I>
<BLOCKQUOTE>
<P>
<HR>
<B> </B><FONT COLOR="#000077"><B>Time Saver:</B></FONT><B> </B>To ensure that the
text fits properly in the bitmap, use a regular (non-bold) 8-point Arial font.
<HR>
</BLOCKQUOTE>
<P>Build the OleEdit project. As part of the build process, the control will be registered
with the operating system. In the next section, you will learn how to test your control.
<H2><FONT COLOR="#000077"><B>Testing an ActiveX Control</B></FONT></H2>
<P>After following the steps in the previous sections, you are in possession of an
OleEdit ActiveX control. However, because the control is a component rather than
an executable program, it can't be run as an EXE. Testing an ActiveX control requires
a few extra steps, which are discussed in this section.
<H3><FONT COLOR="#000077"><B>Choosing a Test Container for Your Control</B></FONT></H3>
<P>Every ActiveX control requires a control container. The simplest control container
is the ActiveX control test container included with Developer Studio and the Win32
SDK. Other ActiveX control containers include Microsoft Access and Visual Basic 5.0.
In this section, you will test the OleEdit control with <TT>TSTCON32.EXE</TT>, the
test container included with Developer Studio.
<H3><FONT COLOR="#000077"><B>Using the TSTCON32 Test Container</B></FONT></H3>
<P>In order to launch the OleEdit control in the Developer Studio debugger, you must
specify the application to be used to load the control. You can do this by following
these steps:
<DL>
<DD>1. Select Settings from the Project menu in Developer Studio. The Project Setting
dialog box is displayed.<BR>
<BR>
2. Click the Debug tab.<BR>
<BR>
3. A small button with a right-arrow icon is located next to the Executable for Debug
Session edit control. Click this button and choose ActiveX Control Test Container
from the menu that is displayed.<BR>
<BR>
4. Click OK to dismiss the dialog box and save your changes.
</DL>
<P>After you have made these changes, you can use the Developer Studio debugger to
launch the test container. Click the Go icon in the toolbar or otherwise start a
debug session to display the test container, as shown in Figure 24.6.
<BLOCKQUOTE>
<P>
<HR>
<B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>You can also launch
the ActiveX control test container by selecting its menu item from the Tools menu.
This doesn't start your control inside the Visual C++ debugger.
<HR>
</BLOCKQUOTE>
<P><A NAME="06"></A><A HREF="06.htm"><B>Figure 24.6.</B></A> <I><BR>
The ActiveX control test container.</I></P>
<P>When an ActiveX control created by ControlWizard is compiled, the control is automatically
registered. To display a list of all registered controls, select Insert OLE Control...
from the Edit menu. A dialog box containing all available ActiveX controls is displayed.
Select the OleEdit edit control, and click OK. The OleEdit control is inserted into
the test container, as shown in Figure 24.7. Note that an OleEdit icon is also added
to the test container toolbar.</P>
<P><A NAME="07"></A><A HREF="07.htm"><B>Figure 24.7.</B> </A><I><BR>
The ActiveX control test container and OleEdit control.</I>
<H3><FONT COLOR="#000077"><B>Testing Properties</B></FONT></H3>
<P>You can use the test container to test your control's properties in two ways:
<UL>
<LI>Through an Automation interface that lists all exposed properties<BR>
<BR>
<LI>Through your control's property sheet
</UL>
<P>To access all the properties implemented by an ActiveX control, select Properties
from the View menu. A Properties dialog box is displayed, as shown in Figure 24.8.</P>
<P><A NAME="08"></A><A HREF="08.htm"><B>Figure 24.8.</B> </A><I><BR>
Accessing the properties exposed by OleEdit.</I></P>
<P>To display the list of properties exposed by the control, click the drop-down
list. Every property can be accessed and changed through the control's property sheet.
To change a particular property's value, select the property name from the drop-down
list, set the property value, and click Apply.</P>
<P>A slightly easier way to use the interface is provided through the control's property
sheet. You can use the test container to invoke the control's property sheet by selecting
Properties from the Edit menu. The property sheet for OleEdit is shown in Figure
24.9.</P>
<P><A NAME="09"></A><A HREF="09.htm"><B>Figure 24.9.</B> </A><BR>
<I>The property sheet used by OleEdit.</I>
<H2><FONT COLOR="#000077"><B>Summary</B></FONT></H2>
<P>In this hour you learned how to create and test ActiveX controls. ActiveX controls
are smaller and simpler versions of OLE custom controls. Developer Studio helps to
simplify the task of creating an ActiveX control. ControlWizard is very similar to
AppWizard and guides you through the steps required to create a skeleton version
of your control.
<H2><FONT COLOR="#000077"><B>Q&A</B></FONT></H2>
<DL>
<DD><B>Q If I test my ActiveX control in the test container, is it reasonable to
assume that it will work in all control containers?</B><BR>
<BR>
<B>A</B> You might think so, but in reality you should test your control in as many
containers as you can find. The test container is very useful for performing basic
tests on your control. You should also test your control in whatever environment
it will be used. For example, if your control will be used in Visual Basic programs,
you should test your control using VB as a container.<BR>
<BR>
<B>Q Why does ControlWizard offer the Invisible at Runtime option? What use is an
invisible control?</B><BR>
<BR>
<B>A</B> There actually are a large number of controls that don't have a need for
a user interface. For example, an ActiveX control that performs protocol conversion
for data communications might never be presented to the user; it can just perform
work for its container. By offering this option, the control is easier to develop
because the control doesn't need to handle user-interface issues. The control is
also easier to use because it will hide itself automatically.
</DL>
<H2><FONT COLOR="#000077"><B>Workshop</B></FONT></H2>
<P>The Workshop is designed to help you anticipate possible questions, review what
you've learned, and begin thinking ahead to putting your knowledge into practice.
The answers to the quiz are in Appendix B, "Quiz Answers."
<H3><FONT COLOR="#000077"><B>Quiz</B></FONT></H3>
<DL>
<DD>1. What is an ActiveX interface?<BR>
<BR>
2. What interface must be supported by an ActiveX control?<BR>
<BR>
3. What are some examples of ActiveX control containers?<BR>
<BR>
4. What four types of properties are supported by an ActiveX control?<BR>
<BR>
5. What are the two types of events generated by ActiveX controls?<BR>
<BR>
6. What macros are used to transfer data to and from property pages in an ActiveX
control?<BR>
<BR>
7. What MFC base classes provide support for ActiveX controls?<BR>
<BR>
8. What type of properties are supplied to the ActiveX control by its container?<BR>
<BR>
9. What is subclassing?<BR>
<BR>
10. What function is called by an ActiveX control to request that the subclassed
control repaint itself?
</DL>
<H3><FONT COLOR="#000077"><B>Exercises</B></FONT></H3>
<DL>
<DD>1. Change the OleEdit project so that hexadecimal numbers can be entered when
the Numbers Only flag is set.<BR>
<BR>
2. Change the OleEdit project so that only letters, numbers, and backspaces can be
entered into the control.
</DL>
<CENTER>
<P>
<HR>
<A HREF="../ch23/ch23.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"
ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../apa/apa.htm"><IMG
SRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"
BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"
HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A> <BR>
<BR>
<BR>
<IMG SRC="../button/corp.gif" WIDTH="284" HEIGHT="45" ALIGN="BOTTOM" ALT="Macmillan Computer Publishing USA"
BORDER="0"></P>
<P>© <A HREF="../copy.htm">Copyright</A>, Macmillan Computer Publishing. All
rights reserved.
</CENTER>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -