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

📄 ch16.htm

📁 VC使用大全。里面集合了VC使用的各种使用技巧。非常有用。
💻 HTM
📖 第 1 页 / 共 4 页
字号:

<pre><font color="#008000">{</font></pre>

<pre><font color="#008000">     m_horizcenter = bNewValue;</font></pre>

<pre><font color="#008000">     RefreshWindow();</font></pre>

<pre><font color="#008000">}</font></pre>

<P>And <font color="#008000">OnColorChanged()</font> looks like this:</P>

<pre><font color="#008000">void CShowStringDoc::OnColorChanged() </font></pre>

<pre><font color="#008000">{</font></pre>

<pre><font color="#008000">     RefreshWindow();</font></pre>

<pre><font color="#008000">}</font></pre>

<P>Add the same <font color="#008000">RefreshWindow()</font> call to <font color="#008000">SetVertCenter()</font> and <font color="#008000">OnStringChanged()</font>. Now you are ready to build and test. Build the project and correct any typing errors. Run 
ShowString as a stand-alone application, both to register it and to test your drawing code. You cannot change the string, color, or centering as you could with older versions of ShowString, because this version does not implement the <U>T</U>ools, 
<U>O</U>ptions menu item and its dialog box. The controller application is going to do that for this version.</P>

<H3><B>Visual Basic</B></H3>

<P>This chapter has mentioned a controller application several times, and you may have wondered where it's going to come from. You are going to put it together in Visual Basic. Figure 16.7 shows the Visual Basic interface.</P>

<A HREF="Rfigs07.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch16/Rfigs07.gif"><b>Fig. 16.7</b></A>

<P><I>Visual Basic makes Automation controller applications very quickly.</I></P>

<blockquote><p><img src="tip.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/tip.gif">

<P>If you don&#146;t have Visual Basic, but you do have an earlier release of Visual C++, you can use DispTest, a watered-down version of Visual Basic that once came with Visual C++. It was never added to the Start menu, but you can run DISPTEST.EXE from 
the C:\MSDEV\BIN folder or from your old Visual C++ CD-ROM's \MSDEV\BIN folder. If you've written VBA macros in Excel and have a copy of Excel, you can use that, too. For testing OLE Automation servers, it doesn't matter which you choose.</P>

<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>

<P>To build a controller application for the ShowString Automation server, start by running Visual Basic. In the window at the upper-right labeled Project1, click the View Code button. Choose Form from the left-hand drop-down list box in the new window 
that appears, and the <font color="#008000">Form_Load()</font> subroutine is displayed. Enter the code in Listing 16.18 into that subroutine.</P>

<P><I>Listing 16.18&#151;Form1.frm&#151; Visual Basic code</I></P>

<pre><font color="#008000">Private Sub Form_Load ()</font></pre>

<pre><font color="#008000">   Set ShowTest = CreateObject(&quot;ShowString.Document&quot;)</font></pre>

<pre><font color="#008000">   ShowTest.ShowWindow</font></pre>

<pre><font color="#008000">   ShowTest.HorizCenter = False</font></pre>

<pre><font color="#008000">   ShowTest.Color = 1</font></pre>

<pre><font color="#008000">   ShowTest.String = &quot;Hello from VB&quot;</font></pre>

<pre><font color="#008000">   Set ShowTest = Nothing</font></pre>

<pre><font color="#008000">End Sub</font></pre>

<P>Choose (General) from the left-hand drop-down list box and then enter this line of code:</P>

<pre><font color="#008000">Dim ShowTest As Object</font></pre>

<P>For those of you who don't read Visual Basic, this code will be easier to understand if you execute it one line at a time. Choose <U>R</U>un, Step <U>I</U>nto to execute the first line of code. Then repeatedly press F8 to move through the routine. 
(Wait after each press until the cursor is back to normal.) The line in the general code sets up an object called <font color="#008000">ShowTest</font>. When the form is loaded (which is whenever you run this little program), an instance of the ShowString 
object is created. The next line calls the <font color="#008000">ShowWindow</font> method to display the main window on the screen. Whenever the debugger pauses, the dashed box is around the line of code that will run next. You should see something like 
Figure 16.8 with the default ShowString behavior.</P>

<A HREF="Rfigs08.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch16/Rfigs08.gif"><b>Fig. 16.8</b></A>

<P><I>The </I><I>ShowWindow</I><I> method displays the main ShowString window.</I></P>

<P>Press F8 again to run the line that turns off horizontal centering. Notice that you do not call the function <font color="#008000">SetHorizCenter</font>. You exposed <font color="#008000">HorizCenter</font> as a property of the OLE Automation server, 
and from Visual Basic, you access it as a property. The difference is that the C++ framework code calls <font color="#008000">SetHorizCenter</font> to make the change, rather than just making the change and then calling a notification function to tell you 
that it was changed. After this line has executed, your screen will resemble Figure 16.9, because the <font color="#008000">SetHorizCenter</font> method calls <font color="#008000">RefreshWindow()</font> to immediately redraw the screen.</P>

<A HREF="Rfigs09.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch16/Rfigs09.gif"><b>Fig. 16.9</b></A>

<P><I>The Visual Basic program has turned off centering.</I></P>

<P>As you continue through this program, pressing F8 to move a step at a time, the string will turn red and then change to <font color="#008000">Hello from VB</font>. Notice that the change to these directly exposed properties looks no different than the 
change to the Get/Set method property, <font color="#008000">HorizCenter</font>. When the program finishes, the window goes away. You have successfully controlled your Automation server from Visual Basic.</P>

<H3><B>Type Libraries and ActiveX Internals</B></H3>

<P>Many programmers are intimidated by ActiveX, and the last thing they want is to know what's happening under the hood. There's nothing wrong with that attitude at all. It's quite object-oriented, really, to trust the already-written ActiveX framework to 
handle the black magic of translating <font color="#008000">ShowTest.HorizCenter = False</font> into a call to <font color="#008000">CShowStringDoc::SetHorizCenter()</font>. But if you want to know how that &quot;magic&quot; happens, or what to do if it 
doesn't, you need to add one more piece to the puzzle. You have already seen the dispatch map for ShowString, but you haven't seen the <I>type library</I>. It is not meant for humans to read, but it's for ActiveX and the Registry. It is generated for you 
as part of a normal build from your Object Definition Language (ODL) file. This file was generated by AppWizard and is maintained by ClassWizard.</P>

<P>Perhaps you&#146;ve noticed, as you built this application, a new entry in the ClassView pane. Figure 16.10 shows this entry expanded: it contains all the properties and methods exposed in the IShowString interface of your Automation Server. If you 
right-click IShowString in this list, you can use the shortcut menu to add methods or properties. If you double-click any of the properties or methods, the old file is opened for you to view. Listing 16.19 shows ShowString.odl.</P>

<A HREF="Rfigs10.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch16/Rfigs10.gif"><b>Fig. 16.10</b></A>

<P><I>Automation servers have an entry in the ClassView for each of their </I><I>interfaces</I></P>

<P><I>Listing 16.19&#151;ShowString.odl&#151;ShowString type library</I></P>

<pre><font color="#008000">// ShowString.odl : type library source for ShowString.exe</font></pre>

<pre><font color="#008000">// This file will be processed by the MIDL compiler to produce the</font></pre>

<pre><font color="#008000">// type library (ShowString.tlb).</font></pre>

<pre><font color="#008000">[ uuid(61C76C06-70EA-11D0-9AFF-0080C81A397C), version(1.0) ]</font></pre>

<pre><font color="#008000">library ShowString</font></pre>

