📄 ch15.htm
字号:
<li> <font color="#008000">A destructor</font></pre>
<li> <font color="#008000">AssertValid()</font></pre>
<li> <font color="#008000">Dump()</font></pre>
<li> <font color="#008000">OnCreate()</font></pre>
<li> <font color="#008000">OnCreateControlBars()</font></pre>
<li> <font color="#008000">PreCreateWindow()</font></pre>
</ul>
<P>The constructor and destructor do nothing. <font color="#008000">AssertValid()</font> and <font color="#008000">Dump()</font> are debug functions that simply call the base class functions. <font color="#008000">OnCreate()</font> actually has code,
shown in Listing 15.9.</P>
<p><img src="cd_rom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/cd_rom.gif" hspace=10>
<P><I>Listing 15.9—IPFrame.cpp—CInPlaceFrame::OnCreate()</I></P>
<pre><font color="#008000">int CInPlaceFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)</font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000"> if (COleIPFrameWnd::OnCreate(lpCreateStruct) == -1)</font></pre>
<pre><font color="#008000"> return -1;</font></pre>
<pre><font color="#008000"> // CResizeBar implements in-place resizing.</font></pre>
<pre><font color="#008000"> if (!m_wndResizeBar.Create(this))</font></pre>
<pre><font color="#008000"> {</font></pre>
<pre><font color="#008000"> TRACE0("Failed to create resize bar\n");</font></pre>
<pre><font color="#008000"> return -1; // fail to create</font></pre>
<pre><font color="#008000"> }</font></pre>
<pre><font color="#008000"> // By default, it is a good idea to register a drop-target that does</font></pre>
<pre><font color="#008000"> // nothing with your frame window. This prevents drops from</font></pre>
<pre><font color="#008000"> // "falling through" to a container that supports drag-drop.</font></pre>
<pre><font color="#008000"> m_dropTarget.Register(this);</font></pre>
<pre><font color="#008000"> return 0;</font></pre>
<pre><font color="#008000">}</font></pre>
<P>This function catches the <font color="#008000">WM_CREATE</font> message that is sent when an in-place frame is created and drawn on the screen. It calls the base class function, then creates the resize bar. Finally, it registers a drop target, so that
if anything is dropped over this in-place frame, it is dropped on this server rather than the underlying container.</P>
<P>When a server document is activated in place, <font color="#008000">COleServerDoc::ActivateInPlace()</font> calls <font color="#008000">CInPlaceFrame::OnCreateControlBars()</font>, which is shown in Listing 15.10.</P>
<p><img src="cd_rom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/cd_rom.gif" hspace=10>
<P><I>Listing 15.10—IPFrame.cpp—CInPlaceFrame::OnCreateControlBars()</I></P>
<pre><font color="#008000">BOOL CInPlaceFrame::OnCreateControlBars(CFrameWnd* pWndFrame, CFrameWnd* pWndDoc)</font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000"> // Set owner to this window, so messages are delivered to correct app</font></pre>
<pre><font color="#008000"> m_wndToolBar.SetOwner(this);</font></pre>
<pre><font color="#008000"> // Create toolbar on client's frame window</font></pre>
<pre><font color="#008000"> if (!m_wndToolBar.Create(pWndFrame) ||</font></pre>
<pre><font color="#008000"> !m_wndToolBar.LoadToolBar(IDR_SHOWSTTYPE_SRVR_IP))</font></pre>
<pre><font color="#008000"> {</font></pre>
<pre><font color="#008000"> TRACE0("Failed to create toolbar\n");</font></pre>
<pre><font color="#008000"> return FALSE;</font></pre>
<pre><font color="#008000"> }</font></pre>
<pre><font color="#008000"> // TODO: Remove this if you don't want tool tips or a resizeable toolbar</font></pre>
<pre><font color="#008000"> m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |</font></pre>
<pre><font color="#008000"> CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);</font></pre>
<pre><font color="#008000"> // TODO: Delete these three lines if you don't want the toolbar to</font></pre>
<pre><font color="#008000"> // be dockable</font></pre>
<pre><font color="#008000"> m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);</font></pre>
<pre><font color="#008000"> pWndFrame->EnableDocking(CBRS_ALIGN_ANY);</font></pre>
<pre><font color="#008000"> pWndFrame->DockControlBar(&m_wndToolBar);</font></pre>
<pre><font color="#008000"> return TRUE;</font></pre>
<pre><font color="#008000">}</font></pre>
<P>This function creates a docking, resizable toolbar with tool tips, docked against the edge of the main frame window for the application.</P>
<blockquote><p><img src="tip.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/tip.gif">
<P>If you are developing an MDI application and prefer the toolbar against the document frame, use <font color="#008000">pWndDoc</font> instead of <font color="#008000">PWndFrame</font>, in the call to <font color="#008000">m_wndToolBar.Create()</font>
but be sure to check that it is not <font color="#008000">NULL</font>.</P>
<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>
<P>The last function in <font color="#008000">CInPlaceFrame</font> is <font color="#008000">PreCreateWindow().</font> At the moment, it just calls the base class, as shown in Listing 15.11.</P>
<p><img src="cd_rom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/cd_rom.gif" hspace=10>
<P><I>Listing 15.11—IPFrame.cpp—CInPlaceFrame::PreCreateWindow()</I></P>
<pre><font color="#008000">BOOL CInPlaceFrame::PreCreateWindow(CREATESTRUCT& cs)</font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000"> // TODO: Modify the Window class or styles here by modifying</font></pre>
<pre><font color="#008000"> // the CREATESTRUCT cs</font></pre>
<pre><font color="#008000"> return COleIPFrameWnd::PreCreateWindow(cs);</font></pre>
<pre><font color="#008000">}</font></pre>
<P>This function is called before <font color="#008000">OnCreate()</font> and sets up the styles for the frame window through a <font color="#008000">CREATESTRUCT</font>.</P>
<blockquote><p><img src="caution.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/caution.gif">
<P>Modifying these styles is not for the faint of heart. The Microsoft documentation recommends reading the source code for all the classes in the hierarchy of your <font color="#008000">CInPlaceFrame</font> (<font color="#008000">Cwnd</font>, <font
color="#008000">CFrameWnd</font>, <font color="#008000">COleIPFrameWnd</font>) to see what <font color="#008000">CREATESTRUCT</font> elements are already set before making any changes. For this sample application, don't change the <font
color="#008000">CREATESTRUCT</font>.</P>
<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>
<P><B>Shortcomings of This Server</B></P>
<P>Apart from the fact that the starter application from AppWizard doesn't show a string, what's missing from this server? The <font color="#008000">OnDraw() and </font><font color="#008000">GetExtent() TODOs</font> are the only significant tasks left for
you by AppWizard. Try building ShowString, then run it once stand-alone just to register it.</P>
<P>Figure 15.4 shows the Object dialog box in Microsoft Word, reached by choosing <U>I</U>nsert, <U>O</U>bject. ShowString appears in this list as ShowSt Document—not surprising considering the menu name was IDR_SHOWSTTYPE. Developer Studio calls
this document a ShowSt document. This setting could have been overriden in AppWizard by choosing the Advanced button in Step 4 of AppWizard. Figure 15.5 shows this dialog box and the long and short names of the file type.</P>
<A HREF="Qfigs04.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch15/Qfigs04.gif"><b>Fig. 15.4</b></A>
<P><I>The ShowString document type, called ShowSt document, now appears in the Insert Object dialog box </I><I>when inserting a new object into a Word document.</I></P>
<A HREF="Qfigs05.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch15/Qfigs05.gif"><b>Fig. 15.5</b></A>
<P><I>The Advanced options dialog box of Step 4 in AppWizard provides an opportunity to change the name of </I><I>the file type.</I></P>
<P>So, the file type names used by the Registry have been set incorrectly for this project. The next few pages take you on a tour of the way file type names are stored and show you how difficult they are to change.</P>
<P>The file type name has been stored in the string table. It is the caption of the IDR_SHOWSTTYPE resource, and AppWizard has set it to:</P>
<pre><font color="#008000">\nShowSt\nShowSt\n\n\nShowString.Document\nShowSt Document</font></pre>
<p><img src="cd_rom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/cd_rom.gif" hspace=10>
<P>To look at this string, choose String Table from the Resource View, open the only string table there, click IDR_SHOWSTTYPE once to highlight it, and choose <U>V</U>iew, <U>P</U>roperties. This string is saved in the document template when a new one is
constructed in <font color="#008000">CShowStringApp::InitInstance()</font>, like this:</P>
<P><I>Listing 15.12— ShowString.cpp— excerpt from </I><I><font color="#008000">ShowStringApp::InitInstance()</font></I></P>
<pre><font color="#008000"> pDocTemplate = new CMultiDocTemplate(</font></pre>
<pre><font color="#008000"> IDR_SHOWSTTYPE,</font></pre>
<pre><font color="#008000"> RUNTIME_CLASS(CShowStringDoc),</font></pre>
<pre><font color="#008000"> RUNTIME_CLASS(CChildFrame), // custom MDI child frame</font></pre>
<pre><font color="#008000"> RUNTIME_CLASS(CShowStringView));</font></pre>
<P>The caption of the menu resource holds seven strings, and each is used by a different part of the framework. They are separated by the newline character, \n. The seven strings, their purposes, and the values provided by AppWizard for ShowString are as
follows:</P>
<ul>
<li> <B>Window Title.</B>Used by SDI apps in the title bar. For ShowString: not provided.</P>
<li> <B>Document Name.</B>Used as the root for default document names. For ShowString: <font color="#008000">ShowSt</font>, so that new documents will be <font color="#008000">ShowSt1</font>, <font color="#008000">ShowSt2</font>, and so on.</P>
<li> <B>File New Name.</B>Prompt in the File New dialog box for file type. (For example, in Developer Studio there are eight file types, including Text File and Project Workspace.) For ShowString: <font color="#008000">ShowSt</font>.</P>
<li> <B>Filter Name.</B>An entry for the drop-down box List File of Type in the File Open dialog box. For ShowString: not provided.</P>
<li> <B>Filter Extension.</B>The extension that matches the filter name. For ShowString: not provided.</P>
<li> <B>Registry File Type ID.</B>A short string to be stored in the Registry. For ShowString: <font color="#008000">ShowString.Document</font>.</P>
<li> <B>Registry File Type Name.</B>A longer string that shows in dialog boxes involving the Registry. For Showstring: <font color="#008000">ShowSt Document</font>.</P>
</ul>
<P>(Look again at Figure 15.5 and you can see where these values came from.) Try changing the last entry. In the Properties dialog box, change the caption so that the last element of the string is <font color="#008000">ShowString </font><font
color="#008000">Document</font>. Build the project. Run it once and exit. In the output section of Developer Studio you see these messages:</P>
<pre><font color="#008000">Warning: Leaving value 'ShowSt Document' for key 'ShowString.Document' </font></pre>
<pre><font color="#008000"> in registry</font></pre>
<pre><font color="#008000"> intended value was 'ShowString Document'.</font></pre>
<pre><font color="#008000">Warning: Leaving value 'ShowSt Document' for key </font></pre>
<pre><font color="#008000"> 'CLSID\{0B1DEE40-C373-11CF-870C-00201801DDD6}' in registry</font></pre>
<pre><font color="#008000"> intended value was 'ShowString Document'.</font></pre>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -