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

📄 ch14.htm

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

<pre><font color="#008000">          // As an arbitrary user interface design, this sets the selection</font></pre>

<pre><font color="#008000">          //  to the last item inserted.</font></pre>

<pre><font color="#008000">          // TODO: reimplement selection as appropriate for your application</font></pre>

<pre><font color="#008000">          m_pSelection = pItem;   // set selection to last inserted item</font></pre>

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

<P>If the creation of the object failed, execution ends up in the <font color="#008000">CATCH</font> block, shown in Listing 14.13.</P>

<P><I>Listing 14.13&#151;ShowStringView.cpp&#151;</I>CATCH<I> Block</I></P>

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

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

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

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

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

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

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

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

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

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

<P>This deletes the item that was created and gives the user a message box.</P>

<P>Finally, that hourglass cursor can go away:</P>

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

<H5><A ID="I13" NAME="I13"><B>OnSetFocus()</B></A></H5>

<p><font color="#008000">OnSetFocus()</font>, shown in Listing 14.14, is called whenever this view gets focus.</P>

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

<pre><font color="#008000">void CShowStringView::OnSetFocus(CWnd* pOldWnd)</font></pre>

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

<pre><font color="#008000">     COleClientItem* pActiveItem = GetDocument()-&gt;GetInPlaceActiveItem(this);</font></pre>

<pre><font color="#008000">     if (pActiveItem != NULL &amp;&amp;</font></pre>

<pre><font color="#008000">          pActiveItem-&gt;GetItemState() == COleClientItem::activeUIState)</font></pre>

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

<pre><font color="#008000">          // need to set focus to this item if it is in the same view</font></pre>

<pre><font color="#008000">          CWnd* pWnd = pActiveItem-&gt;GetInPlaceWindow();</font></pre>

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

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

<pre><font color="#008000">               pWnd-&gt;SetFocus();   // don't call the base class</font></pre>

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

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

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

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

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

<P>If there is an active item and its server is loaded, then that active item gets focus. If not, focus remains with the old window, and to the user, it appears that the click was ignored.</P>

<H5><A ID="I14" NAME="I14"><B>OnSize()</B></A></H5>

<p><font color="#008000">OnSize()</font>, shown in Listing 14.15, is called when the application is resized by the user.</P>

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

<pre><font color="#008000">void CShowStringView::OnSize(UINT nType, int cx, int cy)</font></pre>

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

<pre><font color="#008000">     CView::OnSize(nType, cx, cy);</font></pre>

<pre><font color="#008000">     COleClientItem* pActiveItem = GetDocument()-&gt;GetInPlaceActiveItem(this);</font></pre>

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

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

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

<P>This resizes the view using the base class function, and then, if there is an active item, tells it to adjust to the resized view.</P>

<H5><A ID="I15" NAME="I15"><B>OnCancelEditCntr()</B></A></H5>

<p><font color="#008000">OnCancelEditCntr()</font> is called when a user who has been editing in place presses Esc. The server must be closed, and the object stops being active. The code is shown in Listing 14.16.</P>

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

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

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

<pre><font color="#008000">     // Close any in-place active item on this view.</font></pre>

<pre><font color="#008000">     COleClientItem* pActiveItem = </font></pre>

<pre><font color="#008000">          GetDocument()-&gt;GetInPlaceActiveItem(this);</font></pre>

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

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

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

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

<pre><font color="#008000">     ASSERT(GetDocument()-&gt;GetInPlaceActiveItem(this) == NULL);</font></pre>

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

<P><A ID="I16" NAME="I16"><I>CShowStringCntrItem</I></A></P>

<P>The container item class is a completely new addition to ShowString. It describes an item that is contained in the document. As you've already seen, the document and the view use this object quite a lot, primarily through the <font 
color="#008000">m_pSelection</font> member variable of <font color="#008000">CShowStringView</font>. It has no member variables other than those inherited from the base class, <font color="#008000">COleClientItem</font>. It has overrides for a lot of 
functions, though. They are as follows:</P>

<ul>

<li> A constructor</P>

<li> A destructor</P>

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

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

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

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

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

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

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

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

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

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

</ul>

<P>The constructor simply passes the document pointer along to the base class. The destructor does nothing. <font color="#008000">GetDocument()</font> and <font color="#008000">GetActiveView()</font> are inline functions that return member variables 
inherited from the base class by calling the base class function with the same name and casting the result.</P>

<p><font color="#008000">OnChange()</font> is the first of these functions that has more than one line of code (see Listing 14.17).</P>

<P><I>Listing 14.17&#151;CntrItem.cpp&#151;</I>CShowStringCntrItem::OnChange()</P>

<pre><font color="#008000">void CShowStringCntrItem::OnChange(OLE_NOTIFICATION nCode, </font></pre>

<pre><font color="#008000">     DWORD dwParam)</font></pre>

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

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

<pre><font color="#008000">     COleClientItem::OnChange(nCode, dwParam);</font></pre>

<pre><font color="#008000">     // When an item is being edited (either in-place or fully open)</font></pre>

<pre><font color="#008000">     //  it sends OnChange notifications for changes in the state of the</font></pre>

<pre><font color="#008000">     //  item or visual appearance of its content.</font></pre>

<pre><font color="#008000">     // TODO: invalidate the item by calling UpdateAllViews</font></pre>

<pre><font color="#008000">     //  (with hints appropriate to your application)</font></pre>

<pre><font color="#008000">     GetDocument()-&gt;UpdateAllViews(NULL);</font></pre>

<pre><font color="#008000">          // for now just update ALL views/no hints</font></pre>

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

<P>Actually, there are three lines of code. The comments are actually more useful than the code. When the user changes the contained item, the server notifies the container. Calling <font color="#008000">UpdateAllViews()</font> is a rather drastic way of 
refreshing the screen, but it gets the job done.</P>

<p><font color="#008000">OnActivate()</font> (shown in Listing 14.18) is called when a user double-clicks an item to activate it and edit it in place. ActiveX objects are usually <I>outside-in</I>, which means that a single click of the item selects it 
but does not activate it. Activating an outside-in object requires a double click, or a single click followed by choosing the appropriate OLE verb from the Edit menu.</P>

<P><I>Listing 14.18&#151;CntrItem.cpp&#151;</I>CShowStringCntrItem::OnActivate()</P>

<pre><font color="#008000">void CShowStringCntrItem::OnActivate()</font></pre>

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

<pre><font color="#008000">    // Allow only one inplace activate item per frame</font></pre>

<pre><font color="#008000">    CShowStringView* pView = GetActiveView();</font></pre>

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

<pre><font color="#008000">    COleClientItem* pItem = GetDocument()-&gt;GetInPlaceActiveItem(pView);</font></pre>

<pre><font color="#008000">    if (pItem != NULL &amp;&amp; pItem != this)</font></pre>

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

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

<pre><font color="#008000">    COleClientItem::OnActivate();</font></pre>

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

<P>This code makes sure that the current view is valid, closes the active item, if any, and then activates this item.</P>

<p><font color="#008000">OnGetItemPosition()</font> (shown in Listing 14.19) is called as part of the in-place activation process.</P>

<P><I>Listing 14.19&#151;CntrItem.cpp&#151;</I>CShowStringCntrItem:: OnGetItemPosition()</P>

<pre><font color="#008000">void CShowStringCntrItem::OnGetItemPosition(CRect&amp; rPosition)</font></pre>

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

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

⌨️ 快捷键说明

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