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

📄 ch15.htm

📁 VC使用所有细节的逻列
💻 HTM
📖 第 1 页 / 共 5 页
字号:

<pre><font color="#008000">     // TODO: set mapping mode and extent</font></pre>

<pre><font color="#008000">     //  (The extent is usually the same as the size returned from OnGetExtent)</font></pre>

<pre><font color="#008000">     pDC-&gt;SetMapMode(MM_ANISOTROPIC);</font></pre>

<pre><font color="#008000">     pDC-&gt;SetWindowOrg(0,0);</font></pre>

<pre><font color="#008000">     pDC-&gt;SetWindowExt(3000, 3000);</font></pre>

<pre><font color="#008000">     COLORREF oldcolor;</font></pre>

<pre><font color="#008000">     switch (pDoc-&gt;GetColor())</font></pre>

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

<pre><font color="#008000">     case 0:</font></pre>

<pre><font color="#008000">          oldcolor = pDC-&gt;SetTextColor(RGB(0x80,0x80,0x80)); //gray</font></pre>

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

<pre><font color="#008000">     case 1:</font></pre>

<pre><font color="#008000">          oldcolor = pDC-&gt;SetTextColor(RGB(0xB0,0,0)); // dull red</font></pre>

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

<pre><font color="#008000">     case 2:</font></pre>

<pre><font color="#008000">          oldcolor = pDC-&gt;SetTextColor(RGB(0,0xB0,0)); // dull green</font></pre>

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

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

<pre><font color="#008000">     int DTflags = 0;</font></pre>

<pre><font color="#008000">     if (pDoc-&gt;GetHorizcenter())</font></pre>

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

<pre><font color="#008000">          DTflags |= DT_CENTER;</font></pre>

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

<pre><font color="#008000">     if (pDoc-&gt;GetVertcenter())</font></pre>

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

<pre><font color="#008000">          DTflags |= (DT_VCENTER|DT_SINGLELINE);</font></pre>

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

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

<pre><font color="#008000">     CRect rect;</font></pre>

<pre><font color="#008000">     rect.TopLeft() = pDC-&gt;GetWindowOrg();</font></pre>

<pre><font color="#008000">     rect.BottomRight() = rect.TopLeft() + pDC-&gt;GetWindowExt();</font></pre>

<pre><font color="#008000">     pDC-&gt;DrawText(pDoc-&gt;GetString(), &amp;rect, DTflags);</font></pre>

<pre><font color="#008000">     pDC-&gt;SetTextColor(oldcolor);</font></pre>

<pre><font color="#008000">     return TRUE;</font></pre>

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

<P>The function starts with the boilerplate from AppWizard. With an application that doesn't just draw itself in whatever space is provided, you would want to add code to determine the extent rather than just using (3000,3000). (You'd want to add the code 
to <font color="#008000">OnGetExtent(),</font> too.) But hardcoding the numbers works for this simple example. Next, paste in the drawing code from the view's <font color="#008000">OnDraw(),</font> but change the colors slightly to give the user a 
reminder.</P>

<P>Build the application, fix any typos or other simple errors, and then start Excel and insert a ShowString document into your worksheet. ShowString should run as before, with <font color="#008000">Hello, world!</font> in the center of the view. Convince 
yourself that the Options dialog box still works and that you have restored all the old functionality. Be sure to change at least one thing: the string, the color, or the centering. Then, press Esc to finish editing in place. Oops! It still draws the old 
<font color="#008000">Hello, world!</font> in gray in the center of the server area. Why?</P>

<P>Remember that in <font color="#008000">CShowStringDoc::OnToolsOptions(),</font> after the user clicks OK, you tell the document that it has been changed and arrange to have the view redrawn:</P>

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

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

<P>You need to add another line there to make sure that any containers that are containing this document are also notified:</P>

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

<P>Now, build it again, and insert a different ShowString object into a Word document. This time the changes are reflected in the inactive server display as well. Figure 15.12 shows a ShowString item being edited in place, and Figure 15.13 shows the same 
item inactive.</P>

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

<P><I>This ShowString item is being edited in place.</I></P>

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

<P><I>This ShowString item is inactive.</I></P>

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

<P>There is one oddity you may notice as you edit. If you choose to have the string centered horizontally when it is inactive, the first character of the string is centered, but when it is active the entire string is centered. Because the code is 
identical for these cases, this behavior has to be blamed on MFC.</P>

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

<P>Good old ShowString has been through a lot. It's time for one more transformation.</P>

<H3><B>Applications that Are both Container and Server</B></H3>

<P>As you might expect, adding container features to this version of ShowString is as difficult as adding them to the ordinary ShowString of the previous chapter. If you add these features, you gain an application that can tap the full power of ActiveX to 
bring extraordinary power to your work and your documents.</P>

<P><B> Building Another Version of ShowString</B></P>

<P>The way to get a ShowString that is both a container and a server is to follow these steps:</P>

<ol>

