📄 ch19.htm
字号:
whether you were able to load the drawing. Finally, add a single parameter to theparameter 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 shownin Figure 19.7. Click the OK button to add the method to your control. Once you addthe method, click the Edit Code button to edit the method, adding the code in Listing19.11.</P><P><A HREF="javascript:popUp('19fig07.gif')"><B>FIGURE 19.7.</B></A><B> </B><I>TheAdd 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 file11: m_maDrawing.Serialize(lArchive);12: // Make sure that the loaded drawing won't be overwritten13: m_bGenNewDrawing = FALSE;14: // Draw the loaded drawing15: 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 controlwill trigger in the container application. When using your control, the user willbe able to add code to be triggered on these events. Adding these events to yourcontrol is done through the ActiveX Events tab of the Class Wizard. If you want toadd a stock event to be triggered by your control, then you just click the Add Eventbutton and select a stock event from the drop-down list of stock events. If you needto add a custom event to your control, then in the Add Event dialog, instead of selectinga stock event, you enter the name of your custom event. At the bottom of the AddEvent dialog is an area for adding parameters that you can pass from your controlto the container application with the event.</P><P>For the sample control, you'll add one event, a custom event to let the applicationknow that the drawing file specified has been loaded. To add this event, open theClass Wizard and select the ActiveX Events tab, as shown in Figure 19.8. Click theAdd 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 forthe event, in this case, FireFileLoaded, as shown in Figure 19.9. This internal nameis the name for the function that you need to call in your code when you want totrigger 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 clickthe OK button to add this second event.</P><P>Now that you've added your event to your control, you need to make the necessarychanges to the code to trigger this event at the appropriate places. You'll triggeryour event at the end of your LoadDrawing function, assuming that you are able toload the drawing correctly. Add this additional functionality to the LoadDrawingfunction, 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>TheActiveX Events tab of the Class Wizard.</I></P><P><A HREF="javascript:popUp('19fig09.gif')"><B>FIGURE 19.9.</B></A><B> </B><I>TheAdd 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 file11: m_maDrawing.Serialize(lArchive);12: // Make sure that the loaded drawing won't be overwritten13: m_bGenNewDrawing = FALSE;14: // Draw the loaded drawing15: Invalidate();16: // Fire the FileLoaded event17: 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 tothe store to pick up a copy of Visual Basic, you already have a tool just for testingActiveX 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 thatyou have built. Once you compile your control, run the ActiveX Control Test Containerto 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 controlwill appear. You need to insert your control into this container area by selectingEdit | Insert New Control. This will open the Insert Control dialog, as shown inFigure 19.10. Select your control from the list of available controls and click theOK 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>TheInsert Control dialog.</I></P><P>Now that you have your control loaded into the test container, you can play withit, resize it, click it, and check when it generates a new drawing and when it justredraws the existing drawing. If you trigger any events for your control, you'llsee the event that your control fired in the bottom pane of the test container sothat 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>Thesquiggle control in the test container.</I></P><P>With your control selected, if you select Edit | Properties from the menu, you'llopen the property page that you designed for your control, allowing you to modifythe 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>TheSquiggle 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. Inhere, you can select from the list of available methods in your control, enteringeach of the parameters required for the methods, and then click the Invoke buttonto 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>TheInvoke 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 buildActiveX controls with little effort on your part. You learned how you can createthe shell of the control project using the Control Wizard. You also learned how youcan 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 theClass Wizard to attach the controls on this dialog to the properties you definedfor your control without having to add any code. Finally, you learned how you canuse the ActiveX Control Test Container to test your control, triggering all the functionalityby 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 ofthe material covered and exercises to provide you with experience in using what you'velearned. The answers to the quiz questions and exercises are provided in AppendixB, "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"><IMGSRC="../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. Allrights reserved.</CENTER></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -