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

📄 ch14.htm

📁 VC使用所有细节的逻列
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<pre><font color="#008000">     // get started.</font></pre>

<pre><font color="#008000">     // TODO: replace this selection mechanism with one appropriate to your app.</font></pre>

<pre><font color="#008000">     CShowStringCntrItem* m_pSelection;</font></pre>

<P>This new member variable shows up again in the view constructor, Listing 14.4, and the revised <font color="#008000">OnDraw()</font>, Listing 14.5.</P>

<P><I>Listing 14.4&#151;ShowStringView.cpp&#151;Constructor</I></P>

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

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

<pre><font color="#008000">     m_pSelection = NULL;</font></pre>

<pre><font color="#008000">     // TODO: add construction code here</font></pre>

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

<P><I>Listing 14.5&#151;ShowStringView.cpp&#151; </I>CShowStringView::OnDraw()</P>

<pre><font color="#008000">void CShowStringView::OnDraw(CDC* pDC)</font></pre>

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

<pre><font color="#008000">     CShowStringDoc* pDoc = GetDocument();</font></pre>

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

<pre><font color="#008000">     // TODO: add draw code for native data here</font></pre>

<pre><font color="#008000">     // TODO: also draw all OLE items in the document</font></pre>

<pre><font color="#008000">     // Draw the selection at an arbitrary position.  This code should be</font></pre>

<pre><font color="#008000">     //  removed once your real drawing code is implemented.  This position</font></pre>

<pre><font color="#008000">     //  corresponds exactly to the rectangle returned by CShowStringCntrItem,</font></pre>

<pre><font color="#008000">     //  to give the effect of in-place editing.</font></pre>

<pre><font color="#008000">     // TODO: remove this code when final draw code is complete.</font></pre>

<pre><font color="#008000">     if (m_pSelection == NULL)</font></pre>

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

<pre><font color="#008000">          POSITION pos = pDoc-&gt;GetStartPosition();</font></pre>

<pre><font color="#008000">          m_pSelection = (CShowStringCntrItem*)pDoc-&gt;GetNextClientItem(pos);</font></pre>

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

<pre><font color="#008000">     if (m_pSelection != NULL)</font></pre>

<pre><font color="#008000">          m_pSelection-&gt;Draw(pDC, CRect(10, 10, 210, 210));</font></pre>

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

<P>The code supplied for <font color="#008000">OnDraw()</font> draws only a single contained item. It doesn't draw any native data&#151;in other words, elements of <font color="#008000">ShowString</font> that are not contained items. At the moment there 
is no native data, but after the string is added to the application, <font color="#008000">OnDraw()</font> is going to have to draw it. What's more, this code only draws one contained item, and it does so in an arbitrary rectangle. <font 
color="#008000">OnDraw()</font> is going to see a lot of changes as you work through this chapter.</P>

<P>The view class has gained a lot of new functions. They are as follows:</P>

<ul>

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

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

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

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

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

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

</ul>

<P>Each of these new functions is discussed in the subsections that follow.</P>

<H5><A ID="I10" NAME="I10"><B>OnInitialUpdate()</B></A></H5>

<p><font color="#008000">OnInitialUpdate()</font> is called the very first time the view is to be displayed. The boilerplate code (Listing 14.6) is pretty dull.</P>

<P><I>Listing 14.6&#151;ShowStringView.cpp&#151;</I>CShowStringView::OnInitialUpdate()</P>

<pre><font color="#008000">void CShowStringView::OnInitialUpdate()</font></pre>

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

<pre><font color="#008000">     CView::OnInitialUpdate();</font></pre>

<pre><font color="#008000">     // TODO: remove this code when final selection </font></pre>

<pre><font color="#008000">     // model code is written</font></pre>

<pre><font color="#008000">     m_pSelection = NULL;    // initialize selection</font></pre>

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

<P>The base class <font color="#008000">OnInitialUpdate()</font> calls the base class <font color="#008000">OnUpdate()</font>, which calls <font color="#008000">Invalidate()</font>, requiring a full repaint of the client area.</P>

<H5><A ID="I11" NAME="I11"><B>IsSelected()</B></A></H5>

<p><font color="#008000">IsSelected()</font> currently isn&#146;t working, because the selection mechanism is so rudimentary. Listing 14.7 shows what works.</P>

<P><I>Listing 14.7&#151;ShowStringView.cpp&#151;</I>CShowStringView::IsSelected()</P>

<pre><font color="#008000">BOOL CShowStringView::IsSelected(const CObject* pDocItem) const</font></pre>

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

<pre><font color="#008000">     // The implementation below is adequate if your selection consists of</font></pre>

<pre><font color="#008000">     //  only CShowStringCntrItem objects.  To handle different selection</font></pre>

<pre><font color="#008000">     //  mechanisms, the implementation here should be replaced.</font></pre>

<pre><font color="#008000">     // TODO: implement this function that tests for a selected OLE client item</font></pre>

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

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