<pre><font color="#008000">{</font></pre>

<pre><font color="#008000">     importlib(&quot;stdole32.tlb&quot;);</font></pre>

<pre><font color="#008000">     </font></pre>

<pre><font color="#008000">     //  Primary dispatch interface for CShowStringDoc</font></pre>

<pre><font color="#008000">     </font></pre>

<pre><font color="#008000">     [ uuid(61C76C07-70EA-11D0-9AFF-0080C81A397C) ]</font></pre>

<pre><font color="#008000">     dispinterface IShowString</font></pre>

<pre><font color="#008000">     {</font></pre>

<pre><font color="#008000">          properties:</font></pre>

<pre><font color="#008000">               // NOTE - ClassWizard will maintain property information here.</font></pre>

<pre><font color="#008000">               //    Use extreme caution when editing this section.</font></pre>

<pre><font color="#008000">               //{{AFX_ODL_PROP(CShowStringDoc)</font></pre>

<pre><font color="#008000">               [id(1)] BSTR String;</font></pre>

<pre><font color="#008000">               [id(2)] short Color;</font></pre>

<pre><font color="#008000">               [id(3)] boolean HorizCenter;</font></pre>

<pre><font color="#008000">               [id(4)] boolean VertCenter;</font></pre>

<pre><font color="#008000">               //}}AFX_ODL_PROP</font></pre>

<pre><font color="#008000">               </font></pre>

<pre><font color="#008000">          methods:</font></pre>

<pre><font color="#008000">               // NOTE - ClassWizard will maintain method information here.</font></pre>

<pre><font color="#008000">               //    Use extreme caution when editing this section.</font></pre>

<pre><font color="#008000">               //{{AFX_ODL_METHOD(CShowStringDoc)</font></pre>

<pre><font color="#008000">               [id(5)] void ShowWindow();</font></pre>

<pre><font color="#008000">               [id(6)] void RefreshWindow();</font></pre>

<pre><font color="#008000">               //}}AFX_ODL_METHOD</font></pre>

<pre><font color="#008000">     };</font></pre>

<pre><font color="#008000">     //  Class information for CShowStringDoc</font></pre>

<pre><font color="#008000">     </font></pre>

<pre><font color="#008000">     [ uuid(61C76C05-70EA-11D0-9AFF-0080C81A397C) ]</font></pre>

<pre><font color="#008000">     coclass Document</font></pre>

<pre><font color="#008000">     {</font></pre>

<pre><font color="#008000">          [default] dispinterface IShowString;</font></pre>

<pre><font color="#008000">     };</font></pre>

<pre><font color="#008000">     //{{AFX_APPEND_ODL}}</font></pre>

<pre><font color="#008000">     //}}AFX_APPEND_ODL}}</font></pre>

<pre><font color="#008000">};</font></pre>

<P>This explains why Visual Basic just thought of all four properties as properties; that's how they are listed in this ODL file. The two methods are here, too, in the methods section. The reason you passed <font 
color="#008000">&quot;ShowString.Document&quot;</font> to <font color="#008000">CreateObject()</font> is that there is a <font color="#008000">coclass </font><font color="#008000">Document</font> section here. It points to a dispatch interface 
(dispinterface) called <font color="#008000">IShowString</font>. Here's the interface map from <font color="#008000">ShowStringDoc.cpp</font>:</P>

<pre><font color="#008000">BEGIN_INTERFACE_MAP(CShowStringDoc, CDocument)</font></pre>

<pre><font color="#008000">     INTERFACE_PART(CShowStringDoc, IID_IShowString, Dispatch)</font></pre>

<pre><font color="#008000">END_INTERFACE_MAP()</font></pre>

<P>So a call to <font color="#008000">CreateObject(&quot;ShowString.Document&quot;)</font> leads to the coclass section of the odl file, which points to <font color="#008000">IShowString</font>. The interface map points from <font 
color="#008000">IShowString</font> to <font color="#008000">CShowStringDoc</font>, which has a dispatch map that connects the properties and methods in the outside world to C++ code. You can see that editing any of these sections by hand could have 
disastrous results. Trust the wizards to do this for you.</P>

<H3><B>From Here...</B></H3>

<P>In this chapter, you built an Automation server and controlled it from Visual Basic. Automation servers are far more powerful than older ways of application interaction, but your server doesn't have any user interaction. If the Visual Basic program 
wanted to allow the user to choose the color, that would have to be built into the Visual Basic program. The next logical step is to allow the little embedded object to react to user events like clicks and drags, and to report to the controller program 
what has happened. That's what ActiveX controls do, as you'll see in the next chapter. Some chapters you may want to read include:</P>

<ul>

<li> <A HREF="index09.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index09.htm" target="text">Chapter 9</A>, &quot;Building a Complete Application: ShowString,&quot; introduced the ShowString application.</P>

<li> <A HREF="index13.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index13.htm" target="text">Chapter 13</A>, &quot;ActiveX Concepts,&quot; is a roadmap to part IV of this book and defines many of the concepts used in this and related chapters.</P>

<li> <A HREF="index14.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index14.htm" target="text">Chapter 14</A>, &quot;Building an ActiveX Container Application,&quot; built the second version of ShowString, which acts as an ActiveX container.</P>

<li> <A HREF="index15.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index15.htm" target="text">Chapter 15</A>, &quot;Building an ActiveX Server Application,&quot; built the third version of ShowString, which acts as an ActiveX server.</P>

<li> <A HREF="index17.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index17.htm" target="text">Chapter 17</A>, &quot;Building an ActiveX Control,&quot; leaves ShowString behind and builds a control you can include in any Visual C++ or Visual Basic program.</P>

</ul>

<p><hr></p>

<center>

<p><font size=-2>

&copy; 1997, QUE Corporation, an imprint of Macmillan Publishing USA, a

Simon and Schuster Company.</font></p>

</center>

</BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -