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

📄 ch19.htm

📁 24小时学会vc++
💻 HTM
📖 第 1 页 / 共 3 页
字号:
		<TD ALIGN="LEFT" VALIGN="TOP"><B>Message</B></TD>		<TD ALIGN="LEFT" VALIGN="TOP"><B>Function</B></TD>	</TR>	<TR ALIGN="LEFT" rowspan="1">		<TD ALIGN="LEFT" VALIGN="TOP"><TT>IDC_TREE</TT></TD>		<TD ALIGN="LEFT" VALIGN="TOP"><TT>TVN_BEGINDRAG</TT></TD>		<TD ALIGN="LEFT" VALIGN="TOP"><TT>OnBegindragTree</TT></TD>	</TR>	<TR ALIGN="LEFT" rowspan="1">		<TD ALIGN="LEFT" VALIGN="TOP"><TT>CAboutDlg</TT></TD>		<TD ALIGN="LEFT" VALIGN="TOP"><TT>WM_MOUSEMOVE</TT></TD>		<TD ALIGN="LEFT" VALIGN="TOP"><TT>OnMouseMove</TT></TD>	</TR>	<TR ALIGN="LEFT" rowspan="1">		<TD ALIGN="LEFT" VALIGN="TOP"><TT>CAboutDlg</TT></TD>		<TD ALIGN="LEFT" VALIGN="TOP"><TT>WM_LBUTTONUP</TT></TD>		<TD ALIGN="LEFT" VALIGN="TOP"><TT>OnLButtonUp</TT></TD>	</TR></TABLE></P><P>The source code for the three functions is provided in Listing 19.6.<H4><FONT COLOR="#000077">TYPE: Listing 19.6. Functions used to implement simpledrag and drop.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT>void CAboutDlg::OnBegindragTree(NMHDR* pNMHDR, LRESULT* pResult)</TT><TT>{</TT><TT>    NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;</TT><TT>    m_dragItem = pNMTreeView-&gt;itemNew.hItem;</TT><TT>    if( m_tree.GetParentItem( m_dragItem ) != NULL )</TT><TT>    {</TT><TT>        CImageList* pDragImage;</TT><TT>        pDragImage = m_tree.CreateDragImage( m_dragItem );</TT><TT>        m_tree.SelectItem( m_dragItem );</TT><TT>        pDragImage-&gt;BeginDrag( 0, CPoint(0,0) );</TT><TT>        pDragImage-&gt;DragEnter( &amp;m_tree, pNMTreeView-&gt;ptDrag );</TT><TT>        SetCapture();</TT><TT>        m_bIsDragging = TRUE;</TT><TT>        delete pDragImage;</TT><TT>    }</TT><TT>    *pResult = 0 ;</TT><TT>}</TT><TT>void CAboutDlg::OnMouseMove(UINT nFlags, CPoint point)</TT><TT>{</TT><TT>    if( m_bIsDragging != FALSE )</TT><TT>    {</TT><TT>        CPoint      ptTree( point );</TT><TT>        MapWindowPoints( &amp;m_tree, &amp;ptTree, 1 );</TT><TT>        CImageList::DragMove( ptTree );</TT><TT>        UINT uHitTest = TVHT_ONITEM;</TT><TT>        m_dragTarget = m_tree.HitTest( ptTree, &amp;uHitTest );</TT><TT>    }</TT><TT>    CDialog::OnMouseMove(nFlags, point);</TT><TT>}</TT><TT>void CAboutDlg::OnLButtonUp(UINT nFlags, CPoint point)</TT><TT>{</TT><TT>    if( m_bIsDragging != FALSE )</TT><TT>    {</TT><TT>        CImageList::DragLeave( &amp;m_tree );</TT><TT>        CImageList::EndDrag();</TT><TT>        ReleaseCapture();</TT><TT>        m_bIsDragging = FALSE;</TT><TT>        if( m_dragTarget != NULL )</TT><TT>        {</TT><TT>            HTREEITEM hParent;</TT><TT>            hParent = m_tree.GetParentItem( m_dragTarget );</TT><TT>            CString szLabel = m_tree.GetItemText( m_dragItem );</TT><TT>            if( hParent != NULL )</TT><TT>                m_tree.InsertItem( szLabel, 1, 1, hParent,</TT><TT>                                   m_dragTarget );</TT><TT>            else</TT><TT>                m_tree.InsertItem( szLabel, 1, 1, m_dragTarget,</TT><TT>                                   TVI_FIRST );</TT><TT>            m_tree.DeleteItem( m_dragItem );</TT><TT>        }</TT><TT>    }</TT><TT>    else</TT><TT>        CDialog::OnLButtonUp(nFlags, point);</TT></FONT></PRE><P><TT>}</TT> The source code in Listing 19.6 is all you need to perform a drag-and-dropoperation inside a single control. The drag sequence begins with the tree view controlsending the <TT>TVN_DRAGBEGIN</TT> to the control's parent--in this case, the TreeExAbout dialog box. The MFC framework translates this message into a <TT>CAboutDlg::OnBegindragTree</TT>function call. Inside this function, the handle to the drag item is stored in <TT>m_dragItem</TT>.<BLOCKQUOTE>	<P><HR><B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>In this example,	you aren't dragging items at the first, or root level, so <TT>GetParentItem</TT>	is used to get a handle to the drag item's parent; if <TT>NULL</TT> is returned,	the item is at the root level, and the drag never starts. <HR></BLOCKQUOTE><P>As the mouse is moved across the screen during the drag and drop, <TT>WM_MOUSEMOVE</TT>messages are sent to the dialog box, just as in the ListEx example from Hour 18.</P><P>At some point, the user releases the left mouse button, resulting in a <TT>WM_LBUTTONUP</TT>message, which is translated into a call to the <TT>CAboutDlg::OnLButtonUp</TT> function.If a drag and drop is in progress, the operation is completed by calling the <TT>DragLeave</TT>and <TT>EndDrag</TT> functions. The mouse capture is released, and the <TT>m_bIsDragging</TT>flag is set to <TT>FALSE</TT>.</P><P>The completion of a drag and drop is slightly more complicated in a tree viewcontrol than in a list view control. If the drop target is a second-level item, thedrag item is inserted just after the drop target. If the drop target is a root-levelitem, the drag item is inserted as the first child item. These calls to the <TT>InsertItem</TT>function use a fourth parameter, which is a handle for the item just before the newitem. This parameter can be one of three predefined values:<UL>	<LI><TT>TVI_FIRST</TT> inserts the item as the first child, as used in this example.	<LI><TT>TVI_LAST</TT> inserts the item as the last child.	<LI><TT>TVI_SORT</TT> sorts the item alphabetically.</UL><P>After the drag item has been inserted in a new position, the old position is removedusing the <TT>DeleteItem</TT> function. In order to implement a copy-drag, just eliminatethe call to <TT>DeleteItem</TT>.<H3><FONT COLOR="#000077"><B>Performing In-Place Label Editing</B></FONT></H3><P>Like the list view control, the tree view control offers a built-in Edit controlthat you can use to edit items contained in the control. In order to take advantageof this capability, the tree view control must have its Edit labels property checked.</P><P>In addition to setting the tree view control style, there are two messages thatrelate to label editing:<UL>	<LI><TT>TVN_BEGINLABELEDIT</TT>, which is sent just before the label editing begins.	<LI><TT>TVN_ENDLABELEDIT</TT>, which is sent after editing is completed or after	the user has cancelled the editing operation.</UL><P>These messages are handled exactly as they are for a list view control. When youreceive <TT>TVN_BEGINLABELEDIT</TT>, you can prevent a label from being edited bysetting <TT>*pResult</TT> to <TT>TRUE</TT>, and you can allow editing to proceedby setting <TT>*pResult</TT> to <TT>FALSE</TT>. In addition, you can use the <TT>TVN_BEGINLABELEDIT</TT>message to take control of the tree view's edit control.</P><P>The <TT>TVN_ENDLABELEDIT</TT> message is used exactly like the <TT>LVN_ENDLABELEDIT</TT>message is used with a list view control.<BLOCKQUOTE>	<P><HR><B> </B><FONT COLOR="#000077"><B>CAUTION:</B></FONT><B> </B>If you use the newly	edited label text in your application, make sure to look out for situations in which	the user has cancelled the label editing operation. When this happens, the <TT>TV_ITEM</TT>	<TT>pszText</TT> member variable is set to <TT>NULL</TT>, or the <TT>iItem</TT> member	variable is set to <TT>-1</TT>. <HR></BLOCKQUOTE><P>Add new message-handling functions for the label editing messages using the valuesin Table 19.6.<H4><FONT COLOR="#000077">Table 19.6. Message-handling functions used for label editing.</FONT></H4><P><TABLE BORDER="1">	<TR ALIGN="LEFT" rowspan="1">		<TD ALIGN="LEFT" VALIGN="TOP"><B>Class Name</B></TD>		<TD ALIGN="LEFT" VALIGN="TOP"><B>Object ID</B></TD>		<TD ALIGN="LEFT" VALIGN="TOP"><B>Message</B></TD>		<TD ALIGN="LEFT" VALIGN="TOP"><B>Function</B></TD>	</TR>	<TR ALIGN="LEFT" rowspan="1">		<TD ALIGN="LEFT" VALIGN="TOP"><TT>CAboutDlg</TT></TD>		<TD ALIGN="LEFT" VALIGN="TOP"><TT>IDC_TREE</TT></TD>		<TD ALIGN="LEFT" VALIGN="TOP"><TT>TVN_BEGINLABELEDIT</TT></TD>		<TD ALIGN="LEFT" VALIGN="TOP"><TT>OnBeginlabeleditTree</TT></TD>	</TR>	<TR ALIGN="LEFT" rowspan="1">		<TD ALIGN="LEFT" VALIGN="TOP"><TT>CAboutDlg</TT></TD>		<TD ALIGN="LEFT" VALIGN="TOP"><TT>IDC_TREE</TT></TD>		<TD ALIGN="LEFT" VALIGN="TOP"><TT>TVN_ENDLABELEDIT</TT></TD>		<TD ALIGN="LEFT" VALIGN="TOP"><TT>OnEndlabeleditTree</TT></TD>	</TR></TABLE></P><P>The source code for these functions is provided in Listing 19.7.<H4><FONT COLOR="#000077">TYPE: Listing 19.7. Functions used to implement simpledrag and drop.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT>void CAboutDlg::OnBeginlabeleditTree(NMHDR* pNMHDR, LRESULT* pResult)</TT><TT>{</TT><TT>    *pResult = FALSE;</TT><TT>}</TT><TT>void CAboutDlg::OnEndlabeleditTree(NMHDR* pNMHDR, LRESULT* pResult)</TT><TT>{</TT><TT>    *pResult = TRUE;</TT></FONT></PRE><P><TT>}</TT> Compile and run the TreeEx project. Experiment with editing item labels,drag and drop, and removing items in the About dialog box.<H2><FONT COLOR="#000077"><B>Summary</B></FONT></H2><P>In this hour, you learned about the tree view control and created two examples:a tree view used as an SDI main view and a tree view control in a dialog box. Youalso learned about drag and drop as well as edit notifications.<H2><FONT COLOR="#000077"><B>Q&amp;A</B></FONT></H2><DL>	<DD><B>Q How can I allow or deny label editing based on the text the user has entered	into a label?</B><BR>	<BR>	<B>A</B> When you receive the <TT>TVN_ENDLABELEDIT</TT> message, you can check the	value stored in the <TT>pszText</TT> member of the item structure. If the string	is valid, set <TT>*pResult</TT> to <TT>TRUE</TT>, otherwise, set it to <TT>FALSE</TT>	to reject the change.<BR>	<BR>	<B>Q How can I force the tree view control to scroll to a particular position?</B><BR>	<BR>	<B>A</B> You can force a particular item to be visible by calling the <TT>CTreeCtrl::EnsureVisible</TT>	function:</DL><BLOCKQUOTE>	<PRE><FONT COLOR="#0066FF"><TT>m_tree.EnsureVisible(hItem);</TT></FONT></PRE></BLOCKQUOTE><PRE><FONT COLOR="#0066FF"><TT></TT></FONT></PRE><DL>	<DD>The tree view control will scroll if necessary to display the item referred to	by <TT>hItem</TT>. If the item is hidden under a parent, the tree will be expanded	to show the item.</DL><H2><FONT COLOR="#000077"><B>Workshop</B></FONT></H2><P>The Workshop is designed to help you anticipate possible questions, review whatyou've learned, and begin thinking ahead to putting your knowledge into practice.The answers to the quiz are in Appendix B, &quot;Quiz Answers.&quot;<H3><FONT COLOR="#000077"><B>Quiz</B></FONT></H3><DL>	<DD>1. What messages are sent when a user edits a tree view label?<BR>	<BR>	2. What messages must be handled to implement drag and drop?<BR>	<BR>	3. Why are two image list indexes specified when adding an item that displays an	image to a tree view control?<BR>	<BR>	4. What <TT>CTreeCtrl</TT> member function is used to remove all items in a tree	view control?<BR>	<BR>	5. How do you change the properties for a tree view control that is part of the <TT>CTreeView</TT>	class?<BR>	<BR>	6. What properties are available specifically for tree view controls, and what are	their equivalent window styles?<BR>	<BR>	7. How are individual tree view items referred to?<BR>	<BR>	8. What is the size of the images used by the tree view control?<BR>	<BR>	9. How do you insert an item as the first child under a parent?</DL><H3><FONT COLOR="#000077"><B>Exercises</B></FONT></H3><DL>	<DD>1. Modify the <TT>CAboutDlg</TT> so that you can add an item to the tree view	control.<BR>	<BR>	2. Add an additional item to the <TT>IDB_TREE</TT> image list. Use the new image	list item when a top-level tree view control item is selected.<FONT COLOR="#000077"></FONT></DL><CENTER><P><HR><A HREF="../ch18/ch18.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch20/ch20.htm"><IMGSRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A> <BR><BR><BR><IMG SRC="../button/corp.gif" WIDTH="284" HEIGHT="45" ALIGN="BOTTOM" ALT="Macmillan Computer Publishing USA"BORDER="0"></P><P>&copy; <A HREF="../copy.htm">Copyright</A>, Macmillan Computer Publishing. Allrights reserved.</CENTER></BODY></HTML>

⌨️ 快捷键说明

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