<P>This function is passed a pointer to a container item. If that is the same as the current selection, it returns <font color="#008000">TRUE</font>.</P>

<H5><A ID="I12" NAME="I12"><B>OnInsertObject()</B></A></H5>

<p><font color="#008000">OnInsertObject()</font> is called when the user chooses <U>E</U>dit, Insert <U>N</U>ew Object. It's quite a long function, so it is presented in parts. The overall structure is presented in Listing 14.8.</P>

<P><I>Listing 14.8&#151;ShowStringView.cpp&#151;</I>CShowStringView::OnInsertObject()</P>

<pre><font color="#008000">void CShowStringView::OnInsertObject()</font></pre>

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

<pre><font color="#008000">     //display the Insert Object dialog box</font></pre>

<pre><font color="#008000">     CShowStringCntrItem* pItem = NULL;</font></pre>

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

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

<pre><font color="#008000">          // Create new item connected to this document.</font></pre>

<pre><font color="#008000">          // Initialize the item</font></pre>

<pre><font color="#008000">          // set selection and update all views</font></pre>

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

<pre><font color="#008000">     CATCH(CException, e)</font></pre>

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

<pre><font color="#008000">          // handle failed create</font></pre>

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

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

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

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

<P>Each comment here is replaced with a small block of code, discussed in the remainder of this section. The <font color="#008000">TRY</font> and <font color="#008000">CATCH</font> statements, by the way, are on old-fashioned form of exception handling, 
discussed in <A HREF="index26.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index26.htm" target="text">Chapter 26</A>, &quot;Exceptions, Templates, and the Latest C++ Features.&quot;</P>

<P>First, this function displays the Insert Object dialog box, as shown in Listing 14.9.</P>

<P><I>Listing 14.9&#151;ShowStringView.cpp&#151;Display the Insert Object Dialog </I><I>Box</I></P>

<pre><font color="#008000">     // Invoke the standard Insert Object dialog box to obtain information</font></pre>

<pre><font color="#008000">     //  for new CShowStringCntrItem object.</font></pre>

<pre><font color="#008000">     COleInsertDialog dlg;</font></pre>

<pre><font color="#008000">     if (dlg.DoModal() != IDOK)</font></pre>

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

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

<P>If the user clicks Cancel, this function returns and nothing is inserted. If the user clicks OK, the cursor is set to an hourglass while the rest of the processing occurs.</P>

<P>To create a new item, the code in Listing 14.10 is inserted.</P>

<P><I>Listing 14.10&#151;ShowStringView.cpp&#151;Create a New Item</I></P>

<pre><font color="#008000">          // Create new item connected to this document.</font></pre>

<pre><font color="#008000">          CShowStringDoc* pDoc = GetDocument();</font></pre>

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

<pre><font color="#008000">          pItem = new CShowStringCntrItem(pDoc);</font></pre>

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

<P>This code makes sure there is a document, even though the menu item is only enabled if there is, and then creates a new container item, passing it the pointer to the document. As you see later, container items hold a pointer to the document that 
contains them.</P>

<P>The code in Listing 14.11 initializes that item.</P>

<P><I>Listing 14.11&#151;ShowStringView.cpp&#151;Initializing the Inserted Item</I></P>

<pre><font color="#008000">          // Initialize the item from the dialog data.</font></pre>

<pre><font color="#008000">          if (!dlg.CreateItem(pItem))</font></pre>

<pre><font color="#008000">               AfxThrowMemoryException();  // any exception will do</font></pre>

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

<pre><font color="#008000">          // If item created from class list (not from file) then launch</font></pre>

<pre><font color="#008000">          //  the server to edit the item.</font></pre>

<pre><font color="#008000">          if (dlg.GetSelectionType() == COleInsertDialog::createNewItem)</font></pre>

<pre><font color="#008000">               pItem-&gt;DoVerb(OLEIVERB_SHOW, this);</font></pre>

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

<P>The code in Listing 14.11 calls the <font color="#008000">CreateItem()</font> function of the dialog class, <font color="#008000">ColeInsertDialog</font>. That may seem a strange place to keep such a function, but the function needs to know all the 
answers that were given on the dialog box, so if it was a member of another class, it would have to interrogate the dialog for the type and file name, find out whether was it link or embedded, and so on. It calls member functions of the container item like 
<font color="#008000">CreateLinkFromFile()</font>, <font color="#008000">CreateFromFile()</font>, <font color="#008000">CreateNewItem()</font>, and so on. So it's not that the code to actually fill the object from the file is in the dialog box, but rather 
that the work is partitioned between the objects instead of passing information back and forth between them.</P>

<P>Then, one question is asked of the dialog box: Was this a new item? If so, the server is called to edit it. Objects created from a file can just be displayed.</P>

<P>Finally, the selection is updated and so are the views, as shown in Listing 14.12.</P>

<P><I>Listing 14.12&#151;ShowStringView.cpp&#151;Update selection and views</I></P>

⌨️ 快捷键说明

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