<li><P> Build a new ShowString with AppWizard that is a container and a full server. Run AppWizard as usual, but in a different directory than the one where you created the server-only ShowString. Be sure to select the Both Container And Server radio 
button in Step 3. In Step 4, be sure to click the Advanced button and change the file name types as you did earlier in this chapter. And finally, when asked whether you want to use the same CLSID, say No. This is a different application.</P>

<li><P> Make the container changes from the previous chapter. When adding the Tools Options menu item and accelerator, add it to the main menu, the server in-place menu, and the server-embedded menu.</P>

<li><P> Make the server changes from this chapter.</P>

<li><P> Add the ShowString functionality.</P>

</ol>

<P>This section does not present the process of building a container and server application in detail; that is covered in the &quot;Adding Server Capabilities to ShowString&quot; section of this chapter and all of the previous chapter. Rather, the focus 
here is on the consequences of building such an application.</P>

<P><B> </B><B>Nesting and Recursion Issues</B></P>

<P>After an application is both a server (meaning its documents can be embedded in other applications) and a container, it is possible to create nested documents. For example, an Excel spreadsheet might contain a Word document, which, in turn, contains a 
bitmap, as shown in Figure 15.14.</P>

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

<P><I>This Excel spreadsheet contains a Word document that contains a bitmap.</I></P>

<P>Within Excel, you can double-click the Word document to edit it in place, as shown in Figure 15.15, but you cannot go on to double-click the bitmap and edit it in place, too. You can edit it in a window of its own, as shown in Figure 15.16. It is a 
limitation of ActiveX that you cannot nest in-place editing sessions indefinitely.</P>

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

<P><I>This Word document is being edited in place.</I></P>

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

<P><I>This bitmap is nested within a Word document within an Excel spreadsheet, and so cannot be edited in </I><I>place. Instead, it is edited in a separate window.</I></P>

<H3><B>ActiveX Documents</B></H3>

<P>The final, important recent addition to ActiveX is ActiveX documents, also known as ActiveX Document Objects. An ordinary ActiveX server takes over the menus and interface of a container application when the document is being edited in place, but does 
so in cooperation with the container application. An ActiveX Document server takes over far more dramatically.</P>

<P><B>What ActiveX Documents Do</B></P>

<P>The first application to demonstrate the use of ActiveX Documents is the Microsoft Office Binder, shown in Figure 15.17. To the user, it appears that this application can open any Office document. In reality, the documents are opened with their own 
server applications while the frame around them and the list of other documents remains intact. Microsoft Internet Explorer 3.0 is also an ActiveX Document container&#151;Figure 15.18 shows a Word document open in Explorer. Notice the menus are Word menus, 
but the Explorer toolbar can still be used. For example, clicking the Back button closes this Word document and opens the document that was loaded previously.</P>

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

<P><I>The Microsoft Office Binder makes it simple to pull Office documents together.</I></P>

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

<P><I>Microsoft Internet Explorer is also a container for ActiveX documents.</I></P>

<P>What this means to users is a complete transition to a document-centered approach. No matter what application the user is working with, any kind of document can be opened and edited, using the code written to work with that document but the interface 
that the user has learned for his or her own application.</P>

<P><B>Making ShowString an ActiveX Document Server</B></P>

<P>Making yet another version of ShowString, this one as an ActiveX document server, is pretty simple. Follow the instructions from the &quot;AppWizard's Server Boilerplate&quot; subsection at the beginning of this chapter, with two exceptions: in 
AppWizard's Step 3, select ActiveX document support, and in AppWizard's Step 4, click the Advanced button. Fix the file type names, and fill in the file extension as .<font color="#008000">sst</font>, as shown in Figure 15.19. This helps ActiveX document 
containers determine what application to launch when you open a ShowString file.</P>

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

<P><I>The Advanced Options dialog box of App Wizard's Step 4 is where you specify the extension for </I><I>ShowString files.</I></P>

<P><B>Document Extension Boilerplate</B></P>

<P>Any one of the versions of ShowString built up to this point could have had a document extension specified. AppWizard adds these lines to <font color="#008000">CShowStringApp::InitInstance()</font> when you specify a document extension for an ActiveX 
document server application:</P>

<pre><font color="#008000">     // Enable drag/drop open</font></pre>

<pre><font color="#008000">     m_pMainWnd-&gt;DragAcceptFiles();</font></pre>

<pre><font color="#008000">     // Enable DDE Execute open</font></pre>

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

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

<P>It is the call to <font color="#008000">RegisterShellFileTypes</font> that matters here, though the drag and drop is a nice touch. You're able to drag files from your desktop or a folder onto the ShowString icon or an open copy of ShowString, and the 
file opens in ShowString.</P>

<P><B>ActiveX Document Server Boilerplate</B></P>

<P>Selecting ActiveX document support makes remarkably little difference to the code generated by AppWizard. In <font color="#008000">CShowStringApp::InitInstance(),</font>the versions of ShowString that were not ActiveX document servers had this call to 
update the Registry:</P>

<pre><font color="#008000">     m_server.UpdateRegistry(OAT_INPLACE_SERVER);</font></pre>

<P>The ActiveX document version of Showstring has this li

⌨️ 快捷键说明

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