📄 ch19.htm
字号:
name will automatically be generated based on the external name you entered. Next,
specify the return type as BOOL so that you can let the container application know
whether you were able to load the drawing. Finally, add a single parameter to the
parameter list, giving it a name such as sFileName and specifying its type as LPCTSTR
(the CString type is not available, but the LPCTSTR type is compatible), as shown
in Figure 19.7. Click the OK button to add the method to your control. Once you add
the method, click the Edit Code button to edit the method, adding the code in Listing
19.11.</P>
<P><A HREF="javascript:popUp('19fig07.gif')"><B>FIGURE 19.7.</B></A><B> </B><I>The
Add custom Method dialog.</I></P>
<P>
<H4>LISTING 19.11. THE CSquiggleCtrl LoadDrawing FUNCTION.</H4>
<PRE> 1: BOOL CSquiggleCtrl::LoadDrawing(LPCTSTR sFileName)
2: {
3: // TODO: Add your dispatch handler code here
4: try
5: {
6: // Create a CFile object
7: CFile lFile(sFileName, CFile::modeRead);
8: // Create a CArchive object to load the file
9: CArchive lArchive(&lFile, CArchive::load);
10: // Load the file
11: m_maDrawing.Serialize(lArchive);
12: // Make sure that the loaded drawing won't be overwritten
13: m_bGenNewDrawing = FALSE;
14: // Draw the loaded drawing
15: Invalidate();
16: }
17: catch (CFileException err)
18: {
19: return FALSE;
20: }
21: return TRUE;
22: }</PRE>
<H3><BR>
<B>Adding Events</B></H3>
<P>The final part of building your control is adding the events that your control
will trigger in the container application. When using your control, the user will
be able to add code to be triggered on these events. Adding these events to your
control is done through the ActiveX Events tab of the Class Wizard. If you want to
add a stock event to be triggered by your control, then you just click the Add Event
button and select a stock event from the drop-down list of stock events. If you need
to add a custom event to your control, then in the Add Event dialog, instead of selecting
a stock event, you enter the name of your custom event. At the bottom of the Add
Event dialog is an area for adding parameters that you can pass from your control
to the container application with the event.</P>
<P>For the sample control, you'll add one event, a custom event to let the application
know that the drawing file specified has been loaded. To add this event, open the
Class Wizard and select the ActiveX Events tab, as shown in Figure 19.8. Click the
Add Event button to add the event. Enter the name for your custom event, FileLoaded.
You'll notice that the Add Event dialog automatically builds an internal name for
the event, in this case, FireFileLoaded, as shown in Figure 19.9. This internal name
is the name for the function that you need to call in your code when you want to
trigger this event. Click the OK button to add this event. To add a stock event,
select the desired stock event from the drop-down list of stock events, and click
the OK button to add this second event.</P>
<P>Now that you've added your event to your control, you need to make the necessary
changes to the code to trigger this event at the appropriate places. You'll trigger
your event at the end of your LoadDrawing function, assuming that you are able to
load the drawing correctly. Add this additional functionality to the LoadDrawing
function, as shown in line 17 of Listing 19.12.</P>
<P><A HREF="javascript:popUp('19fig08.gif')"><B>FIGURE 19.8.</B></A><B> </B><I>The
ActiveX Events tab of the Class Wizard.</I></P>
<P><A HREF="javascript:popUp('19fig09.gif')"><B>FIGURE 19.9.</B></A><B> </B><I>The
Add Event dialog.</I></P>
<P>
<H4>LISTING 19.12. THE MODIFIED CSquiggleCtrl LoadDrawing FUNCTION.</H4>
<PRE> 1: BOOL CSquiggleCtrl::LoadDrawing(LPCTSTR sFileName)
2: {
3: // TODO: Add your dispatch handler code here
4: try
5: {
6: // Create a CFile object
7: CFile lFile(sFileName, CFile::modeRead);
8: // Create a CArchive object to load the file
9: CArchive lArchive(&lFile, CArchive::load);
10: // Load the file
11: m_maDrawing.Serialize(lArchive);
12: // Make sure that the loaded drawing won't be overwritten
13: m_bGenNewDrawing = FALSE;
14: // Draw the loaded drawing
15: Invalidate();
16: // Fire the FileLoaded event
17: FireFileLoaded();
18: }
19: catch (CFileException err)
20: {
21: return FALSE;
22: }
23: return TRUE;
24: }</PRE>
<P>
<H3><A NAME="Heading12"></A>Testing the Control</H3>
<P>Now you are ready to compile and begin testing your control. Before you run to
the store to pick up a copy of Visual Basic, you already have a tool just for testing
ActiveX controls. On the Tools menu is one entry labeled ActiveX Control Test Container.
This is a utility that is designed specifically for testing ActiveX controls that
you have built. Once you compile your control, run the ActiveX Control Test Container
to test your control.</P>
<BLOCKQUOTE>
<P>
<HR>
<STRONG>TIP:</STRONG> If Visual C++ is unable to register your control, but is able
to compile it, you might need to register your control yourself. You can do this
by selecting Tools | Register Control from the menu. This will register the compiled
control in the Registry database.
<HR>
</BLOCKQUOTE>
<P>When you first start the test container, you see a blank area where your control
will appear. You need to insert your control into this container area by selecting
Edit | Insert New Control. This will open the Insert Control dialog, as shown in
Figure 19.10. Select your control from the list of available controls and click the
OK button to add your control to the container area, as shown in Figure 19.11.</P>
<P><A HREF="javascript:popUp('19fig10.gif')"><B>FIGURE 19.10.</B></A><B> </B><I>The
Insert Control dialog.</I></P>
<P>Now that you have your control loaded into the test container, you can play with
it, resize it, click it, and check when it generates a new drawing and when it just
redraws the existing drawing. If you trigger any events for your control, you'll
see the event that your control fired in the bottom pane of the test container so
that you can watch as each of the events you added to your control are triggered.</P>
<P><A HREF="javascript:popUp('19fig11.gif')"><B>FIGURE 19.11.</B></A><B> </B><I>The
squiggle control in the test container.</I></P>
<P>With your control selected, if you select Edit | Properties from the menu, you'll
open the property page that you designed for your control, allowing you to modify
the various properties of the control so that you can see whether they work correctly,
as shown in Figure 19.12.</P>
<P><A HREF="javascript:popUp('19fig12.gif')"><B>FIGURE 19.12.</B></A><B> </B><I>The
Squiggle Control Properties page.</I></P>
<P>Finally, to test the methods that you added to your control, select Control |
Invoke Methods. This opens the Invoke Methods dialog, as shown in Figure 19.13. In
here, you can select from the list of available methods in your control, entering
each of the parameters required for the methods, and then click the Invoke button
to call that method. You can watch as your methods are called and your control responds.</P>
<P><A HREF="javascript:popUp('19fig13.gif')"><B>FIGURE 19.13.</B></A><B> </B><I>The
Invoke Methods dialog.</I></P>
<P>
<H2><A NAME="Heading13"></A>Summary</H2>
<P>Today, you learned how you can use the tools and wizards in Visual C++ to build
ActiveX controls with little effort on your part. You learned how you can create
the shell of the control project using the Control Wizard. You also learned how you
can use the Class Wizard to add properties, methods, and events to your control.
You saw how you can design a property page for your control and how you can use the
Class Wizard to attach the controls on this dialog to the properties you defined
for your control without having to add any code. Finally, you learned how you can
use the ActiveX Control Test Container to test your control, triggering all the functionality
by using the tools of this utility.</P>
<P>
<H2><A NAME="Heading14"></A>Q&A</H2>
<DL>
<DT></DT>
<DD><B>Q How do I change the icon that appears in the toolbox for my control?</B>
<P>
<DT></DT>
<DD><B>A</B> In the Resource View tab of the workspace pane, open the Bitmap folder.
You should find a single bitmap in this folder. This image is displayed in the toolbox
for your control when you add it to a Visual C++ or Visual Basic project. You should
edit this bitmap so that it displays the image that you want to represent your control.
<P>
<DT></DT>
<DD><B>Q Why does my control have an About box?</B>
<P>
<DT></DT>
<DD><B>A</B> If you are building ActiveX controls that will be used by other developers,
whether you sell the control or give it away, you probably want to include some way
of indicating that you wrote the control, and that you, or your employer, owns the
copyright on the control. This acts as a legal identification on the control so that
whoever obtains your control cannot turn around and sell it as his creation.
<P>
</DL>
<H2><A NAME="Heading15"></A>Workshop</H2>
<P>The Workshop provides quiz questions to help you solidify your understanding of
the material covered and exercises to provide you with experience in using what you've
learned. The answers to the quiz questions and exercises are provided in Appendix
B, "Answers."</P>
<P>
<H3><A NAME="Heading16"></A>Quiz</H3>
<DL>
<DT></DT>
<DD><B>1. </B>What are the three aspects of a control that are visible to the container
application?
<P>
<DT></DT>
<DD><B>2. </B>Why do you need to design a property page for your control?
<P>
<DT></DT>
<DD><B>3. </B>What are the four types of properties that a control might have?
<P>
<DT></DT>
<DD><B>4. </B>What happens to the parameters that are passed to the methods of a
control?
<P>
<DT></DT>
<DD><B>5. </B>What tool can you use to test your controls?
<P>
</DL>
<H3><A NAME="Heading17"></A>Exercises</H3>
<DL>
<DT></DT>
<DD><B>1. </B>Add a method to your control to enable the container application to
trigger the generation of a new squiggle drawing.
<P>
<DT></DT>
<DD><B>2. </B>Add a method to your control to save a squiggle drawing. Use the CFile::modeWrite
and CArchive::store flags when creating the CFile and CArchive objects.
</DL>
<H1></H1>
<CENTER>
<P>
<HR>
<A HREF="../ch18/ch18.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"
ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch20/ch20.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>
</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 + -